Created
June 18, 2015 19:15
-
-
Save Jofkos/570803bc0035f2fbc472 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Type; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.UUID; | |
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
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.JsonPrimitive; | |
import com.google.gson.JsonSerializationContext; | |
import com.google.gson.JsonSerializer; | |
import com.mojang.authlib.properties.Property; | |
import com.mojang.authlib.properties.PropertyMap; | |
import com.mojang.util.UUIDTypeAdapter; | |
@SuppressWarnings("serial") | |
public class PropertyWrapper { | |
private static Gson gson = new GsonBuilder().registerTypeAdapter(TexturesMap.class, new TexturesMapSerializer()).registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).create(); | |
private String name; | |
private String signature; | |
private ValueWrapper value; | |
public PropertyWrapper(String property, PropertyMap map) { | |
this(map.get(property).iterator().next()); | |
} | |
public PropertyWrapper(Property property) { | |
this(property.getName(), property.getValue(), property.getSignature()); | |
} | |
public PropertyWrapper(String name, String value, String signature) { | |
this.name = name; | |
this.value = gson.fromJson(Base64Coder.decodeString(value), ValueWrapper.class); | |
this.signature = signature; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getSignature() { | |
return signature; | |
} | |
public ValueWrapper getValue() { | |
return value; | |
} | |
public String getBase64Value() { | |
return Base64Coder.encodeString(gson.toJson(value)); | |
} | |
public Property toProperty() { | |
return new Property(name, getBase64Value(), signature); | |
} | |
public void saveTo(PropertyMap map) { | |
map.removeAll(name); | |
map.put(name, toProperty()); | |
} | |
public class ValueWrapper { | |
private long timestamp; | |
private UUID profileId; | |
private String profileName; | |
private boolean isPublic; | |
private TexturesMap textures; | |
public long getTimestamp() { | |
return timestamp; | |
} | |
public UUID getProfileId() { | |
return profileId; | |
} | |
public String getProfileName() { | |
return profileName; | |
} | |
public boolean isPublic() { | |
return isPublic; | |
} | |
public Map<String, String> getTextures() { | |
return textures; | |
} | |
} | |
private static class TexturesMap extends HashMap<String, String> {} | |
private static class TexturesMapSerializer implements JsonSerializer<TexturesMap>, JsonDeserializer<TexturesMap> { | |
@Override | |
public TexturesMap deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { | |
TexturesMap map = new TexturesMap(); | |
json.getAsJsonObject().entrySet().forEach(entry -> map.put(entry.getKey(), entry.getValue().getAsJsonObject().get("url").getAsString())); | |
return map; | |
} | |
@Override | |
public JsonElement serialize(TexturesMap profile, Type type, JsonSerializationContext context) { | |
JsonObject textures = new JsonObject(); | |
profile.entrySet().forEach(entry -> textures.add(entry.getKey(), serializeProperty(entry.getValue()))); | |
return textures; | |
} | |
private JsonObject serializeProperty(String url) { | |
JsonObject object = new JsonObject(); | |
object.add("url", new JsonPrimitive(url)); | |
return object; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment