From be380460520f591e8e7c1dd37c0a6c630358968a Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sat, 22 May 2021 01:50:20 +1200 Subject: [PATCH] Add ability to enable mineskin debug --- .../commands/LibsDisguisesCommand.java | 1 + .../libsdisguises/LDDebugMineSkin.java | 42 ++++++++++++++++ .../utilities/mineskin/MineSkinAPI.java | 48 +++++++++++++++++-- .../utilities/translations/LibsMsg.java | 2 + 4 files changed, 88 insertions(+), 5 deletions(-) create mode 100644 src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDDebugMineSkin.java diff --git a/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java b/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java index e31bdcb6..25d2bc3a 100644 --- a/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java +++ b/src/main/java/me/libraryaddict/disguise/commands/LibsDisguisesCommand.java @@ -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 filterTabs(ArrayList list, String[] origArgs) { diff --git a/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDDebugMineSkin.java b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDDebugMineSkin.java new file mode 100644 index 00000000..3c82f187 --- /dev/null +++ b/src/main/java/me/libraryaddict/disguise/commands/libsdisguises/LDDebugMineSkin.java @@ -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 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; + } +} 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 f2206876..a6788732 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/mineskin/MineSkinAPI.java @@ -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"); } - 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(); } - DisguiseUtilities.getLogger().warning("Failed to access MineSkin.org"); + if (DisguiseUtilities.getLogger() != null) { + DisguiseUtilities.getLogger().warning("Failed to access MineSkin.org"); + } ex.printStackTrace(); } finally { nextRequest = System.currentTimeMillis() + nextRequestIn + 1000; diff --git a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java index 9fc1b42e..794c74ae 100644 --- a/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java +++ b/src/main/java/me/libraryaddict/disguise/utilities/translations/LibsMsg.java @@ -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 - " + ChatColor.AQUA +