80 lines
2.4 KiB
Java
80 lines
2.4 KiB
Java
package com.thebrokenrail.gestus.skin;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.net.URL;
|
|
import java.util.Base64;
|
|
import java.util.Map;
|
|
import java.util.UUID;
|
|
|
|
final class SkinJSON {
|
|
private static class Response {
|
|
public Property[] properties = null;
|
|
}
|
|
|
|
private static class Property {
|
|
public String name = null;
|
|
public String value = null;
|
|
}
|
|
|
|
private static class Data {
|
|
public Map<String, Texture> textures;
|
|
}
|
|
|
|
private static class Texture {
|
|
public String url = null;
|
|
}
|
|
|
|
private static String urlToString(String urlStr) {
|
|
try {
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
URL url = new URL(urlStr);
|
|
|
|
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()))) {
|
|
String line;
|
|
while ((line = in.readLine()) != null) {
|
|
stringBuilder.append(line);
|
|
stringBuilder.append('\n');
|
|
}
|
|
}
|
|
|
|
return stringBuilder.toString();
|
|
} catch (IOException e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
private static final String SKIN_TEXTURE_KEY = "SKIN";
|
|
|
|
static String get(UUID uuid) {
|
|
String url = String.format("https://sessionserver.mojang.com/session/minecraft/profile/%s", uuid.toString().replace("-", ""));
|
|
String data = urlToString(url);
|
|
|
|
if (data != null) {
|
|
Gson gson = new Gson();
|
|
Response response = gson.fromJson(data, Response.class);
|
|
|
|
if (response != null && response.properties != null) {
|
|
for (Property property : response.properties) {
|
|
if ("textures".equals(property.name)) {
|
|
String texturesJSON = new String(Base64.getDecoder().decode(property.value));
|
|
Data textures = gson.fromJson(texturesJSON, Data.class);
|
|
|
|
if (textures.textures.containsKey(SKIN_TEXTURE_KEY)) {
|
|
Texture skin = textures.textures.get(SKIN_TEXTURE_KEY);
|
|
return skin.url;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|