001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package net.tridentsdk.server.packets.play.out; 019 020import io.netty.buffer.ByteBuf; 021import net.tridentsdk.server.netty.Codec; 022import net.tridentsdk.server.netty.packet.OutPacket; 023 024import java.util.UUID; 025 026public class PacketPlayOutPlayerListItem extends OutPacket { 027 028 protected int action; 029 protected PlayerListDataBuilder[] playerListData; 030 031 @Override 032 public int id() { 033 return 0x2D; 034 } 035 036 public int action() { 037 return this.action; 038 } 039 040 public PlayerListDataBuilder[] playerListData() { 041 return this.playerListData; 042 } 043 044 @Override 045 public void encode(ByteBuf buf) { 046 Codec.writeVarInt32(buf, this.action); 047 Codec.writeVarInt32(buf, this.playerListData.length); 048 049 for (PlayerListDataBuilder data : this.playerListData) { 050 data.write(buf); 051 } 052 } 053 054 public static class PlayerListDataBuilder { 055 protected UUID id; 056 protected Object[] values; 057 058 public UUID id() { 059 return this.id; 060 } 061 062 public PlayerListDataBuilder id(UUID id) { 063 this.id = id; 064 065 return this; 066 } 067 068 public Object[] values() { 069 return this.values; 070 } 071 072 public PlayerListDataBuilder values(Object... values) { 073 this.values = values; 074 075 return this; 076 } 077 078 public void write(ByteBuf buf) { 079 buf.writeLong(this.id.getMostSignificantBits()); 080 buf.writeLong(this.id.getLeastSignificantBits()); 081 082 // rip in organize 083 for (Object o : values) { 084 if (o == null) { 085 continue; 086 } 087 088 if (o.getClass().isArray()) { 089 Object[] objects = (Object[]) o; 090 for (Object o1 : objects) { 091 encode(o1, buf); 092 } 093 094 continue; 095 } 096 097 encode(o, buf); 098 } 099 } 100 101 private void encode(Object o, ByteBuf buf) { 102 switch (o.getClass().getSimpleName()) { 103 case "String": 104 Codec.writeString(buf, (String) o); 105 break; 106 107 case "Integer": 108 Codec.writeVarInt32(buf, (Integer) o); 109 break; 110 111 case "Boolean": 112 buf.writeBoolean((Boolean) o); 113 break; 114 115 default: 116 // ignore bad developers 117 break; 118 } 119 } 120 } 121}