Add ability to enable mineskin debug
This commit is contained in:
		| @@ -35,6 +35,7 @@ public class LibsDisguisesCommand implements CommandExecutor, TabCompleter { | ||||
|         getCommands().add(new LDDebugPlayer()); | ||||
|         getCommands().add(new LDUploadLogs()); | ||||
|         getCommands().add(new LDUpdateProtocolLib()); | ||||
|         getCommands().add(new LDDebugMineSkin()); | ||||
|     } | ||||
|  | ||||
|     protected ArrayList<String> filterTabs(ArrayList<String> list, String[] origArgs) { | ||||
|   | ||||
| @@ -0,0 +1,42 @@ | ||||
| package me.libraryaddict.disguise.commands.libsdisguises; | ||||
|  | ||||
| import me.libraryaddict.disguise.utilities.DisguiseUtilities; | ||||
| import me.libraryaddict.disguise.utilities.mineskin.MineSkinAPI; | ||||
| import me.libraryaddict.disguise.utilities.translations.LibsMsg; | ||||
| import org.bukkit.command.CommandSender; | ||||
|  | ||||
| import java.util.Collections; | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * Created by libraryaddict on 22/05/2021. | ||||
|  */ | ||||
| public class LDDebugMineSkin implements LDCommand { | ||||
|     @Override | ||||
|     public List<String> getTabComplete() { | ||||
|         return Collections.singletonList("mineskin"); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public boolean hasPermission(CommandSender sender) { | ||||
|         return sender.hasPermission(getPermission()); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String getPermission() { | ||||
|         return "libsdisguises.mineskin"; | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onCommand(CommandSender sender, String[] args) { | ||||
|         MineSkinAPI api = DisguiseUtilities.getMineSkinAPI(); | ||||
|         api.setDebugging(!api.isDebugging()); | ||||
|  | ||||
|         LibsMsg.LD_DEBUG_MINESKIN_TOGGLE.send(sender, api.isDebugging()); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public LibsMsg getHelp() { | ||||
|         return LibsMsg.LD_DEBUG_MINESKIN; | ||||
|     } | ||||
| } | ||||
| @@ -1,6 +1,8 @@ | ||||
| package me.libraryaddict.disguise.utilities.mineskin; | ||||
|  | ||||
| import com.google.gson.Gson; | ||||
| import lombok.Getter; | ||||
| import lombok.Setter; | ||||
| import me.libraryaddict.disguise.utilities.DisguiseUtilities; | ||||
| import me.libraryaddict.disguise.utilities.SkinUtils; | ||||
| import me.libraryaddict.disguise.utilities.translations.LibsMsg; | ||||
| @@ -32,6 +34,9 @@ public class MineSkinAPI { | ||||
|      */ | ||||
|     private long nextRequest; | ||||
|     private final ReentrantLock lock = new ReentrantLock(); | ||||
|     @Getter | ||||
|     @Setter | ||||
|     private boolean debugging; | ||||
|  | ||||
|     public boolean isInUse() { | ||||
|         return lock.isLocked(); | ||||
| @@ -56,12 +61,28 @@ public class MineSkinAPI { | ||||
|         return doPost(callback, "/generate/url", url, null, modelType); | ||||
|     } | ||||
|  | ||||
|     private void printDebug(String message) { | ||||
|         if (!isDebugging()) { | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         System.out.println("[MineSkinAPI] " + message); | ||||
|     } | ||||
|  | ||||
|     private MineSkinResponse doPost(SkinUtils.SkinCallback callback, String path, String skinUrl, File file, SkinUtils.ModelType modelType) { | ||||
|         lock.lock(); | ||||
|  | ||||
|         long sleep = nextRequest - System.currentTimeMillis(); | ||||
|  | ||||
|         if (file != null) { | ||||
|             printDebug("Grabbing a skin from file at " + file.getPath()); | ||||
|         } else if (skinUrl != null) { | ||||
|             printDebug("Grabbing a skin from url '" + skinUrl + "'"); | ||||
|         } | ||||
|  | ||||
|         if (sleep > 0) { | ||||
|             printDebug("Sleeping for " + sleep + "ms before calling the API due to a recent request"); | ||||
|  | ||||
|             try { | ||||
|                 Thread.sleep(sleep); | ||||
|             } catch (InterruptedException e) { | ||||
| @@ -121,9 +142,15 @@ public class MineSkinAPI { | ||||
|                 writer.append("--").append(boundary).append("--").append(CRLF).flush(); | ||||
|             } | ||||
|  | ||||
|             printDebug("Received status code: " + connection.getResponseCode()); | ||||
|  | ||||
|             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); | ||||
|                 String errorMessage = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)).lines() | ||||
|                         .collect(Collectors.joining("\n")); | ||||
|  | ||||
|                 APIError error = new Gson().fromJson(errorMessage, APIError.class); | ||||
|  | ||||
|                 printDebug("Received error: " + errorMessage); | ||||
|  | ||||
|                 if (error.code == 403) { | ||||
|                     callback.onError(LibsMsg.SKIN_API_FAIL_CODE, "" + error.code, LibsMsg.SKIN_API_403.get()); | ||||
| @@ -156,6 +183,8 @@ public class MineSkinAPI { | ||||
|                 // Read it to string | ||||
|                 String response = new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); | ||||
|  | ||||
|                 printDebug("Received: " + response); | ||||
|  | ||||
|                 MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class); | ||||
|  | ||||
|                 nextRequestIn = (long) (skinResponse.getNextRequest() * 1000); | ||||
| @@ -163,6 +192,10 @@ public class MineSkinAPI { | ||||
|                 return skinResponse; | ||||
|             } | ||||
|         } catch (SocketTimeoutException ex) { | ||||
|             if (isDebugging()) { | ||||
|                 ex.printStackTrace(); | ||||
|             } | ||||
|  | ||||
|             callback.onError(skinUrl == null ? LibsMsg.SKIN_API_TIMEOUT_ERROR : LibsMsg.SKIN_API_IMAGE_TIMEOUT); | ||||
|             return null; | ||||
|         } catch (Exception ex) { | ||||
| @@ -172,10 +205,13 @@ public class MineSkinAPI { | ||||
|                     callback.onError(LibsMsg.SKIN_API_TIMEOUT_ERROR); | ||||
|                     return null; | ||||
|                 } | ||||
|             } catch (IOException e) { | ||||
|             } catch (IOException ignored) { | ||||
|             } | ||||
|  | ||||
|             if (DisguiseUtilities.getLogger() != null) { | ||||
|                 DisguiseUtilities.getLogger().warning("Failed to access MineSkin.org"); | ||||
|             } | ||||
|  | ||||
|             ex.printStackTrace(); | ||||
|  | ||||
|             callback.onError(LibsMsg.SKIN_API_FAIL); | ||||
| @@ -231,7 +267,9 @@ public class MineSkinAPI { | ||||
|                 throw new IllegalArgumentException(); | ||||
|             } | ||||
|  | ||||
|             if (DisguiseUtilities.getLogger() != null) { | ||||
|                 DisguiseUtilities.getLogger().warning("Failed to access MineSkin.org"); | ||||
|             } | ||||
|             ex.printStackTrace(); | ||||
|         } finally { | ||||
|             nextRequest = System.currentTimeMillis() + nextRequestIn + 1000; | ||||
|   | ||||
| @@ -337,6 +337,8 @@ public enum LibsMsg { | ||||
|             " release build update and 'update!' will download that update!"), | ||||
|     LD_COMMAND_CHANGELOG(ChatColor.BLUE + "/libsdisguises changelog - " + ChatColor.AQUA + | ||||
|             "Gives you the changelog of the current update fetched"), | ||||
|     LD_DEBUG_MINESKIN(ChatColor.BLUE + "/libsdisguises mineskin - " + ChatColor.AQUA + "Prints debug information about MineSkin to console"), | ||||
|     LD_DEBUG_MINESKIN_TOGGLE(ChatColor.BLUE + "MineSkin debug is now %s, this command toggles the printing of MineSkin information to console"), | ||||
|     LD_COMMAND_JSON(ChatColor.BLUE + "/libsdisguises json - " + ChatColor.AQUA + | ||||
|             "Turns the current held item into a string format"), | ||||
|     LD_COMMAND_MODS(ChatColor.BLUE + "/libsdisguises mods <Player?> - " + ChatColor.AQUA + | ||||
|   | ||||
		Reference in New Issue
	
	Block a user