2021-06-26 19:08:15 -07:00
|
|
|
package net.nuggetmc.ai.utils;
|
|
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
|
import com.google.gson.JsonParser;
|
|
|
|
|
|
2021-06-28 03:14:07 -05:00
|
|
|
import java.io.IOException;
|
2021-06-26 19:08:15 -07:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.net.URL;
|
2021-06-28 13:18:49 -05:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
2021-06-26 19:08:15 -07:00
|
|
|
|
|
|
|
|
public class MojangAPI {
|
|
|
|
|
|
2021-07-09 15:49:10 -05:00
|
|
|
private static final boolean CACHE_ENABLED = false;
|
|
|
|
|
|
2021-06-28 13:18:49 -05:00
|
|
|
private static final Map<String, String[]> CACHE = new HashMap<>();
|
|
|
|
|
|
2021-06-26 19:08:15 -07:00
|
|
|
public static String[] getSkin(String name) {
|
2021-07-09 15:49:10 -05:00
|
|
|
if (CACHE_ENABLED && CACHE.containsKey(name)) {
|
2021-06-28 13:18:49 -05:00
|
|
|
return CACHE.get(name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String[] values = pullFromAPI(name);
|
|
|
|
|
CACHE.put(name, values);
|
|
|
|
|
return values;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 03:15:30 -05:00
|
|
|
// CATCHING NULL ILLEGALSTATEEXCEPTION BAD!!!! eventually fix from the getAsJsonObject thingy
|
2021-06-28 13:18:49 -05:00
|
|
|
public static String[] pullFromAPI(String name) {
|
2021-06-26 19:08:15 -07:00
|
|
|
try {
|
|
|
|
|
String uuid = new JsonParser().parse(new InputStreamReader(new URL("https://api.mojang.com/users/profiles/minecraft/" + name)
|
|
|
|
|
.openStream())).getAsJsonObject().get("id").getAsString();
|
|
|
|
|
JsonObject property = new JsonParser()
|
|
|
|
|
.parse(new InputStreamReader(new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false")
|
|
|
|
|
.openStream())).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
|
|
|
|
|
return new String[] {property.get("value").getAsString(), property.get("signature").getAsString()};
|
2021-06-28 13:18:49 -05:00
|
|
|
} catch (IOException | IllegalStateException e) {
|
2021-06-26 19:08:15 -07:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|