Allow usage of :slim to get the slim version of a skin
This commit is contained in:
		| @@ -25,12 +25,18 @@ public class SkinUtils { | |||||||
|         void onSuccess(WrappedGameProfile profile); |         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() { |         new BukkitRunnable() { | ||||||
|             @Override |             @Override | ||||||
|             public void run() { |             public void run() { | ||||||
|                 try { |                 try { | ||||||
|                     MineSkinResponse response = DisguiseUtilities.getMineSkinAPI().generateFromFile(callback, file); |                     MineSkinResponse response = DisguiseUtilities.getMineSkinAPI() | ||||||
|  |                             .generateFromFile(callback, file, modelType); | ||||||
|  |  | ||||||
|                     new BukkitRunnable() { |                     new BukkitRunnable() { | ||||||
|                         @Override |                         @Override | ||||||
| @@ -42,7 +48,7 @@ public class SkinUtils { | |||||||
|                                 return; |                                 return; | ||||||
|                             } |                             } | ||||||
|  |  | ||||||
|                             handleProfile(response.getGameProfile(), callback); |                             handleProfile(response.getGameProfile(), modelType, callback); | ||||||
|                         } |                         } | ||||||
|                     }.runTask(LibsDisguises.getInstance()); |                     }.runTask(LibsDisguises.getInstance()); | ||||||
|                 } |                 } | ||||||
| @@ -58,11 +64,12 @@ public class SkinUtils { | |||||||
|         }.runTaskAsynchronously(LibsDisguises.getInstance()); |         }.runTaskAsynchronously(LibsDisguises.getInstance()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void handleUrl(String url, SkinCallback callback) { |     public static void handleUrl(String url, ModelType modelType, SkinCallback callback) { | ||||||
|         new BukkitRunnable() { |         new BukkitRunnable() { | ||||||
|             @Override |             @Override | ||||||
|             public void run() { |             public void run() { | ||||||
|                 MineSkinResponse response = DisguiseUtilities.getMineSkinAPI().generateFromUrl(callback, url); |                 MineSkinResponse response = DisguiseUtilities.getMineSkinAPI() | ||||||
|  |                         .generateFromUrl(callback, url, modelType); | ||||||
|  |  | ||||||
|                 new BukkitRunnable() { |                 new BukkitRunnable() { | ||||||
|                     @Override |                     @Override | ||||||
| @@ -73,14 +80,14 @@ public class SkinUtils { | |||||||
|                             callback.onError(LibsMsg.SKIN_API_FAIL); |                             callback.onError(LibsMsg.SKIN_API_FAIL); | ||||||
|                         } |                         } | ||||||
|  |  | ||||||
|                         handleProfile(response.getGameProfile(), callback); |                         handleProfile(response.getGameProfile(), modelType, callback); | ||||||
|                     } |                     } | ||||||
|                 }.runTask(LibsDisguises.getInstance()); |                 }.runTask(LibsDisguises.getInstance()); | ||||||
|             } |             } | ||||||
|         }.runTaskAsynchronously(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() { |         WrappedGameProfile gameProfile = DisguiseUtilities.getProfileFromMojang(playerName, new LibsProfileLookup() { | ||||||
|             @Override |             @Override | ||||||
|             public void onLookup(WrappedGameProfile gameProfile) { |             public void onLookup(WrappedGameProfile gameProfile) { | ||||||
| @@ -94,7 +101,7 @@ public class SkinUtils { | |||||||
|                     return; |                     return; | ||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 handleProfile(gameProfile, callback); |                 handleProfile(gameProfile, modelType, callback); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
| @@ -108,18 +115,18 @@ public class SkinUtils { | |||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         handleProfile(gameProfile, callback); |         handleProfile(gameProfile, modelType, callback); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void handleProfile(GameProfile profile, SkinCallback callback) { |     public static void handleProfile(GameProfile profile, ModelType modelType, SkinCallback callback) { | ||||||
|         handleProfile(WrappedGameProfile.fromHandle(profile), 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); |         callback.onSuccess(profile); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void handleUUID(UUID uuid, SkinCallback callback) { |     public static void handleUUID(UUID uuid, ModelType modelType, SkinCallback callback) { | ||||||
|         new BukkitRunnable() { |         new BukkitRunnable() { | ||||||
|             @Override |             @Override | ||||||
|             public void run() { |             public void run() { | ||||||
| @@ -134,7 +141,7 @@ public class SkinUtils { | |||||||
|                             return; |                             return; | ||||||
|                         } |                         } | ||||||
|  |  | ||||||
|                         handleProfile(profile, callback); |                         handleProfile(profile, modelType, callback); | ||||||
|                     } |                     } | ||||||
|                 }.runTask(LibsDisguises.getInstance()); |                 }.runTask(LibsDisguises.getInstance()); | ||||||
|             } |             } | ||||||
| @@ -158,10 +165,17 @@ public class SkinUtils { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void grabSkin(String param, SkinCallback callback) { |     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?:\\/\\/.+")) { |         if (param.matches("https?:\\/\\/.+")) { | ||||||
|             // Its an url |             // Its an url | ||||||
|             callback.onInfo(LibsMsg.SKIN_API_USING_URL); |             callback.onInfo(LibsMsg.SKIN_API_USING_URL); | ||||||
|             handleUrl(param, callback); |  | ||||||
|  |             handleUrl(param, modelType, callback); | ||||||
|         } else { |         } else { | ||||||
|             // Check if it contains legal file characters |             // Check if it contains legal file characters | ||||||
|             if (!param.matches("[a-zA-Z0-9 -_]+(\\.png)?")) { |             if (!param.matches("[a-zA-Z0-9 -_]+(\\.png)?")) { | ||||||
| @@ -183,7 +197,7 @@ public class SkinUtils { | |||||||
|  |  | ||||||
|             if (file != null) { |             if (file != null) { | ||||||
|                 callback.onInfo(LibsMsg.SKIN_API_USING_FILE); |                 callback.onInfo(LibsMsg.SKIN_API_USING_FILE); | ||||||
|                 handleFile(file, callback); |                 handleFile(file, modelType, callback); | ||||||
|                 // We're using a file! |                 // We're using a file! | ||||||
|             } else { |             } else { | ||||||
|                 // We're using a player name or UUID! |                 // We're using a player name or UUID! | ||||||
| @@ -192,7 +206,7 @@ public class SkinUtils { | |||||||
|                         UUID uuid = UUID.fromString(param); |                         UUID uuid = UUID.fromString(param); | ||||||
|  |  | ||||||
|                         callback.onInfo(LibsMsg.SKIN_API_USING_UUID); |                         callback.onInfo(LibsMsg.SKIN_API_USING_UUID); | ||||||
|                         handleUUID(uuid, callback); |                         handleUUID(uuid, modelType, callback); | ||||||
|                         return; |                         return; | ||||||
|                     } |                     } | ||||||
|                     catch (Exception ignored) { |                     catch (Exception ignored) { | ||||||
| @@ -200,7 +214,7 @@ public class SkinUtils { | |||||||
|                 } |                 } | ||||||
|  |  | ||||||
|                 callback.onInfo(LibsMsg.SKIN_API_USING_NAME); |                 callback.onInfo(LibsMsg.SKIN_API_USING_NAME); | ||||||
|                 handleName(param, callback); |                 handleName(param, modelType, callback); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -52,11 +52,13 @@ public class MineSkinAPI { | |||||||
|      * |      * | ||||||
|      * @param url |      * @param url | ||||||
|      */ |      */ | ||||||
|     public MineSkinResponse generateFromUrl(SkinUtils.SkinCallback callback, String url) { |     public MineSkinResponse generateFromUrl(SkinUtils.SkinCallback callback, String url, | ||||||
|         return doPost(callback, "/generate/url", url, null); |             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(); |         lock.lock(); | ||||||
|  |  | ||||||
|         HttpURLConnection connection = null; |         HttpURLConnection connection = null; | ||||||
| @@ -102,13 +104,20 @@ public class MineSkinAPI { | |||||||
|                     writer.append(CRLF).append(skinUrl).append(CRLF).flush(); |                     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. |                 // End of multipart/form-data. | ||||||
|                 writer.append("--").append(boundary).append("--").append(CRLF).flush(); |                 writer.append("--").append(boundary).append("--").append(CRLF).flush(); | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             if (connection.getResponseCode() == 500) { |             if (connection.getResponseCode() == 500) { | ||||||
|                 APIError error = new Gson().fromJson(new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)) |                 APIError error = new Gson().fromJson( | ||||||
|                         .lines().collect(Collectors.joining("\n")), APIError.class); |                         new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)) | ||||||
|  |                                 .lines().collect(Collectors.joining("\n")), APIError.class); | ||||||
|  |  | ||||||
|                 if (error.code == 403) { |                 if (error.code == 403) { | ||||||
|                     callback.onError(LibsMsg.SKIN_API_FAIL_CODE, "" + error.code, LibsMsg.SKIN_API_403.get()); |                     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 |             // Get the input stream, what we receive | ||||||
|             try (InputStream input = connection.getInputStream()) { |             try (InputStream input = connection.getInputStream()) { | ||||||
|                 // Read it to string |                 // Read it to string | ||||||
|                 String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)) |                 String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines() | ||||||
|                         .lines().collect(Collectors.joining("\n")); |                         .collect(Collectors.joining("\n")); | ||||||
|  |  | ||||||
|                 MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class); |                 MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class); | ||||||
|  |  | ||||||
| @@ -176,22 +185,27 @@ public class MineSkinAPI { | |||||||
|         return null; |         return null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public MineSkinResponse generateFromUUID(UUID uuid) throws IllegalArgumentException { |     public MineSkinResponse generateFromUUID(UUID uuid, SkinUtils.ModelType modelType) throws IllegalArgumentException { | ||||||
|         lock.lock(); |         lock.lock(); | ||||||
|  |  | ||||||
|         try { |         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 |             // Creating a connection | ||||||
|             HttpURLConnection con = (HttpURLConnection) url.openConnection(); |             HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||||||
|             con.setRequestProperty("User-Agent", "LibsDisguises"); |             con.setRequestProperty("User-Agent", "LibsDisguises"); | ||||||
|             // We're writing a body that contains the API access key (Not required and obsolete, but!) |  | ||||||
|             con.setDoOutput(true); |             con.setDoOutput(true); | ||||||
|  |  | ||||||
|             // Get the input stream, what we receive |             // Get the input stream, what we receive | ||||||
|             try (InputStream input = con.getInputStream()) { |             try (InputStream input = con.getInputStream()) { | ||||||
|                 // Read it to string |                 // Read it to string | ||||||
|                 String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)) |                 String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines() | ||||||
|                         .lines().collect(Collectors.joining("\n")); |                         .collect(Collectors.joining("\n")); | ||||||
|  |  | ||||||
|                 MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class); |                 MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class); | ||||||
|  |  | ||||||
| @@ -223,7 +237,8 @@ public class MineSkinAPI { | |||||||
|      * |      * | ||||||
|      * @param file |      * @param file | ||||||
|      */ |      */ | ||||||
|     public MineSkinResponse generateFromFile(SkinUtils.SkinCallback callback, File file) { |     public MineSkinResponse generateFromFile(SkinUtils.SkinCallback callback, File file, | ||||||
|         return doPost(callback, "/generate/upload", null, file); |             SkinUtils.ModelType modelType) { | ||||||
|  |         return doPost(callback, "/generate/upload", null, file, modelType); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user