package protocolsupport.protocol.transformer.middlepacket.clientbound.play;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.mojang.authlib.properties.Property;

import gnu.trove.map.TIntObjectMap;

import protocolsupport.api.ProtocolVersion;
import protocolsupport.protocol.PacketDataSerializer;
import protocolsupport.protocol.storage.LocalStorage;
import protocolsupport.protocol.transformer.middlepacket.ClientBoundMiddlePacket;
import protocolsupport.protocol.typeremapper.watchedentity.types.WatchedEntity;
import protocolsupport.protocol.typeremapper.watchedentity.types.WatchedPlayer;
import protocolsupport.utils.DataWatcherObject;
import protocolsupport.utils.DataWatcherSerializer;
import protocolsupport.utils.Utils;

public abstract class MiddleSpawnNamed<T> extends ClientBoundMiddlePacket<T> {

	protected int playerEntityId;
	protected UUID uuid;
	protected String name;
	protected int x;
	protected int y;
	protected int z;
	protected int yaw;
	protected int pitch;
	protected int itemId;
	protected List<Property> properties;
	protected WatchedEntity wplayer;
	protected TIntObjectMap<DataWatcherObject> metadata;

	@Override
	public void readFromServerData(PacketDataSerializer serializer) throws IOException {
		playerEntityId = serializer.readVarInt();
		uuid = serializer.g();
		x = serializer.readInt();
		y = serializer.readInt();
		z = serializer.readInt();
		yaw = serializer.readUnsignedByte();
		pitch = serializer.readUnsignedByte();
		itemId = serializer.readUnsignedShort();
		metadata = DataWatcherSerializer.decodeData(ProtocolVersion.MINECRAFT_1_8, Utils.toArray(serializer));
	}

	@Override
	public void handle(LocalStorage storage) {
		wplayer = new WatchedPlayer(playerEntityId);
		storage.addWatchedEntity(wplayer);
		name = storage.getPlayerListEntry(uuid).getName();
		properties = storage.getPlayerListEntry(uuid).getProperties().getAll(true);
	}	

}

@@ -382,16 +382,86 @@
 torage;%0A
+import protocolsupport.protocol.storage.LocalStorage.PlayerListEntry;%0A
 import p
@@ -1857,20 +1857,37 @@
 yer);%0A%09%09
-name
+PlayerListEntry entry
  = stora
@@ -1917,18 +1917,62 @@
 uid)
-.getName()
+;%0A%09%09name = entry != null ? entry.getName() : %22Unknown%22
 ;%0A%09%09
@@ -2051,11 +2051,10 @@
 e);%0A
+
 %09%7D
-%09
 %0A%0A%7D%0A

Fix npe when something spawns player without adding it to tab first