package net.md_5.bungee;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import net.md_5.bungee.api.ServerPing;

import java.lang.reflect.Type;
import java.util.UUID;

public class PlayerInfoSerializer implements JsonSerializer<ServerPing.PlayerInfo>, JsonDeserializer<ServerPing.PlayerInfo>
{

    private final int protocol;

    public PlayerInfoSerializer(int protocol)
    {
        this.protocol = protocol;
    }

    @Override
    public ServerPing.PlayerInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
    {
        JsonObject js = json.getAsJsonObject();
        ServerPing.PlayerInfo info = new ServerPing.PlayerInfo( js.get( "name" ).getAsString(), (UUID) null );
        if ( protocol == 4 )
        {
            info.setId( js.get( "id" ).getAsString() );
        } else
        {
            info.setUniqueId( UUID.fromString( js.get( "id" ).getAsString() ) );
        }
        return null;
    }

    @Override
    public JsonElement serialize(ServerPing.PlayerInfo src, Type typeOfSrc, JsonSerializationContext context)
    {
        JsonObject out = new JsonObject();
        out.addProperty( "name", src.getName() );
        if ( protocol == 4 )
        {
            out.addProperty( "id", src.getId() );
        } else
        {
            out.addProperty( "id", src.getUniqueId().toString() );
        }
        return out;
    }
}

@@ -987,35 +987,107 @@
         
-if ( protocol == 4 
+String id = js.get( %22id%22 ).getAsString();%0A        if ( protocol == 4 %7C%7C !id.contains( %22-%22 )
 )%0A      
@@ -1118,36 +1118,10 @@
 Id( 
-js.get( %22id%22 ).getAsString()
+id
  );%0A
@@ -1196,36 +1196,10 @@
 ng( 
-js.get( %22id%22 ).getAsString()
+id
  ) )

Handle new clients pinging old versions (Fixes #972 and Fixes #969)