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

import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;

import protocolsupport.api.ProtocolVersion;
import protocolsupport.protocol.packet.ClientBoundPacket;
import protocolsupport.protocol.packet.middle.clientbound.play.MiddlePlayerListSetEntry;
import protocolsupport.protocol.packet.middleimpl.ClientBoundPacketData;
import protocolsupport.protocol.serializer.StringSerializer;
import protocolsupport.protocol.storage.netcache.PlayerListCache;
import protocolsupport.utils.Utils;
import protocolsupport.utils.recyclable.RecyclableArrayList;
import protocolsupport.utils.recyclable.RecyclableCollection;

public class PlayerListSetEntry extends MiddlePlayerListSetEntry {

	protected final HashMap<UUID, String> oldNames = new HashMap<>();

	@Override
	public boolean postFromServerRead() {
		PlayerListCache plcache = cache.getPlayerListCache();
		oldNames.clear();
		Arrays.stream(infos).forEach(info -> oldNames.put(info.uuid, plcache.getEntry(info.uuid).getName(cache.getAttributesCache().getLocale())));
		return super.postFromServerRead();
	}

	@Override
	public RecyclableCollection<ClientBoundPacketData> toData() {
		ProtocolVersion version = connection.getVersion();
		RecyclableArrayList<ClientBoundPacketData> packets = RecyclableArrayList.create();
		switch (action) {
			case ADD: {
				Arrays.stream(infos).forEach(info -> packets.add(create(info.getName(cache.getAttributesCache().getLocale()), true, version)));
				break;
			}
			case DISPLAY_NAME: {
				Arrays.stream(infos).forEach(info -> {
					packets.add(create(oldNames.get(info.uuid), false, version));
					packets.add(create(info.getName(cache.getAttributesCache().getLocale()), true, version));
				});
				break;
			}
			case REMOVE: {
				Arrays.stream(infos).forEach(info -> packets.add(create(oldNames.get(info.uuid), false, version)));
				break;
			}
			default: {
				break;
			}
		}
		return packets;
	}

	protected static ClientBoundPacketData create(String name, boolean add, ProtocolVersion version) {
		ClientBoundPacketData serializer = ClientBoundPacketData.create(ClientBoundPacket.PLAY_PLAYER_INFO_ID);
		StringSerializer.writeString(serializer, version, Utils.clampString(name, 16));
		serializer.writeBoolean(add);
		serializer.writeShort(0);
		return serializer;
	}

}

@@ -1496,32 +1496,51 @@
 ().getLocale()),
+ (short) info.ping,
  true, version))
@@ -1665,32 +1665,51 @@
 .get(info.uuid),
+ (short) info.ping,
  false, version)
@@ -1788,16 +1788,35 @@
 cale()),
+ (short) info.ping,
  true, v
@@ -1951,16 +1951,35 @@
 o.uuid),
+ (short) info.ping,
  false, 
@@ -2121,16 +2121,28 @@
 ng name,
+ short ping,
  boolean
@@ -2422,9 +2422,12 @@
 ort(
-0
+ping
 );%0A%09

Add ping updates for pre-1.8 clients