From 64641c53901d5b7fd85e5fd8db05b9e379ed21ac Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Fri, 10 Apr 2020 13:05:10 +1200 Subject: [PATCH] Allow usage of :slim to get the slim version of a skin --- .../disguise/utilities/SkinUtils.java | 50 ++++++++++++------- .../utilities/mineskin/MineSkinAPI.java | 43 ++++++++++------ 2 files changed, 61 insertions(+), 32 deletions(-) diff --git a/src/main/java/me/libraryaddict/disguise/utilities/SkinUtils.java b/src/main/java/me/libraryaddict/disguise/utilities/SkinUtils.java index eadfcaf3..fa0bffd6 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/SkinUtils.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/SkinUtils.java @@ -25,12 +25,18 @@ public class SkinUtils { void onSuccess(WrappedGameProfile profile); } - public static void handleFile(File file, SkinCallback callback) { + public enum ModelType { + SLIM, + NORMAL + } + + public static void handleFile(File file, ModelType modelType, SkinCallback callback) { new BukkitRunnable() { @Override public void run() { try { - MineSkinResponse response = DisguiseUtilities.getMineSkinAPI().generateFromFile(callback, file); + MineSkinResponse response = DisguiseUtilities.getMineSkinAPI() + .generateFromFile(callback, file, modelType); new BukkitRunnable() { @Override @@ -42,7 +48,7 @@ public class SkinUtils { return; } - handleProfile(response.getGameProfile(), callback); + handleProfile(response.getGameProfile(), modelType, callback); } }.runTask(LibsDisguises.getInstance()); } @@ -58,11 +64,12 @@ public class SkinUtils { }.runTaskAsynchronously(LibsDisguises.getInstance()); } - public static void handleUrl(String url, SkinCallback callback) { + public static void handleUrl(String url, ModelType modelType, SkinCallback callback) { new BukkitRunnable() { @Override public void run() { - MineSkinResponse response = DisguiseUtilities.getMineSkinAPI().generateFromUrl(callback, url); + MineSkinResponse response = DisguiseUtilities.getMineSkinAPI() + .generateFromUrl(callback, url, modelType); new BukkitRunnable() { @Override @@ -73,14 +80,14 @@ public class SkinUtils { callback.onError(LibsMsg.SKIN_API_FAIL); } - handleProfile(response.getGameProfile(), callback); + handleProfile(response.getGameProfile(), modelType, callback); } }.runTask(LibsDisguises.getInstance()); } }.runTaskAsynchronously(LibsDisguises.getInstance()); } - public static void handleName(String playerName, SkinCallback callback) { + public static void handleName(String playerName, ModelType modelType, SkinCallback callback) { WrappedGameProfile gameProfile = DisguiseUtilities.getProfileFromMojang(playerName, new LibsProfileLookup() { @Override public void onLookup(WrappedGameProfile gameProfile) { @@ -94,7 +101,7 @@ public class SkinUtils { return; } - handleProfile(gameProfile, callback); + handleProfile(gameProfile, modelType, callback); } }); @@ -108,18 +115,18 @@ public class SkinUtils { return; } - handleProfile(gameProfile, callback); + handleProfile(gameProfile, modelType, callback); } - public static void handleProfile(GameProfile profile, SkinCallback callback) { - handleProfile(WrappedGameProfile.fromHandle(profile), callback); + public static void handleProfile(GameProfile profile, ModelType modelType, SkinCallback callback) { + handleProfile(WrappedGameProfile.fromHandle(profile), modelType, callback); } - public static void handleProfile(WrappedGameProfile profile, SkinCallback callback) { + public static void handleProfile(WrappedGameProfile profile, ModelType modelType, SkinCallback callback) { callback.onSuccess(profile); } - public static void handleUUID(UUID uuid, SkinCallback callback) { + public static void handleUUID(UUID uuid, ModelType modelType, SkinCallback callback) { new BukkitRunnable() { @Override public void run() { @@ -134,7 +141,7 @@ public class SkinUtils { return; } - handleProfile(profile, callback); + handleProfile(profile, modelType, callback); } }.runTask(LibsDisguises.getInstance()); } @@ -158,10 +165,17 @@ public class SkinUtils { } public static void grabSkin(String param, SkinCallback callback) { + ModelType modelType = param.toLowerCase().endsWith(":slim") ? ModelType.SLIM : ModelType.NORMAL; + + if (modelType == ModelType.SLIM) { + param = param.substring(0, param.length() - ":slim".length()); + } + if (param.matches("https?:\\/\\/.+")) { // Its an url callback.onInfo(LibsMsg.SKIN_API_USING_URL); - handleUrl(param, callback); + + handleUrl(param, modelType, callback); } else { // Check if it contains legal file characters if (!param.matches("[a-zA-Z0-9 -_]+(\\.png)?")) { @@ -183,7 +197,7 @@ public class SkinUtils { if (file != null) { callback.onInfo(LibsMsg.SKIN_API_USING_FILE); - handleFile(file, callback); + handleFile(file, modelType, callback); // We're using a file! } else { // We're using a player name or UUID! @@ -192,7 +206,7 @@ public class SkinUtils { UUID uuid = UUID.fromString(param); callback.onInfo(LibsMsg.SKIN_API_USING_UUID); - handleUUID(uuid, callback); + handleUUID(uuid, modelType, callback); return; } catch (Exception ignored) { @@ -200,7 +214,7 @@ public class SkinUtils { } callback.onInfo(LibsMsg.SKIN_API_USING_NAME); - handleName(param, callback); + handleName(param, modelType, callback); } } } diff --git a/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java b/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java index 86b9e13c..796a24b2 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java @@ -52,11 +52,13 @@ public class MineSkinAPI { * * @param url */ - public MineSkinResponse generateFromUrl(SkinUtils.SkinCallback callback, String url) { - return doPost(callback, "/generate/url", url, null); + public MineSkinResponse generateFromUrl(SkinUtils.SkinCallback callback, String url, + SkinUtils.ModelType modelType) { + return doPost(callback, "/generate/url", url, null, modelType); } - private MineSkinResponse doPost(SkinUtils.SkinCallback callback, String path, String skinUrl, File file) { + private MineSkinResponse doPost(SkinUtils.SkinCallback callback, String path, String skinUrl, File file, + SkinUtils.ModelType modelType) { lock.lock(); HttpURLConnection connection = null; @@ -102,13 +104,20 @@ public class MineSkinAPI { writer.append(CRLF).append(skinUrl).append(CRLF).flush(); } + if (modelType == SkinUtils.ModelType.SLIM) { + writer.append("--").append(boundary).append(CRLF); + writer.append("Content-Disposition: form-data; name=\"model\"").append(CRLF); + writer.append(CRLF).append("slim").append(CRLF).flush(); + } + // End of multipart/form-data. writer.append("--").append(boundary).append("--").append(CRLF).flush(); } if (connection.getResponseCode() == 500) { - APIError error = new Gson().fromJson(new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)) - .lines().collect(Collectors.joining("\n")), APIError.class); + APIError error = new Gson().fromJson( + new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)) + .lines().collect(Collectors.joining("\n")), APIError.class); if (error.code == 403) { callback.onError(LibsMsg.SKIN_API_FAIL_CODE, "" + error.code, LibsMsg.SKIN_API_403.get()); @@ -137,8 +146,8 @@ public class MineSkinAPI { // Get the input stream, what we receive try (InputStream input = connection.getInputStream()) { // Read it to string - String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)) - .lines().collect(Collectors.joining("\n")); + String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines() + .collect(Collectors.joining("\n")); MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class); @@ -176,22 +185,27 @@ public class MineSkinAPI { return null; } - public MineSkinResponse generateFromUUID(UUID uuid) throws IllegalArgumentException { + public MineSkinResponse generateFromUUID(UUID uuid, SkinUtils.ModelType modelType) throws IllegalArgumentException { lock.lock(); try { - URL url = new URL("https://api.mineskin.org/generate/user/:" + uuid.toString()); + String siteUrl = "https://api.mineskin.org/generate/user/:" + uuid.toString(); + + if (modelType == SkinUtils.ModelType.SLIM) { + siteUrl += "?model=slim"; + } + + URL url = new URL(siteUrl); // Creating a connection HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestProperty("User-Agent", "LibsDisguises"); - // We're writing a body that contains the API access key (Not required and obsolete, but!) con.setDoOutput(true); // Get the input stream, what we receive try (InputStream input = con.getInputStream()) { // Read it to string - String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)) - .lines().collect(Collectors.joining("\n")); + String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines() + .collect(Collectors.joining("\n")); MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class); @@ -223,7 +237,8 @@ public class MineSkinAPI { * * @param file */ - public MineSkinResponse generateFromFile(SkinUtils.SkinCallback callback, File file) { - return doPost(callback, "/generate/upload", null, file); + public MineSkinResponse generateFromFile(SkinUtils.SkinCallback callback, File file, + SkinUtils.ModelType modelType) { + return doPost(callback, "/generate/upload", null, file, modelType); } }