Add ability to enable mineskin debug

This commit is contained in:
libraryaddict 2021-05-22 01:50:20 +12:00
parent e91c022d92
commit be38046052
4 changed files with 88 additions and 5 deletions

View File

@ -35,6 +35,7 @@ public class LibsDisguisesCommand implements CommandExecutor, TabCompleter {
getCommands().add(new LDDebugPlayer()); getCommands().add(new LDDebugPlayer());
getCommands().add(new LDUploadLogs()); getCommands().add(new LDUploadLogs());
getCommands().add(new LDUpdateProtocolLib()); getCommands().add(new LDUpdateProtocolLib());
getCommands().add(new LDDebugMineSkin());
} }
protected ArrayList<String> filterTabs(ArrayList<String> list, String[] origArgs) { protected ArrayList<String> filterTabs(ArrayList<String> list, String[] origArgs) {

View File

@ -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;
}
}

View File

@ -1,6 +1,8 @@
package me.libraryaddict.disguise.utilities.mineskin; package me.libraryaddict.disguise.utilities.mineskin;
import com.google.gson.Gson; import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import me.libraryaddict.disguise.utilities.SkinUtils; import me.libraryaddict.disguise.utilities.SkinUtils;
import me.libraryaddict.disguise.utilities.translations.LibsMsg; import me.libraryaddict.disguise.utilities.translations.LibsMsg;
@ -32,6 +34,9 @@ public class MineSkinAPI {
*/ */
private long nextRequest; private long nextRequest;
private final ReentrantLock lock = new ReentrantLock(); private final ReentrantLock lock = new ReentrantLock();
@Getter
@Setter
private boolean debugging;
public boolean isInUse() { public boolean isInUse() {
return lock.isLocked(); return lock.isLocked();
@ -56,12 +61,28 @@ public class MineSkinAPI {
return doPost(callback, "/generate/url", url, null, modelType); 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) { private MineSkinResponse doPost(SkinUtils.SkinCallback callback, String path, String skinUrl, File file, SkinUtils.ModelType modelType) {
lock.lock(); lock.lock();
long sleep = nextRequest - System.currentTimeMillis(); 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) { if (sleep > 0) {
printDebug("Sleeping for " + sleep + "ms before calling the API due to a recent request");
try { try {
Thread.sleep(sleep); Thread.sleep(sleep);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -121,9 +142,15 @@ public class MineSkinAPI {
writer.append("--").append(boundary).append("--").append(CRLF).flush(); writer.append("--").append(boundary).append("--").append(CRLF).flush();
} }
printDebug("Received status code: " + connection.getResponseCode());
if (connection.getResponseCode() == 500) { if (connection.getResponseCode() == 500) {
APIError error = new Gson().fromJson(new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)).lines() String errorMessage = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8)).lines()
.collect(Collectors.joining("\n")), APIError.class); .collect(Collectors.joining("\n"));
APIError error = new Gson().fromJson(errorMessage, APIError.class);
printDebug("Received error: " + errorMessage);
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());
@ -156,6 +183,8 @@ public class MineSkinAPI {
// Read it to string // 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"));
printDebug("Received: " + response);
MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class); MineSkinResponse skinResponse = new Gson().fromJson(response, MineSkinResponse.class);
nextRequestIn = (long) (skinResponse.getNextRequest() * 1000); nextRequestIn = (long) (skinResponse.getNextRequest() * 1000);
@ -163,6 +192,10 @@ public class MineSkinAPI {
return skinResponse; return skinResponse;
} }
} catch (SocketTimeoutException ex) { } catch (SocketTimeoutException ex) {
if (isDebugging()) {
ex.printStackTrace();
}
callback.onError(skinUrl == null ? LibsMsg.SKIN_API_TIMEOUT_ERROR : LibsMsg.SKIN_API_IMAGE_TIMEOUT); callback.onError(skinUrl == null ? LibsMsg.SKIN_API_TIMEOUT_ERROR : LibsMsg.SKIN_API_IMAGE_TIMEOUT);
return null; return null;
} catch (Exception ex) { } catch (Exception ex) {
@ -172,10 +205,13 @@ public class MineSkinAPI {
callback.onError(LibsMsg.SKIN_API_TIMEOUT_ERROR); callback.onError(LibsMsg.SKIN_API_TIMEOUT_ERROR);
return null; 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(); ex.printStackTrace();
callback.onError(LibsMsg.SKIN_API_FAIL); callback.onError(LibsMsg.SKIN_API_FAIL);
@ -231,7 +267,9 @@ public class MineSkinAPI {
throw new IllegalArgumentException(); 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(); ex.printStackTrace();
} finally { } finally {
nextRequest = System.currentTimeMillis() + nextRequestIn + 1000; nextRequest = System.currentTimeMillis() + nextRequestIn + 1000;

View File

@ -337,6 +337,8 @@ public enum LibsMsg {
" release build update and 'update!' will download that update!"), " release build update and 'update!' will download that update!"),
LD_COMMAND_CHANGELOG(ChatColor.BLUE + "/libsdisguises changelog - " + ChatColor.AQUA + LD_COMMAND_CHANGELOG(ChatColor.BLUE + "/libsdisguises changelog - " + ChatColor.AQUA +
"Gives you the changelog of the current update fetched"), "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 + LD_COMMAND_JSON(ChatColor.BLUE + "/libsdisguises json - " + ChatColor.AQUA +
"Turns the current held item into a string format"), "Turns the current held item into a string format"),
LD_COMMAND_MODS(ChatColor.BLUE + "/libsdisguises mods <Player?> - " + ChatColor.AQUA + LD_COMMAND_MODS(ChatColor.BLUE + "/libsdisguises mods <Player?> - " + ChatColor.AQUA +