Core/src/de/joethei/core/api/utils/GameProfileBuilder.java

145 lines
5.6 KiB
Java

package de.joethei.core.api.utils;
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.JsonParser;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.util.UUIDTypeAdapter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
public class GameProfileBuilder
{
private HashMap<UUID, CachedProfile> cache = new HashMap<UUID, CachedProfile>();
private Gson gson;
public GameProfileBuilder()
{
GsonBuilder builder = new GsonBuilder().disableHtmlEscaping();
builder.registerTypeAdapter(UUID.class, new UUIDTypeAdapter());
builder.registerTypeAdapter(GameProfile.class, new GameProfileSerializer());
builder.registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer());
this.gson = builder.create();
}
public GameProfile fetch(UUID uuid) throws Exception {
return fetch(uuid, false);
}
public GameProfile fetch(UUID uuid, boolean forceNew) throws Exception {
if ((!forceNew) && (this.cache.containsKey(uuid)) && (this.cache.get(uuid).isValid())) {
return this.cache.get(uuid).profile;
}
HttpURLConnection connection = (HttpURLConnection)new URL(String.format("https://sessionserver.mojang.com/session/minecraft/profile/%s?unsigned=false", new Object[] { UUIDTypeAdapter.fromUUID(uuid) })).openConnection();
connection.setReadTimeout(5000);
if (connection.getResponseCode() == 200) {
String json = new BufferedReader(new InputStreamReader(connection.getInputStream())).readLine();
GameProfile result = this.gson.fromJson(json, GameProfile.class);
this.cache.put(uuid, new CachedProfile(result));
return result;
}
if ((!forceNew) && (this.cache.containsKey(uuid))) {
return this.cache.get(uuid).profile;
}
JsonObject error = (JsonObject)new JsonParser().parse(new BufferedReader(new InputStreamReader(connection.getErrorStream())).readLine());
throw new Exception(error.get("error").getAsString() + ": " + error.get("errorMessage").getAsString());
}
public GameProfile getProfile(UUID uuid, String name, String skinUrl)
{
return getProfile(uuid, name, skinUrl, null);
}
@SuppressWarnings({ "rawtypes" })
public GameProfile getProfile(UUID uuid, String name, String skinUrl, String capeUrl) {
GameProfile profile = new GameProfile(uuid, name);
boolean cape = (capeUrl != null) && (!capeUrl.isEmpty());
List<Comparable> args = new ArrayList<Comparable>();
args.add(Long.valueOf(System.currentTimeMillis()));
args.add(UUIDTypeAdapter.fromUUID(uuid));
args.add(name);
args.add(skinUrl);
if (cape) args.add(capeUrl);
profile.getProperties().put("textures", new Property("textures", Base64Coder.encodeString(String.format(cape ? "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"},\"CAPE\":{\"url\":\"%s\"}}}" : "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}", args.toArray(new Object[args.size()])))));
return profile;
}
private class CachedProfile
{
private GameProfile profile;
private long timestamp;
public CachedProfile(GameProfile profile)
{
this.profile = profile;
this.timestamp = System.currentTimeMillis();
}
public boolean isValid() {
return System.currentTimeMillis() - this.timestamp < 150000L;
}
}
private class GameProfileSerializer
implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile>
{
private GameProfileSerializer()
{
}
@SuppressWarnings("rawtypes")
public GameProfile deserialize(JsonElement json, Type type, JsonDeserializationContext context)
throws JsonParseException
{
JsonObject object = (JsonObject)json;
UUID id = object.has("id") ? (UUID)context.deserialize(object.get("id"), UUID.class) : null;
String name = object.has("name") ? object.getAsJsonPrimitive("name").getAsString() : null;
GameProfile profile = new GameProfile(id, name);
PropertyMap properties = object.has("properties") ? (PropertyMap)context.deserialize(object.get("properties"), PropertyMap.class) : null;
if (properties != null) {
for (Map.Entry prop : properties.entries()) {
profile.getProperties().put((String)prop.getKey(), (Property)prop.getValue());
}
}
return profile;
}
public JsonElement serialize(GameProfile profile, Type type, JsonSerializationContext context) {
JsonObject result = new JsonObject();
if (profile.getId() != null)
result.add("id", context.serialize(profile.getId()));
if (profile.getName() != null)
result.addProperty("name", profile.getName());
if (!profile.getProperties().isEmpty())
result.add("properties", context.serialize(profile.getProperties()));
return result;
}
}
}