From ebe9792ad47d1468e83c87cb61b00ee3e2c7fef9 Mon Sep 17 00:00:00 2001 From: Tobias de Bruijn Date: Thu, 19 Jan 2023 13:31:17 +0100 Subject: [PATCH] v1.7.8 --- build.gradle | 2 +- gradle.properties | 2 +- plugininfo.json | 6 +- .../array21/skinfixer/SkinChangeHandler.java | 6 +- .../java/dev/array21/skinfixer/SkinFixer.java | 15 ++- .../skinfixer/apis/RemotePluginInfo.java | 27 ++++ .../array21/skinfixer/apis/SkinFixerApi.java | 115 +++++++++++------- .../apis/gson/RemoteInfoManifest.java | 12 ++ .../array21/skinfixer/common/AddNewSkin.java | 2 +- .../skinfixer/config/ConfigHandler.java | 6 +- .../skinfixer/config/ConfigManifest.java | 2 + .../events/PlayerJoinEventListener.java | 2 +- 12 files changed, 138 insertions(+), 59 deletions(-) create mode 100644 src/main/java/dev/array21/skinfixer/apis/RemotePluginInfo.java create mode 100644 src/main/java/dev/array21/skinfixer/apis/gson/RemoteInfoManifest.java diff --git a/build.gradle b/build.gradle index 144feae..c7138b8 100644 --- a/build.gradle +++ b/build.gradle @@ -37,7 +37,7 @@ dependencies { implementation 'dev.array21:httplib:1.2.2' implementation 'dev.array21:classvalidator:1.0.0' - implementation 'dev.array21:bukkit-reflection-util:1.2.1' + implementation 'dev.array21:bukkit-reflection-util:1.3.0' // JDA and related dependencies implementation 'org.slf4j:slf4j-log4j12:1.7.29' diff --git a/gradle.properties b/gradle.properties index 665083b..3a032aa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -pluginVersion = 1.7.7 +pluginVersion = 1.7.8 diff --git a/plugininfo.json b/plugininfo.json index 0eb4b57..676ad3a 100644 --- a/plugininfo.json +++ b/plugininfo.json @@ -2,9 +2,5 @@ "servers": [ "https://skinfixer.k8s.array21.dev" ], - "messages": [ - { - "text": "Hi Mom!" - } - ] + "messages": [] } \ No newline at end of file diff --git a/src/main/java/dev/array21/skinfixer/SkinChangeHandler.java b/src/main/java/dev/array21/skinfixer/SkinChangeHandler.java index 54c6793..f52f8b9 100644 --- a/src/main/java/dev/array21/skinfixer/SkinChangeHandler.java +++ b/src/main/java/dev/array21/skinfixer/SkinChangeHandler.java @@ -40,9 +40,9 @@ public void run() { //Fetch the skin from Mineskin.org's API Triple apiResponse; if(isPremium) { - apiResponse = new SkinFixerApi().getSkinOfPremiumPlayer(externalUuid.toString()); + apiResponse = new SkinFixerApi(SkinChangeHandler.this.plugin.remoteInfoManifest).getSkinOfPremiumPlayer(externalUuid.toString()); } else { - apiResponse = new SkinFixerApi().getSkin(skinUrl, slim); + apiResponse = new SkinFixerApi(SkinChangeHandler.this.plugin.remoteInfoManifest).getSkin(skinUrl, slim); } if(!apiResponse.getA()) { @@ -65,7 +65,7 @@ public void changeSkinFromUuid(UUID mojangUuid, UUID localPlayerUuid, boolean sl @Override public void run() { Player p = Bukkit.getPlayer(localPlayerUuid); - Triple apiResponse = new SkinFixerApi().getSkinOfPremiumPlayer(mojangUuid.toString()); + Triple apiResponse = new SkinFixerApi(SkinChangeHandler.this.plugin.remoteInfoManifest).getSkinOfPremiumPlayer(mojangUuid.toString()); if(!apiResponse.getA()) { p.sendMessage(ChatColor.RED + LangHandler.model.skinApplyFailed.replaceAll("%ERROR%", ChatColor.GRAY + apiResponse.getC() + ChatColor.RED)); return; diff --git a/src/main/java/dev/array21/skinfixer/SkinFixer.java b/src/main/java/dev/array21/skinfixer/SkinFixer.java index 0808d32..856493c 100644 --- a/src/main/java/dev/array21/skinfixer/SkinFixer.java +++ b/src/main/java/dev/array21/skinfixer/SkinFixer.java @@ -3,7 +3,8 @@ import java.util.HashMap; import java.util.logging.Logger; -import org.apache.log4j.LogManager; +import dev.array21.skinfixer.apis.RemotePluginInfo; +import dev.array21.skinfixer.apis.gson.RemoteInfoManifest; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; @@ -27,6 +28,8 @@ public class SkinFixer extends JavaPlugin { private ConfigHandler configHandler; private JdaHandler jdaHandler; private LibWrapper libWrapper; + + public RemoteInfoManifest remoteInfoManifest; @Override public void onEnable() { @@ -40,6 +43,8 @@ public void onEnable() { this.configHandler = new ConfigHandler(this); ConfigManifest configManifest = configHandler.read(); + this.remoteInfoManifest = RemotePluginInfo.getManifest(this); + this.libWrapper = new LibWrapper(this); this.libWrapper.init(); @@ -69,14 +74,18 @@ public void onEnable() { //Register event listeners Bukkit.getPluginManager().registerEvents(new PlayerJoinEventListener(this), this); + + for(RemoteInfoManifest.Message remoteMessage : this.remoteInfoManifest.messages) { + logInfo(remoteMessage.text); + } } public static void logInfo(Object log) { - LOGGER.info("[" + SkinFixer.INSTANCE.getDescription().getName() + "] " + log.toString()); + LOGGER.info(log.toString()); } public static void logWarn(Object log) { - LOGGER.warning("[" + SkinFixer.INSTANCE.getDescription().getName() + "] " + log.toString()); + LOGGER.warning(log.toString()); } /** diff --git a/src/main/java/dev/array21/skinfixer/apis/RemotePluginInfo.java b/src/main/java/dev/array21/skinfixer/apis/RemotePluginInfo.java new file mode 100644 index 0000000..f2fd553 --- /dev/null +++ b/src/main/java/dev/array21/skinfixer/apis/RemotePluginInfo.java @@ -0,0 +1,27 @@ +package dev.array21.skinfixer.apis; + +import com.google.gson.Gson; +import dev.array21.httplib.Http; +import dev.array21.skinfixer.SkinFixer; +import dev.array21.skinfixer.apis.gson.RemoteInfoManifest; +import dev.array21.skinfixer.util.Utils; +import org.bukkit.Bukkit; + +import java.io.IOException; + +public class RemotePluginInfo { + + public static RemoteInfoManifest getManifest(SkinFixer plugin) { + Http.ResponseObject response; + try { + response = new Http().makeRequest(Http.RequestMethod.GET, plugin.getConfigManifest().remotePluginInfoUrl, null, null, null, null); + } catch (IOException e) { + SkinFixer.logWarn("Failed to retrieve remote plugin info. This is fatal"); + SkinFixer.logWarn(Utils.getStackTrace(e)); + Bukkit.getPluginManager().disablePlugin(plugin); + return null; + } + + return new Gson().fromJson(response.getMessage(), RemoteInfoManifest.class); + } +} diff --git a/src/main/java/dev/array21/skinfixer/apis/SkinFixerApi.java b/src/main/java/dev/array21/skinfixer/apis/SkinFixerApi.java index 5319a23..46fd344 100644 --- a/src/main/java/dev/array21/skinfixer/apis/SkinFixerApi.java +++ b/src/main/java/dev/array21/skinfixer/apis/SkinFixerApi.java @@ -9,72 +9,101 @@ import dev.array21.skinfixer.SkinFixer; import dev.array21.skinfixer.apis.gson.GetSkinResponse; import dev.array21.skinfixer.apis.gson.GetUuidResponse; +import dev.array21.skinfixer.apis.gson.RemoteInfoManifest; import dev.array21.skinfixer.util.Triple; import dev.array21.skinfixer.util.Utils; import dev.array21.httplib.Http; import dev.array21.httplib.Http.RequestMethod; import dev.array21.httplib.Http.ResponseObject; +import org.jetbrains.annotations.NotNull; public class SkinFixerApi { - + + final String[] servers; + + public SkinFixerApi(RemoteInfoManifest infoManifest) { + this.servers = infoManifest.servers; + } + public Triple getUuid(String playerName) { - ResponseObject apiResponse; - try { - apiResponse = new Http().makeRequest(RequestMethod.GET, "https://skinfixer.k8s.array21.dev/player/" + playerName, null, null, null, null); - } catch(IOException e) { - SkinFixer.logWarn("An IOException occured while fetching a UUID from the SkinFixer API"); - SkinFixer.logWarn(Utils.getStackTrace(e)); - return new Triple(false, null, Utils.getStackTrace(e)); + ResponseObject apiResponse = null; + IOException exception = null; + for(String server : this.servers) { + try { + apiResponse = new Http().makeRequest( + RequestMethod.GET, + String.format("%s/player/%s", server, playerName), + null, + null, + null, + null + ); + } catch(IOException e) { + exception = e; + SkinFixer.logWarn(String.format("An IOException occured while fetching a UUID from the SkinFixer API (%s). Trying other servers (if there are any)", server)); + } } - - if(apiResponse.getResponseCode() != 200) { - SkinFixer.logWarn("The SkinFixer API returned an unexpected result: " + apiResponse.getConnectionMessage()); - return new Triple(false, null, apiResponse.getConnectionMessage()); + + Triple responseTriple = this.checkResponseAndParse(apiResponse, exception, GetUuidResponse.class); + + if(responseTriple.getB() != null) { + return new Triple<>(responseTriple.getA(), responseTriple.getB().uuid, responseTriple.getC()); + } else { + return new Triple<>(responseTriple.getA(), null, responseTriple.getC()); } - - String uuid = new Gson().fromJson(apiResponse.getMessage(), GetUuidResponse.class).uuid; - return new Triple(true, uuid, null); } public Triple getSkin(String skinUrl, boolean slim) { String skinUrlBase64 = Base64.getEncoder().encodeToString(skinUrl.getBytes()); - - ResponseObject apiResponse; - try { - apiResponse = new Http().makeRequest(RequestMethod.GET, "https://skinfixer.k8s.array21.dev/generate/url/" + skinUrlBase64, new HashMap<>(), null, null, null); - } catch(IOException e) { - SkinFixer.logWarn("An IOException occured while fetching a skin from the SkinFixer API"); - SkinFixer.logWarn(Utils.getStackTrace(e)); - return new Triple(false, null, Utils.getStackTrace(e)); + + ResponseObject apiResponse = null; + IOException exception = null; + for(String server : this.servers) { + try { + apiResponse = new Http().makeRequest(RequestMethod.GET, String.format("%s/generate/url/%s", server, skinUrlBase64), new HashMap<>(), null, null, null); + break; + } catch(IOException e) { + exception = e; + SkinFixer.logWarn(String.format("An IOException occured while fetching a skin from the SkinFixer API (%s). Trying other servers (if there are any)", server)); + } } - + + return checkResponseAndParse(apiResponse, exception, GetSkinResponse.class); + } + + @NotNull + private Triple checkResponseAndParse(ResponseObject apiResponse, IOException exception, Class parseToClass) { + if(apiResponse == null) { + assert exception != null; + + SkinFixer.logWarn(Utils.getStackTrace(exception)); + return new Triple<>(false, null, Utils.getStackTrace(exception)); + } + if(apiResponse.getResponseCode() != 200) { SkinFixer.logWarn("The SkinFixer API returned an unexpected result: " + apiResponse.getConnectionMessage()); - return new Triple(false, null, apiResponse.getConnectionMessage()); + return new Triple<>(false, null, apiResponse.getConnectionMessage()); } - + final Gson gson = new Gson(); - return new Triple(true, gson.fromJson(apiResponse.getMessage(), GetSkinResponse.class), null); + return new Triple<>(true, gson.fromJson(apiResponse.getMessage(), parseToClass), null); } - + public Triple getSkinOfPremiumPlayer(String uuid) { String uuidBase64 = Base64.getEncoder().encodeToString(uuid.getBytes()); - ResponseObject apiResponse; - try { - apiResponse = new Http().makeRequest(RequestMethod.GET, "https://skinfixer.k8s.array21.dev/generate/uuid/" + uuidBase64, new HashMap<>(), null, null, null); - } catch(IOException e) { - SkinFixer.logWarn("An IOException occured while fetching a skin from the SkinFixer API."); - SkinFixer.logWarn(Utils.getStackTrace(e)); - return new Triple(false, null, Utils.getStackTrace(e)); + ResponseObject apiResponse = null; + IOException exception = null; + for(String server : servers) { + try { + apiResponse = new Http().makeRequest(RequestMethod.GET, String.format("%s/generate/uuid/%s", server, uuidBase64), new HashMap<>(), null, null, null); + break; + } catch(IOException e) { + exception = e; + SkinFixer.logWarn(String.format("An IOException occured while fetching a skin from the SkinFixer API (%s). Trying other servers (if there are any)", server)); + } } - - if(apiResponse.getResponseCode() != 200) { - SkinFixer.logWarn("The SkinFixer API returned an unexpected result: " + apiResponse.getConnectionMessage()); - return new Triple(false, null, apiResponse.getConnectionMessage()); - } - - final Gson gson = new Gson(); - return new Triple(true, gson.fromJson(apiResponse.getMessage(), GetSkinResponse.class), null); + + return checkResponseAndParse(apiResponse, exception, GetSkinResponse.class); } } diff --git a/src/main/java/dev/array21/skinfixer/apis/gson/RemoteInfoManifest.java b/src/main/java/dev/array21/skinfixer/apis/gson/RemoteInfoManifest.java new file mode 100644 index 0000000..e60ac05 --- /dev/null +++ b/src/main/java/dev/array21/skinfixer/apis/gson/RemoteInfoManifest.java @@ -0,0 +1,12 @@ +package dev.array21.skinfixer.apis.gson; + +public class RemoteInfoManifest { + + public String[] servers; + + public Message[] messages; + + public static class Message { + public String text; + } +} diff --git a/src/main/java/dev/array21/skinfixer/common/AddNewSkin.java b/src/main/java/dev/array21/skinfixer/common/AddNewSkin.java index 0fd7c3c..c3924a4 100644 --- a/src/main/java/dev/array21/skinfixer/common/AddNewSkin.java +++ b/src/main/java/dev/array21/skinfixer/common/AddNewSkin.java @@ -25,7 +25,7 @@ public Pair addByNickname(String url) { code = generateRandomInt(); } - Triple apiResponse = new SkinFixerApi().getUuid(url); + Triple apiResponse = new SkinFixerApi(this.plugin.remoteInfoManifest).getUuid(url); if(!apiResponse.getA()) { return new Pair(null, apiResponse.getC()); } diff --git a/src/main/java/dev/array21/skinfixer/config/ConfigHandler.java b/src/main/java/dev/array21/skinfixer/config/ConfigHandler.java index de2ee7a..413e1c9 100644 --- a/src/main/java/dev/array21/skinfixer/config/ConfigHandler.java +++ b/src/main/java/dev/array21/skinfixer/config/ConfigHandler.java @@ -62,6 +62,10 @@ public ConfigManifest read() { if(manifest.applySkinOnJoin == null) { manifest.applySkinOnJoin = true; } + + if(manifest.remotePluginInfoUrl == null) { + manifest.remotePluginInfoUrl = "https://raw.githubusercontent.com/TobiasDeBruijn/SkinFixer/master/plugininfo.json"; + } if(manifest.useDiscord) { if(manifest.discordSettings == null) { @@ -90,5 +94,5 @@ public ConfigManifest read() { public ConfigManifest getConfigManifest() { return this.manifest; } - + } diff --git a/src/main/java/dev/array21/skinfixer/config/ConfigManifest.java b/src/main/java/dev/array21/skinfixer/config/ConfigManifest.java index f0764b7..3ae4bca 100644 --- a/src/main/java/dev/array21/skinfixer/config/ConfigManifest.java +++ b/src/main/java/dev/array21/skinfixer/config/ConfigManifest.java @@ -27,4 +27,6 @@ public class ConfigManifest { public Boolean disableSkinApplyOnLoginMessage; public Boolean applySkinOnJoin; + + public String remotePluginInfoUrl; } diff --git a/src/main/java/dev/array21/skinfixer/events/PlayerJoinEventListener.java b/src/main/java/dev/array21/skinfixer/events/PlayerJoinEventListener.java index 8d09baa..0e4ae99 100644 --- a/src/main/java/dev/array21/skinfixer/events/PlayerJoinEventListener.java +++ b/src/main/java/dev/array21/skinfixer/events/PlayerJoinEventListener.java @@ -45,7 +45,7 @@ public void run() { return; } - Triple mojangApiResponse = new SkinFixerApi().getUuid(event.getPlayer().getName()); + Triple mojangApiResponse = new SkinFixerApi(PlayerJoinEventListener.this.plugin.remoteInfoManifest).getUuid(event.getPlayer().getName()); if(!mojangApiResponse.getA()) { SkinFixer.logWarn("Something went wrong fetching the UUID from Mojang."); } else if(mojangApiResponse.getB() != null) {