Skip to content

Instantly share code, notes, and snippets.

@codebucketdev
Created April 7, 2015 08:51
Show Gist options
  • Save codebucketdev/1695088407e95cdb4f68 to your computer and use it in GitHub Desktop.
Save codebucketdev/1695088407e95cdb4f68 to your computer and use it in GitHub Desktop.
This is an example of my GameProfile Builder in Java using the Public API from Razex.de
import java.io.BufferedReader;
import java.io.IOException;
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.Entry;
import java.util.UUID;
import org.bukkit.craftbukkit.libs.com.google.gson.Gson;
import org.bukkit.craftbukkit.libs.com.google.gson.GsonBuilder;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonArray;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializationContext;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonDeserializer;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonElement;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonObject;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonParseException;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonParser;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializationContext;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonSerializer;
import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.util.UUIDTypeAdapter;
public class GameProfileBuilder
{
private static final String SERVICE_URL = "https://api.razex.de/user/profile/%s";
private static final String JSON_SKIN = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}";
private static final String JSON_CAPE = "{\"timestamp\":%d,\"profileId\":\"%s\",\"profileName\":\"%s\",\"isPublic\":true,\"textures\":{\"SKIN\":{\"url\":\"%s\"},\"CAPE\":{\"url\":\"%s\"}}}";
private static Gson gson = new GsonBuilder().disableHtmlEscaping().registerTypeAdapter(UUID.class, new UUIDTypeAdapter()).registerTypeAdapter(GameProfile.class, new GameProfileSerializer()).registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create();
private static HashMap<UUID, CachedProfile> cache = new HashMap<UUID, CachedProfile>();
private static long cacheTime = -1;
public static GameProfile fetch(UUID uuid) throws IOException
{
return fetch(uuid, false);
}
public static GameProfile fetch(UUID uuid, boolean forceNew) throws IOException
{
if (!forceNew && cache.containsKey(uuid) && cache.get(uuid).isValid())
{
return cache.get(uuid).profile;
}
else
{
HttpURLConnection connection = (HttpURLConnection) new URL(String.format(SERVICE_URL, UUIDTypeAdapter.fromUUID(uuid))).openConnection();
connection.setReadTimeout(5000);
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK)
{
InputStreamReader response = new InputStreamReader(connection.getInputStream());
GameProfile result = gson.fromJson(new BufferedReader(response), GameProfile.class);
cache.put(uuid, new CachedProfile(result));
return result;
}
else
{
if (!forceNew && cache.containsKey(uuid))
{
return cache.get(uuid).profile;
}
JsonObject error = (JsonObject) new JsonParser().parse(new BufferedReader(new InputStreamReader(connection.getErrorStream())).readLine());
throw new IOException(error.get("error").getAsString() + ": " + error.get("errorMessage").getAsString());
}
}
}
public static GameProfile getProfile(UUID uuid, String name, String skin)
{
return getProfile(uuid, name, skin, null);
}
public static GameProfile getProfile(UUID uuid, String name, String skinUrl, String capeUrl)
{
GameProfile profile = new GameProfile(uuid, name);
boolean cape = capeUrl != null && !capeUrl.isEmpty();
List<Object> args = new ArrayList<Object>();
args.add(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 ? JSON_CAPE : JSON_SKIN, args.toArray(new Object[args.size()])))));
return profile;
}
public static void setCacheTime(long time)
{
GameProfileBuilder.cacheTime = time;
}
private static class GameProfileSerializer implements JsonSerializer<GameProfile>, JsonDeserializer<GameProfile>
{
@Override
public GameProfile deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException
{
JsonObject object = (JsonObject) json;
UUID uuid = object.has("uuid") ? (UUID) context.deserialize(object.get("uuid"), UUID.class) : null;
String username = object.has("username") ? object.getAsJsonPrimitive("username").getAsString() : null;
if (object.has("properties"))
{
JsonArray properties = object.getAsJsonArray("properties");
for (JsonElement entry : properties)
{
JsonObject property = (JsonObject) entry;
String value = property.get("value").toString();
value = Base64Coder.encodeString(value);
property.remove("value");
property.addProperty("value", value);
}
}
PropertyMap properties = object.has("properties") ? (PropertyMap) context.deserialize(object.get("properties"), PropertyMap.class) : null;
GameProfile profile = new GameProfile(uuid, username);
if (properties != null)
{
for (Entry<String, Property> prop : properties.entries())
{
profile.getProperties().put(prop.getKey(), prop.getValue());
}
}
return profile;
}
@Override
public JsonElement serialize(GameProfile profile, Type type, JsonSerializationContext context)
{
JsonObject result = new JsonObject();
if (profile.getId() != null)
{
result.add("uuid", context.serialize(profile.getId()));
}
if (profile.getName() != null)
{
result.addProperty("username", profile.getName());
}
if (!profile.getProperties().isEmpty())
{
result.add("properties", context.serialize(profile.getProperties()));
}
if (result.has("properties"))
{
JsonArray properties = result.getAsJsonArray("properties");
for (JsonElement entry : properties)
{
JsonObject property = (JsonObject) entry;
String value = property.getAsJsonPrimitive("value").getAsString();
value = Base64Coder.decodeString(value);
property.remove("value");
property.add("value", new JsonParser().parse(value));
}
}
return result;
}
}
private static class CachedProfile
{
private long timestamp = System.currentTimeMillis();
private GameProfile profile;
public CachedProfile(GameProfile profile)
{
this.profile = profile;
}
public boolean isValid()
{
return cacheTime < 0 ? true : (System.currentTimeMillis() - timestamp) < cacheTime;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment