From 00143078e351d14e289a8d16a61160c1a913c3df Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sun, 31 May 2020 18:07:09 +0200 Subject: [PATCH] Added PlayerSkin#fromUuid and PlayerSkin#fromUsername --- .../minestom/server/entity/PlayerSkin.java | 61 +++++++++++++++++++ .../minestom/server/utils/url/URLUtils.java | 40 ++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/main/java/net/minestom/server/utils/url/URLUtils.java diff --git a/src/main/java/net/minestom/server/entity/PlayerSkin.java b/src/main/java/net/minestom/server/entity/PlayerSkin.java index 21262214e..c5d4e788f 100644 --- a/src/main/java/net/minestom/server/entity/PlayerSkin.java +++ b/src/main/java/net/minestom/server/entity/PlayerSkin.java @@ -1,5 +1,14 @@ package net.minestom.server.entity; +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import net.minestom.server.utils.url.URLUtils; + +import java.io.IOException; +import java.util.Iterator; + /** * Contains all the data required to store a skin */ @@ -30,4 +39,56 @@ public class PlayerSkin { public String getSignature() { return signature; } + + /** + * Get a skin from a Mojang UUID + * + * @param uuid Mojang UUID + * @return a player skin based on the UUID + */ + public static PlayerSkin fromUuid(String uuid) { + final String url = "https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false"; + + try { + final String response = URLUtils.getText(url); + JsonObject jsonObject = (new JsonParser()).parse(response).getAsJsonObject(); + JsonArray propertiesArray = jsonObject.get("properties").getAsJsonArray(); + + Iterator iterator = propertiesArray.iterator(); + while (iterator.hasNext()) { + JsonObject propertyObject = iterator.next().getAsJsonObject(); + final String name = propertyObject.get("name").getAsString(); + if (!name.equals("textures")) + continue; + final String textureValue = propertyObject.get("value").getAsString(); + final String signatureValue = propertyObject.get("signature").getAsString(); + return new PlayerSkin(textureValue, signatureValue); + } + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + /** + * Get a skin from a Minecraft username + * + * @param username the Minecraft username + * @return a skin based on a Minecraft username + */ + public static PlayerSkin fromUsername(String username) { + final String url = "https://api.mojang.com/users/profiles/minecraft/" + username; + + try { + final String response = URLUtils.getText(url); + JsonObject jsonObject = (new JsonParser()).parse(response).getAsJsonObject(); + final String uuid = jsonObject.get("id").getAsString(); + return fromUuid(uuid); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + } diff --git a/src/main/java/net/minestom/server/utils/url/URLUtils.java b/src/main/java/net/minestom/server/utils/url/URLUtils.java new file mode 100644 index 000000000..ac5682133 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/url/URLUtils.java @@ -0,0 +1,40 @@ +package net.minestom.server.utils.url; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; + +public class URLUtils { + + public static String getText(String url) throws IOException { + HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); + //add headers to the connection, or check the status if desired.. + + // handle error response code it occurs + int responseCode = connection.getResponseCode(); + InputStream inputStream; + if (200 <= responseCode && responseCode <= 299) { + inputStream = connection.getInputStream(); + } else { + inputStream = connection.getErrorStream(); + } + + BufferedReader in = new BufferedReader( + new InputStreamReader( + inputStream)); + + StringBuilder response = new StringBuilder(); + String currentLine; + + while ((currentLine = in.readLine()) != null) + response.append(currentLine); + + in.close(); + + return response.toString(); + } + +}