package protocolsupport.protocol.packet.middleimpl.clientbound.play.v_7;

import java.util.List;
import java.util.UUID;

import protocolsupport.api.ProtocolVersion;
import protocolsupport.api.utils.ProfileProperty;
import protocolsupport.protocol.packet.ClientBoundPacket;
import protocolsupport.protocol.packet.middle.clientbound.play.MiddleSpawnNamed;
import protocolsupport.protocol.packet.middleimpl.ClientBoundPacketData;
import protocolsupport.protocol.serializer.StringSerializer;
import protocolsupport.protocol.serializer.VarNumberSerializer;
import protocolsupport.protocol.storage.netcache.PlayerListCache.PlayerListEntry;
import protocolsupport.protocol.typeremapper.legacy.LegacyDataWatcherSerializer;
import protocolsupport.utils.Utils;
import protocolsupport.utils.recyclable.RecyclableCollection;
import protocolsupport.utils.recyclable.RecyclableSingletonList;

public class SpawnNamed extends MiddleSpawnNamed {

	@Override
	public RecyclableCollection<ClientBoundPacketData> toData() {
		ProtocolVersion version = connection.getVersion();
		ClientBoundPacketData serializer = ClientBoundPacketData.create(ClientBoundPacket.PLAY_SPAWN_NAMED_ID);
		VarNumberSerializer.writeVarInt(serializer, entity.getId());
		UUID uuid = entity.getUUID();
		StringSerializer.writeString(serializer, version, version == ProtocolVersion.MINECRAFT_1_7_10 ? uuid.toString() : uuid.toString().replace("-", ""));
		PlayerListEntry entry = cache.getPlayerListCache().getEntry(uuid);
		StringSerializer.writeString(serializer, version, entry != null ? Utils.clampString(entry.getCurrentName(cache.getAttributesCache().getLocale()), 16) : "UNKNOWN");
		if (version == ProtocolVersion.MINECRAFT_1_7_10) {
			List<ProfileProperty> properties = entry.getProperties(true);
			VarNumberSerializer.writeVarInt(serializer, properties.size());
			for (ProfileProperty property : properties) {
				StringSerializer.writeString(serializer, version, property.getName());
				StringSerializer.writeString(serializer, version, property.getValue());
				StringSerializer.writeString(serializer, version, property.getSignature());
			}
		}
		serializer.writeInt((int) (x * 32));
		serializer.writeInt((int) (y * 32));
		serializer.writeInt((int) (z * 32));
		serializer.writeByte(yaw);
		serializer.writeByte(pitch);
		serializer.writeShort(0);
		LegacyDataWatcherSerializer.encodeData(serializer, version, cache.getAttributesCache().getLocale(), metadata.getRemapped());
		return RecyclableSingletonList.create(serializer);
	}

}

@@ -1472,16 +1472,40 @@
 (uuid);%0A
+%09%09if (entry != null) %7B%0A%09
 %09%09String
@@ -1551,24 +1551,8 @@
 ion,
- entry != null ?
  Uti
@@ -1635,25 +1635,14 @@
  16)
- : %22UNKNOWN%22
 );%0A%09%09
+%09
 if (
@@ -1688,16 +1688,17 @@
 7_10) %7B%0A
+%09
 %09%09%09List%3C
@@ -1757,16 +1757,17 @@
 ue);%0A%09%09%09
+%09
 VarNumbe
@@ -1822,16 +1822,17 @@
 ize());%0A
+%09
 %09%09%09for (
@@ -1872,24 +1872,25 @@
 ties) %7B%0A%09%09%09%09
+%09
 StringSerial
@@ -1944,24 +1944,25 @@
 getName());%0A
+%09
 %09%09%09%09StringSe
@@ -2025,24 +2025,25 @@
 lue());%0A%09%09%09%09
+%09
 StringSerial
@@ -2106,16 +2106,209 @@
 ure());%0A
+%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09StringSerializer.writeString(serializer, version, %22UNKNOWN%22);%0A%09%09%09if (version == ProtocolVersion.MINECRAFT_1_7_10) %7B%0A%09%09%09%09VarNumberSerializer.writeVarInt(serializer, 0);%0A
 %09%09%09%7D%0A%09%09%7D

Fix possible npe when spawning player entity not added to tablist in 1.7.10