diff --git a/api/src/main/java/net/jitse/npclib/api/skin/MineSkinFetcher.java b/api/src/main/java/net/jitse/npclib/api/skin/MineSkinFetcher.java index a50dafc..e657699 100755 --- a/api/src/main/java/net/jitse/npclib/api/skin/MineSkinFetcher.java +++ b/api/src/main/java/net/jitse/npclib/api/skin/MineSkinFetcher.java @@ -20,14 +20,20 @@ */ public class MineSkinFetcher { - private static final String MINESKIN_API = "https://api.mineskin.org/get/id/"; + private static final String MINESKIN_API = "https://api.mineskin.org/get/"; private static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor(); + /** + * @deprecated + * Mineskin.org has deprecated its id system. + * Use MineSkinFetcher#fetchSkinAsync(String, Callback) instead. + */ + @Deprecated public static void fetchSkinFromIdAsync(int id, Callback callback) { EXECUTOR.execute(() -> { try { StringBuilder builder = new StringBuilder(); - HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + id).openConnection(); + HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + "id/" + id).openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); @@ -55,10 +61,16 @@ public static void fetchSkinFromIdAsync(int id, Callback callback) { }); } + /** + * @deprecated + * Mineskin.org has deprecated its id system. + * Use MineSkinFetcher#fetchSkinSync(String, Callback) instead. + */ + @Deprecated public static void fetchSkinFromIdSync(int id, Callback callback) { try { StringBuilder builder = new StringBuilder(); - HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + id).openConnection(); + HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + "id/" + id).openConnection(); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); @@ -85,6 +97,68 @@ public static void fetchSkinFromIdSync(int id, Callback callback) { } } + public static void fetchSkinAsync(String uuid, Callback callback) { + EXECUTOR.execute(() -> { + try { + StringBuilder builder = new StringBuilder(); + HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + "uuid/" + uuid).openConnection(); + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setDoOutput(true); + httpURLConnection.setDoInput(true); + httpURLConnection.connect(); + + Scanner scanner = new Scanner(httpURLConnection.getInputStream()); + while (scanner.hasNextLine()) { + builder.append(scanner.nextLine()); + } + + scanner.close(); + httpURLConnection.disconnect(); + + JsonObject jsonObject = (JsonObject) new JsonParser().parse(builder.toString()); + JsonObject textures = jsonObject.get("data").getAsJsonObject().get("texture").getAsJsonObject(); + String value = textures.get("value").getAsString(); + String signature = textures.get("signature").getAsString(); + + callback.call(new Skin(value, signature)); + } catch (IOException exception) { + Bukkit.getLogger().severe("Could not fetch skin! (uuid: " + uuid + "). Message: " + exception.getMessage()); + exception.printStackTrace(); + callback.failed(); + } + }); + } + + public static void fetchSkinSync(String uuid, Callback callback) { + try { + StringBuilder builder = new StringBuilder(); + HttpURLConnection httpURLConnection = (HttpURLConnection) new URL(MINESKIN_API + "uuid/" + uuid).openConnection(); + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setDoOutput(true); + httpURLConnection.setDoInput(true); + httpURLConnection.connect(); + + Scanner scanner = new Scanner(httpURLConnection.getInputStream()); + while (scanner.hasNextLine()) { + builder.append(scanner.nextLine()); + } + + scanner.close(); + httpURLConnection.disconnect(); + + JsonObject jsonObject = (JsonObject) new JsonParser().parse(builder.toString()); + JsonObject textures = jsonObject.get("data").getAsJsonObject().get("texture").getAsJsonObject(); + String value = textures.get("value").getAsString(); + String signature = textures.get("signature").getAsString(); + + callback.call(new Skin(value, signature)); + } catch (IOException exception) { + Bukkit.getLogger().severe("Could not fetch skin! (uuid: " + uuid + "). Message: " + exception.getMessage()); + exception.printStackTrace(); + callback.failed(); + } + } + public interface Callback { void call(Skin skinData);