LibsDisguises/src/main/java/me/libraryaddict/disguise/utilities/json/SerializerGameProfile.java

34 lines
1.2 KiB
Java
Raw Normal View History

2017-06-02 15:51:03 +02:00
package me.libraryaddict.disguise.utilities.json;
import com.comphenix.protocol.wrappers.WrappedGameProfile;
import com.google.gson.*;
import com.mojang.authlib.GameProfile;
import java.lang.reflect.Type;
2017-07-01 00:44:33 +02:00
import java.util.regex.Pattern;
2017-06-02 15:51:03 +02:00
/**
* Created by libraryaddict on 1/06/2017.
*/
public class SerializerGameProfile implements JsonSerializer<WrappedGameProfile>, JsonDeserializer<WrappedGameProfile> {
@Override
public JsonElement serialize(WrappedGameProfile src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src.getHandle(), GameProfile.class);
}
@Override
public WrappedGameProfile deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
2017-07-01 00:44:33 +02:00
JsonObject obj = json.getAsJsonObject();
if (obj.has("id") && !obj.get("id").getAsString().contains("-")) {
obj.addProperty("id",
Pattern.compile("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)")
.matcher(obj.get("id").getAsString()).replaceFirst("$1-$2-$3-$4-$5"));
}
2017-06-02 15:51:03 +02:00
return WrappedGameProfile.fromHandle(context.deserialize(json, GameProfile.class));
}
}