From 3a672378ffe749d2b370044bdc9fe016d0a3db50 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Sat, 3 Dec 2022 20:23:21 +0100 Subject: [PATCH 01/10] Add Update Checker --- .../bungeecord/BungeeCordCore.java | 1 + core/pom.xml | 10 ++ .../core/AdvancedServerList.java | 32 +++- .../core/check/UpdateChecker.java | 139 ++++++++++++++++++ .../advancedserverlist/paper/PaperCore.java | 1 + .../advancedserverlist/spigot/SpigotCore.java | 1 + .../velocity/VelocityCore.java | 1 + 7 files changed, 184 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java diff --git a/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java b/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java index cc14673f..06970507 100644 --- a/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java +++ b/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java @@ -55,6 +55,7 @@ public class BungeeCordCore extends Plugin implements ProxyCore3.1.2 compile + + com.google.code.gson + gson + 2.9.1 + + + com.squareup.okhttp3 + okhttp + 4.10.0 + diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java index 7aa576da..d97b2876 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java @@ -25,6 +25,7 @@ package ch.andre601.advancedserverlist.core; +import ch.andre601.advancedserverlist.core.check.UpdateChecker; import ch.andre601.advancedserverlist.core.commands.CommandHandler; import ch.andre601.advancedserverlist.core.file.FileHandler; import ch.andre601.advancedserverlist.core.interfaces.core.PluginCore; @@ -41,6 +42,7 @@ public class AdvancedServerList{ private final FileHandler fileHandler; private final CommandHandler commandHandler; private final PlayerHandler playerHandler; + private final UpdateChecker updateChecker; private String version; @@ -49,6 +51,7 @@ public AdvancedServerList(PluginCore plugin){ this.fileHandler = new FileHandler(this); this.commandHandler = new CommandHandler(this); this.playerHandler = new PlayerHandler(this); + this.updateChecker = new UpdateChecker(this); load(); } @@ -83,12 +86,27 @@ public void clearFaviconCache(){ plugin.clearFaviconCache(); } + public void checkForUpdates(String platform){ + updateChecker.checkUpdate(platform).whenComplete((version, throwable) -> { + if(version == null || throwable != null){ + getPlugin().getPluginLogger().warn("Update check failed! See previous messages for explanations and causes."); + return; + } + + if(version.getVersionNumber().equals(getVersion())){ + getPlugin().getPluginLogger().info("No new update detected! You're running the latest version."); + return; + } + + printUpdateBanner(version.getVersionNumber(), version.getId()); + }); + } + private void load(){ printBanner(); resolveVersion(); getPlugin().getPluginLogger().info("Starting AdvancedServerList v%s...", version); - getPlugin().getPluginLogger().info("Platform: " + plugin.getPlatformName() + " " + plugin.getPlatformVersion()); if(getFileHandler().loadConfig()){ @@ -138,6 +156,18 @@ private void printBanner(){ getPlugin().getPluginLogger().info(""); } + private void printUpdateBanner(String version, String versionId){ + getPlugin().getPluginLogger().info("=================================================================="); + getPlugin().getPluginLogger().info("You are running an outdated version of AdvancedServerList!"); + getPlugin().getPluginLogger().info(""); + getPlugin().getPluginLogger().info("Your version: %s", getVersion()); + getPlugin().getPluginLogger().info("Modrinth version: %s", version); + getPlugin().getPluginLogger().info(""); + getPlugin().getPluginLogger().info("You can download the latest release from here:"); + getPlugin().getPluginLogger().info("https://modrinth.com/plugin/advancedserverlist/version/%s", versionId); + getPlugin().getPluginLogger().info("=================================================================="); + } + private void resolveVersion(){ try(InputStream is = getClass().getResourceAsStream("/version.properties")){ Properties properties = new Properties(); diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java new file mode 100644 index 00000000..5368077b --- /dev/null +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java @@ -0,0 +1,139 @@ +/* + * MIT License + * + * Copyright (c) 2022 Andre_601 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package ch.andre601.advancedserverlist.core.check; + +import ch.andre601.advancedserverlist.core.AdvancedServerList; +import ch.andre601.advancedserverlist.core.interfaces.PluginLogger; +import com.google.gson.FieldNamingPolicy; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonSyntaxException; +import io.leangen.geantyref.TypeToken; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.ResponseBody; + +import java.io.IOException; +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class UpdateChecker{ + + private final String url = "https://api.modrinth.com/v2/project/advancedserverlist/version?loaders=[\"%s\"]"; + private final OkHttpClient client = new OkHttpClient(); + + private final AdvancedServerList core; + + private final Type listType = new TypeToken>(){}.getType(); + private final Gson gson = new GsonBuilder() + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + .setPrettyPrinting() + .setLenient() + .create(); + + public UpdateChecker(AdvancedServerList core){ + this.core = core; + } + + public CompletableFuture checkUpdate(String loader){ + return CompletableFuture.supplyAsync(() -> { + PluginLogger logger = core.getPlugin().getPluginLogger(); + if(core.getVersion().equals("UNKNOWN")){ + logger.warn("Cannot perform Update check! Plugin version couldn't be parsed."); + return null; + } + + Request request = new Request.Builder() + .url(String.format(url, loader)) + .header("User-Agent", "AdvancedServerList/" + core.getVersion()) + .build(); + + try(Response response = client.newCall(request).execute()){ + if(!response.isSuccessful()){ + logger.warn("Encountered a non-successful response from Modrinth! Code: %d", response.code()); + return null; + } + + ResponseBody body = response.body(); + if(body == null){ + logger.warn("Received an empty Response body from Modrinth!"); + return null; + } + + String responseString = response.toString(); + if(responseString.isEmpty()){ + logger.warn("Received an empty Response body from Modrinth!"); + return null; + } + + List list = gson.fromJson(responseString, listType); + if(list == null || list.isEmpty()){ + logger.warn("Couldn't convert JSON Array into a valid list."); + return null; + } + + ModrinthVersion version = list.get(0); + if(version.getVersionNumber() == null || version.getVersionNumber().isEmpty()){ + logger.warn("Cannot check latest version. Received version number was null/empty."); + return null; + } + + return version; + }catch(JsonSyntaxException ex){ + logger.warn("Encountered invalid JSON from Response body!", ex); + return null; + }catch(IOException ex){ + logger.warn("Encountered Exception while checking for an update!", ex); + return null; + } + }, r -> { + Thread t = new Thread(r, "AdvancedServerList-UpdateCheck-Thread"); + t.setDaemon(true); + t.start(); + }); + } + + public static class ModrinthVersion{ + private String id; + private String versionNumber; + + public ModrinthVersion(String id, String versionNumber){ + this.id = id; + this.versionNumber = versionNumber; + } + + public String getId(){ + return id; + } + + public String getVersionNumber(){ + return versionNumber; + } + } +} diff --git a/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java b/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java index 54da9834..c7c60749 100644 --- a/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java +++ b/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java @@ -158,6 +158,7 @@ public String getPlatformVersion(){ private void enable(){ this.core = new AdvancedServerList(this); + core.checkForUpdates("paper"); } @Override diff --git a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java index f9c9d4fc..5bf6eb64 100644 --- a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java +++ b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java @@ -71,6 +71,7 @@ public void onEnable(){ } this.core = new AdvancedServerList(this); + core.checkForUpdates("spigot"); } @Override diff --git a/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java b/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java index b5eaef81..c5812cd8 100644 --- a/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java +++ b/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java @@ -75,6 +75,7 @@ public VelocityCore(ProxyServer proxy, @DataDirectory Path path, Metrics.Factory @Subscribe public void init(ProxyInitializeEvent event){ this.core = new AdvancedServerList(this); + core.checkForUpdates("velocity"); } @Subscribe From 48edebc043592bf4516ec3992658bb8f6820d164 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Sat, 3 Dec 2022 20:29:45 +0100 Subject: [PATCH 02/10] Add config option --- .../advancedserverlist/core/AdvancedServerList.java | 6 ++++++ core/src/main/resources/config.yml | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java index d97b2876..b3d51d2e 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java @@ -87,6 +87,12 @@ public void clearFaviconCache(){ } public void checkForUpdates(String platform){ + getPlugin().getPluginLogger().info("Checking for updates. Please wait..."); + if(!getFileHandler().getBoolean("check_updates")){ + getPlugin().getPluginLogger().info("'check_updates' set to false. Skipping update checks."); + return; + } + updateChecker.checkUpdate(platform).whenComplete((version, throwable) -> { if(version == null || throwable != null){ getPlugin().getPluginLogger().warn("Update check failed! See previous messages for explanations and causes."); diff --git a/core/src/main/resources/config.yml b/core/src/main/resources/config.yml index 16d9180b..27a39ce9 100644 --- a/core/src/main/resources/config.yml +++ b/core/src/main/resources/config.yml @@ -9,4 +9,12 @@ unknown_player: "Anonymous" # When set to true will AdvancedServerList not load any players from the cache.data file (if present) nor save any to it. # Placeholders that rely on the player being cached (such as ${player isWhitelisted}) will not work while this option is enabled. # -disable_cache: false \ No newline at end of file +disable_cache: false + +# +# Should AdvancedServerList check for new versions? +# +# When set to true will AdvancedServerList connect to modrinth.com to check for any new versions for the platform it is used +# on. This check only happens when the plugin enables. +# +check_updates: true \ No newline at end of file From 39c1fb545287bbfab1a7daa23a5a2abeb2d9f384 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Mon, 5 Dec 2022 21:26:24 +0100 Subject: [PATCH 03/10] Better version comparison. --- bungeecord/pom.xml | 4 +++ .../core/AdvancedServerList.java | 23 ++++++++++------ .../core/check/UpdateChecker.java | 26 +++++++++++++++++-- paper/src/main/resources/plugin.yml | 3 +++ pom.xml | 2 +- spigot/src/main/resources/plugin.yml | 1 + velocity/pom.xml | 4 +++ 7 files changed, 52 insertions(+), 11 deletions(-) diff --git a/bungeecord/pom.xml b/bungeecord/pom.xml index 554fc1a5..1e196fef 100644 --- a/bungeecord/pom.xml +++ b/bungeecord/pom.xml @@ -123,6 +123,10 @@ com.github.benmanes.caffeine ch.andre601.advancedserverlist.bungeecord.depends.caffeine + + okhttp3 + ch.andre601.advancedserverlist.bungeecord.depends.okhttp3 + diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java index b3d51d2e..29667994 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java @@ -28,6 +28,7 @@ import ch.andre601.advancedserverlist.core.check.UpdateChecker; import ch.andre601.advancedserverlist.core.commands.CommandHandler; import ch.andre601.advancedserverlist.core.file.FileHandler; +import ch.andre601.advancedserverlist.core.interfaces.PluginLogger; import ch.andre601.advancedserverlist.core.interfaces.core.PluginCore; import ch.andre601.advancedserverlist.core.profiles.players.PlayerHandler; @@ -87,24 +88,30 @@ public void clearFaviconCache(){ } public void checkForUpdates(String platform){ - getPlugin().getPluginLogger().info("Checking for updates. Please wait..."); + PluginLogger logger = plugin.getPluginLogger(); + + logger.info("Checking for updates. Please wait..."); if(!getFileHandler().getBoolean("check_updates")){ - getPlugin().getPluginLogger().info("'check_updates' set to false. Skipping update checks."); + logger.info("'check_updates' set to false. Skipping update checks."); return; } updateChecker.checkUpdate(platform).whenComplete((version, throwable) -> { if(version == null || throwable != null){ - getPlugin().getPluginLogger().warn("Update check failed! See previous messages for explanations and causes."); + logger.warn("Update check failed! See previous messages for explanations and causes."); return; } - if(version.getVersionNumber().equals(getVersion())){ - getPlugin().getPluginLogger().info("No new update detected! You're running the latest version."); - return; + int result = version.compare(getVersion()); + switch(result){ + case -2 -> { + logger.warn("Encountered an exception while comparing versions. Are they valid?"); + logger.warn("Own version: %s; New version: %s", getVersion(), version.getVersionNumber()); + } + case -1 -> logger.info("You seem to run a newer version compared to Modrinth. Are you running a dev build?"); + case 0 -> logger.info("No new update found. You're running the latest version!"); + case 1 -> printUpdateBanner(version.getVersionNumber(), version.getId()); } - - printUpdateBanner(version.getVersionNumber(), version.getId()); }); } diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java index 5368077b..211cab8a 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java @@ -71,7 +71,7 @@ public CompletableFuture checkUpdate(String loader){ Request request = new Request.Builder() .url(String.format(url, loader)) - .header("User-Agent", "AdvancedServerList/" + core.getVersion()) + .header("User-Agent", "AdvancedServerList-" + loader + "/" + core.getVersion()) .build(); try(Response response = client.newCall(request).execute()){ @@ -86,7 +86,7 @@ public CompletableFuture checkUpdate(String loader){ return null; } - String responseString = response.toString(); + String responseString = body.string(); if(responseString.isEmpty()){ logger.warn("Received an empty Response body from Modrinth!"); return null; @@ -135,5 +135,27 @@ public String getId(){ public String getVersionNumber(){ return versionNumber; } + + public int compare(String version){ + String[] oldParts = version.split("\\."); + String[] newParts = versionNumber.split("\\."); + int length = Math.max(oldParts.length, newParts.length); + + for(int i = 0; i < length; i++){ + try{ + int oldPart = i < oldParts.length ? Integer.parseInt(oldParts[i]) : 0; + int newPart = i < newParts.length ? Integer.parseInt(newParts[i]) : 0; + + if(oldPart < newPart) + return 1; + + if(oldPart > newPart) + return -1; + }catch(NumberFormatException ex){ + return -2; + } + } + return 0; + } } } diff --git a/paper/src/main/resources/plugin.yml b/paper/src/main/resources/plugin.yml index 9895f911..0e5861c7 100644 --- a/paper/src/main/resources/plugin.yml +++ b/paper/src/main/resources/plugin.yml @@ -7,6 +7,9 @@ main: 'ch.andre601.advancedserverlist.paper.PaperCore' api-version: 1.19 +libraries: + - 'com.squareup.okhttp3:okhttp:4.10.0' + softdepend: - 'PlaceholderAPI' diff --git a/pom.xml b/pom.xml index 8bfd1701..4323f8aa 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ UTF-8 - 1.6.4 + 1.7.0 Create multiple Server lists based on conditions. 16 diff --git a/spigot/src/main/resources/plugin.yml b/spigot/src/main/resources/plugin.yml index 590efb8c..0f6f252f 100644 --- a/spigot/src/main/resources/plugin.yml +++ b/spigot/src/main/resources/plugin.yml @@ -13,6 +13,7 @@ libraries: - 'net.kyori:adventure-text-minimessage:4.11.0' - 'net.kyori:adventure-text-serializer-legacy:4.11.0' - 'net.kyori:adventure-platform-bukkit:4.1.2' + - 'com.squareup.okhttp3:okhttp:4.10.0' softdepend: - 'ProtocolLib' diff --git a/velocity/pom.xml b/velocity/pom.xml index 4e8092de..d94c49ec 100644 --- a/velocity/pom.xml +++ b/velocity/pom.xml @@ -104,6 +104,10 @@ com.github.benmanes.caffeine ch.andre601.advancedserverlist.velocity.depends.caffeine + + okhttp3 + ch.andre601.advancedserverlist.velocity.depends.okhttp3 + From 25c053d495cfc5dfd28c3a09653cf38b0c14331f Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Mon, 5 Dec 2022 22:24:19 +0100 Subject: [PATCH 04/10] Use timer class from Java --- .../bungeecord/BungeeCordCore.java | 8 ++- .../core/AdvancedServerList.java | 56 +++++++++++-------- .../core/check/UpdateChecker.java | 4 -- .../core/interfaces/core/PluginCore.java | 2 + .../advancedserverlist/paper/PaperCore.java | 14 +++-- .../advancedserverlist/spigot/SpigotCore.java | 6 +- .../spigot/events/PingEvent.java | 3 +- .../velocity/VelocityCore.java | 8 ++- 8 files changed, 63 insertions(+), 38 deletions(-) diff --git a/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java b/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java index 06970507..91382631 100644 --- a/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java +++ b/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java @@ -30,8 +30,8 @@ import ch.andre601.advancedserverlist.bungeecord.events.PingEvent; import ch.andre601.advancedserverlist.bungeecord.logging.BungeeLogger; import ch.andre601.advancedserverlist.core.AdvancedServerList; -import ch.andre601.advancedserverlist.core.interfaces.core.ProxyCore; import ch.andre601.advancedserverlist.core.interfaces.PluginLogger; +import ch.andre601.advancedserverlist.core.interfaces.core.ProxyCore; import ch.andre601.advancedserverlist.core.parsing.ComponentParser; import ch.andre601.advancedserverlist.core.profiles.favicon.FaviconHandler; import ch.andre601.advancedserverlist.core.profiles.replacer.placeholders.Placeholders; @@ -55,7 +55,6 @@ public class BungeeCordCore extends Plugin implements ProxyCore { - if(version == null || throwable != null){ - logger.warn("Update check failed! See previous messages for explanations and causes."); - return; - } - - int result = version.compare(getVersion()); - switch(result){ - case -2 -> { - logger.warn("Encountered an exception while comparing versions. Are they valid?"); - logger.warn("Own version: %s; New version: %s", getVersion(), version.getVersionNumber()); + new Timer("AdvancedServerList-UpdateChecker-Thread").scheduleAtFixedRate( + new TimerTask(){ + @Override + public void run(){ + logger.info("Checking for updates. Please wait..."); + updateChecker.checkUpdate(platform).whenComplete((version, throwable) -> { + if(version == null || throwable != null){ + logger.warn("Update check failed! See previous messages for explanations and causes."); + return; + } + + int result = version.compare(getVersion()); + switch(result){ + case -2 -> { + logger.warn("Encountered an exception while comparing versions. Are they valid?"); + logger.warn("Own version: %s; New version: %s", getVersion(), version.getVersionNumber()); + } + case -1 -> logger.info("You seem to run a newer version compared to Modrinth. Are you running a dev build?"); + case 0 -> logger.info("No new update found. You're running the latest version!"); + case 1 -> printUpdateBanner(version.getVersionNumber(), version.getId()); + } + }); } - case -1 -> logger.info("You seem to run a newer version compared to Modrinth. Are you running a dev build?"); - case 0 -> logger.info("No new update found. You're running the latest version!"); - case 1 -> printUpdateBanner(version.getVersionNumber(), version.getId()); - } - }); + }, + 0L, + TimeUnit.HOURS.toMillis(12L) + ); } private void load(){ @@ -156,6 +163,9 @@ private void load(){ getPlugin().getPluginLogger().info("Metrics loaded!"); getPlugin().getPluginLogger().info("AdvancedServerList is ready!"); + + if(getFileHandler().getBoolean("check_updates")) + plugin.enableUpdateCheck(); } private void printBanner(){ diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java index 211cab8a..d5ebc8d3 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java @@ -112,10 +112,6 @@ public CompletableFuture checkUpdate(String loader){ logger.warn("Encountered Exception while checking for an update!", ex); return null; } - }, r -> { - Thread t = new Thread(r, "AdvancedServerList-UpdateCheck-Thread"); - t.setDaemon(true); - t.start(); }); } diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java b/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java index cf470f12..00201cda 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java @@ -41,6 +41,8 @@ public interface PluginCore{ void clearFaviconCache(); + void enableUpdateCheck(); + AdvancedServerList getCore(); Path getFolderPath(); diff --git a/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java b/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java index c7c60749..ccc2b5ec 100644 --- a/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java +++ b/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java @@ -123,6 +123,11 @@ public void clearFaviconCache(){ faviconHandler.clearCache(); } + @Override + public void enableUpdateCheck(){ + core.checkForUpdates("paper"); + } + @Override public AdvancedServerList getCore(){ return core; @@ -156,11 +161,6 @@ public String getPlatformVersion(){ return getServer().getVersion(); } - private void enable(){ - this.core = new AdvancedServerList(this); - core.checkForUpdates("paper"); - } - @Override public List createPlayers(List lines, OfflinePlayer player, Placeholders... placeholders){ List players = new ArrayList<>(lines.size()); @@ -182,4 +182,8 @@ public List createPlayers(List lines, OfflinePlayer playe return players; } + + private void enable(){ + this.core = new AdvancedServerList(this); + } } diff --git a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java index 5bf6eb64..51327a79 100644 --- a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java +++ b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java @@ -71,7 +71,6 @@ public void onEnable(){ } this.core = new AdvancedServerList(this); - core.checkForUpdates("spigot"); } @Override @@ -109,6 +108,11 @@ public void clearFaviconCache(){ faviconHandler.clearCache(); } + @Override + public void enableUpdateCheck(){ + core.checkForUpdates("spigot"); + } + @Override public AdvancedServerList getCore(){ return core; diff --git a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/events/PingEvent.java b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/events/PingEvent.java index 5165759b..bc9e128a 100644 --- a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/events/PingEvent.java +++ b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/events/PingEvent.java @@ -47,7 +47,8 @@ import org.bukkit.event.Listener; import java.net.InetSocketAddress; -import java.util.*; +import java.util.HashMap; +import java.util.Map; public class PingEvent implements Listener{ diff --git a/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java b/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java index c5812cd8..d4dbaabe 100644 --- a/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java +++ b/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java @@ -26,8 +26,8 @@ package ch.andre601.advancedserverlist.velocity; import ch.andre601.advancedserverlist.core.AdvancedServerList; -import ch.andre601.advancedserverlist.core.interfaces.core.ProxyCore; import ch.andre601.advancedserverlist.core.interfaces.PluginLogger; +import ch.andre601.advancedserverlist.core.interfaces.core.ProxyCore; import ch.andre601.advancedserverlist.core.parsing.ComponentParser; import ch.andre601.advancedserverlist.core.profiles.favicon.FaviconHandler; import ch.andre601.advancedserverlist.core.profiles.replacer.placeholders.Placeholders; @@ -75,7 +75,6 @@ public VelocityCore(ProxyServer proxy, @DataDirectory Path path, Metrics.Factory @Subscribe public void init(ProxyInitializeEvent event){ this.core = new AdvancedServerList(this); - core.checkForUpdates("velocity"); } @Subscribe @@ -114,6 +113,11 @@ public void clearFaviconCache(){ faviconHandler.clearCache(); } + @Override + public void enableUpdateCheck(){ + core.checkForUpdates("velocity"); + } + @Override public AdvancedServerList getCore(){ return core; From a1276da68f8321ab782ec8e91b12ccba37262136 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Mon, 5 Dec 2022 23:13:21 +0100 Subject: [PATCH 05/10] Fix exceptions --- .../bungeecord/BungeeCordCore.java | 6 +----- .../core/AdvancedServerList.java | 17 +++++++++++++---- .../core/interfaces/core/PluginCore.java | 2 -- .../advancedserverlist/paper/PaperCore.java | 6 +----- .../advancedserverlist/spigot/SpigotCore.java | 6 +----- .../velocity/VelocityCore.java | 6 +----- 6 files changed, 17 insertions(+), 26 deletions(-) diff --git a/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java b/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java index 91382631..60f8baa2 100644 --- a/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java +++ b/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java @@ -55,6 +55,7 @@ public class BungeeCordCore extends Plugin implements ProxyCore plugin){ @@ -56,6 +58,7 @@ public AdvancedServerList(PluginCore plugin){ this.commandHandler = new CommandHandler(this); this.playerHandler = new PlayerHandler(this); this.updateChecker = new UpdateChecker(this); + this.timer = new Timer("AdvancedServerList UpdateCheck-Thread"); load(); } @@ -83,6 +86,10 @@ public String getVersion(){ public void disable(){ getPlugin().getPluginLogger().info("Saving cache.data file..."); getPlayerHandler().save(); + + getPlugin().getPluginLogger().info("Disabling Update Checker..."); + timer.cancel(); + getPlugin().getPluginLogger().info("AdvancedServerList disabled!"); } @@ -92,8 +99,13 @@ public void clearFaviconCache(){ public void checkForUpdates(String platform){ PluginLogger logger = plugin.getPluginLogger(); + logger.info("Enabling Update checker..."); + if(!getFileHandler().getBoolean("check_updates")){ + logger.info("'check_updates' is set to 'false'. Not checking for new Updates."); + return; + } - new Timer("AdvancedServerList-UpdateChecker-Thread").scheduleAtFixedRate( + timer.scheduleAtFixedRate( new TimerTask(){ @Override public void run(){ @@ -163,9 +175,6 @@ private void load(){ getPlugin().getPluginLogger().info("Metrics loaded!"); getPlugin().getPluginLogger().info("AdvancedServerList is ready!"); - - if(getFileHandler().getBoolean("check_updates")) - plugin.enableUpdateCheck(); } private void printBanner(){ diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java b/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java index 00201cda..cf470f12 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java @@ -41,8 +41,6 @@ public interface PluginCore{ void clearFaviconCache(); - void enableUpdateCheck(); - AdvancedServerList getCore(); Path getFolderPath(); diff --git a/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java b/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java index ccc2b5ec..f6520088 100644 --- a/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java +++ b/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java @@ -123,11 +123,6 @@ public void clearFaviconCache(){ faviconHandler.clearCache(); } - @Override - public void enableUpdateCheck(){ - core.checkForUpdates("paper"); - } - @Override public AdvancedServerList getCore(){ return core; @@ -185,5 +180,6 @@ public List createPlayers(List lines, OfflinePlayer playe private void enable(){ this.core = new AdvancedServerList(this); + core.checkForUpdates("paper"); } } diff --git a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java index 51327a79..5bf6eb64 100644 --- a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java +++ b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java @@ -71,6 +71,7 @@ public void onEnable(){ } this.core = new AdvancedServerList(this); + core.checkForUpdates("spigot"); } @Override @@ -108,11 +109,6 @@ public void clearFaviconCache(){ faviconHandler.clearCache(); } - @Override - public void enableUpdateCheck(){ - core.checkForUpdates("spigot"); - } - @Override public AdvancedServerList getCore(){ return core; diff --git a/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java b/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java index d4dbaabe..5081eef3 100644 --- a/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java +++ b/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java @@ -75,6 +75,7 @@ public VelocityCore(ProxyServer proxy, @DataDirectory Path path, Metrics.Factory @Subscribe public void init(ProxyInitializeEvent event){ this.core = new AdvancedServerList(this); + core.checkForUpdates("velocity"); } @Subscribe @@ -113,11 +114,6 @@ public void clearFaviconCache(){ faviconHandler.clearCache(); } - @Override - public void enableUpdateCheck(){ - core.checkForUpdates("velocity"); - } - @Override public AdvancedServerList getCore(){ return core; From fb6d93b45572da78b0234627098e23c740dfc7a6 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Mon, 5 Dec 2022 23:55:54 +0100 Subject: [PATCH 06/10] Init timer only when UpdateChecker is used. --- .../bungeecord/BungeeCordCore.java | 6 +- .../core/AdvancedServerList.java | 67 +++---------------- .../core/check/UpdateChecker.java | 60 ++++++++++++++++- .../core/interfaces/core/PluginCore.java | 2 + .../advancedserverlist/paper/PaperCore.java | 6 +- .../advancedserverlist/spigot/SpigotCore.java | 6 +- .../velocity/VelocityCore.java | 6 +- 7 files changed, 88 insertions(+), 65 deletions(-) diff --git a/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java b/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java index 60f8baa2..694fc451 100644 --- a/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java +++ b/bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/BungeeCordCore.java @@ -55,7 +55,6 @@ public class BungeeCordCore extends Plugin implements ProxyCore createPlayers(List lines, Placeholders... placeholders){ List players = new ArrayList<>(lines.size()); diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java index 4de53610..7dd44c45 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java @@ -28,7 +28,6 @@ import ch.andre601.advancedserverlist.core.check.UpdateChecker; import ch.andre601.advancedserverlist.core.commands.CommandHandler; import ch.andre601.advancedserverlist.core.file.FileHandler; -import ch.andre601.advancedserverlist.core.interfaces.PluginLogger; import ch.andre601.advancedserverlist.core.interfaces.core.PluginCore; import ch.andre601.advancedserverlist.core.profiles.players.PlayerHandler; @@ -36,9 +35,6 @@ import java.io.InputStream; import java.nio.file.Path; import java.util.Properties; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.TimeUnit; public class AdvancedServerList{ @@ -46,9 +42,8 @@ public class AdvancedServerList{ private final FileHandler fileHandler; private final CommandHandler commandHandler; private final PlayerHandler playerHandler; - private final UpdateChecker updateChecker; - private final Timer timer; + private UpdateChecker updateChecker; private String version; @@ -57,8 +52,6 @@ public AdvancedServerList(PluginCore plugin){ this.fileHandler = new FileHandler(this); this.commandHandler = new CommandHandler(this); this.playerHandler = new PlayerHandler(this); - this.updateChecker = new UpdateChecker(this); - this.timer = new Timer("AdvancedServerList UpdateCheck-Thread"); load(); } @@ -87,8 +80,10 @@ public void disable(){ getPlugin().getPluginLogger().info("Saving cache.data file..."); getPlayerHandler().save(); - getPlugin().getPluginLogger().info("Disabling Update Checker..."); - timer.cancel(); + if(updateChecker != null){ + getPlugin().getPluginLogger().info("Disabling Update Checker..."); + updateChecker.disable(); + } getPlugin().getPluginLogger().info("AdvancedServerList disabled!"); } @@ -97,43 +92,6 @@ public void clearFaviconCache(){ plugin.clearFaviconCache(); } - public void checkForUpdates(String platform){ - PluginLogger logger = plugin.getPluginLogger(); - logger.info("Enabling Update checker..."); - if(!getFileHandler().getBoolean("check_updates")){ - logger.info("'check_updates' is set to 'false'. Not checking for new Updates."); - return; - } - - timer.scheduleAtFixedRate( - new TimerTask(){ - @Override - public void run(){ - logger.info("Checking for updates. Please wait..."); - updateChecker.checkUpdate(platform).whenComplete((version, throwable) -> { - if(version == null || throwable != null){ - logger.warn("Update check failed! See previous messages for explanations and causes."); - return; - } - - int result = version.compare(getVersion()); - switch(result){ - case -2 -> { - logger.warn("Encountered an exception while comparing versions. Are they valid?"); - logger.warn("Own version: %s; New version: %s", getVersion(), version.getVersionNumber()); - } - case -1 -> logger.info("You seem to run a newer version compared to Modrinth. Are you running a dev build?"); - case 0 -> logger.info("No new update found. You're running the latest version!"); - case 1 -> printUpdateBanner(version.getVersionNumber(), version.getId()); - } - }); - } - }, - 0L, - TimeUnit.HOURS.toMillis(12L) - ); - } - private void load(){ printBanner(); resolveVersion(); @@ -175,6 +133,9 @@ private void load(){ getPlugin().getPluginLogger().info("Metrics loaded!"); getPlugin().getPluginLogger().info("AdvancedServerList is ready!"); + + if(getFileHandler().getBoolean("check_updates")) + this.updateChecker = new UpdateChecker(this, plugin.getLoader()); } private void printBanner(){ @@ -188,18 +149,6 @@ private void printBanner(){ getPlugin().getPluginLogger().info(""); } - private void printUpdateBanner(String version, String versionId){ - getPlugin().getPluginLogger().info("=================================================================="); - getPlugin().getPluginLogger().info("You are running an outdated version of AdvancedServerList!"); - getPlugin().getPluginLogger().info(""); - getPlugin().getPluginLogger().info("Your version: %s", getVersion()); - getPlugin().getPluginLogger().info("Modrinth version: %s", version); - getPlugin().getPluginLogger().info(""); - getPlugin().getPluginLogger().info("You can download the latest release from here:"); - getPlugin().getPluginLogger().info("https://modrinth.com/plugin/advancedserverlist/version/%s", versionId); - getPlugin().getPluginLogger().info("=================================================================="); - } - private void resolveVersion(){ try(InputStream is = getClass().getResourceAsStream("/version.properties")){ Properties properties = new Properties(); diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java index d5ebc8d3..b235befe 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java @@ -41,7 +41,10 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; +import java.util.Timer; +import java.util.TimerTask; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.TimeUnit; public class UpdateChecker{ @@ -49,6 +52,8 @@ public class UpdateChecker{ private final OkHttpClient client = new OkHttpClient(); private final AdvancedServerList core; + private final PluginLogger logger; + private final String loader; private final Type listType = new TypeToken>(){}.getType(); private final Gson gson = new GsonBuilder() @@ -57,11 +62,50 @@ public class UpdateChecker{ .setLenient() .create(); - public UpdateChecker(AdvancedServerList core){ + private final Timer timer = new Timer("AdvancedServerList Update-Thread"); + + public UpdateChecker(AdvancedServerList core, String loader){ this.core = core; + this.logger = core.getPlugin().getPluginLogger(); + this.loader = loader; + + startUpdateChecker(); + } + + public void startUpdateChecker(){ + timer.scheduleAtFixedRate( + new TimerTask(){ + @Override + public void run(){ + checkUpdate().whenComplete((version, throwable) -> { + if(version == null || throwable != null){ + logger.warn("Failed to look for any updates. See previous messages for reasons."); + return; + } + + int result = version.compare(core.getVersion()); + switch(result){ + case -2 -> { + logger.warn("Encountered an exception while comparing versions. Are they valid?"); + logger.warn("Own version: %s; New version: %s", core.getVersion(), version.getVersionNumber()); + } + case -1 -> logger.info("You seem to run a newer version compared to Modrinth. Are you running a dev build?"); + case 0 -> logger.info("No new update found. You're running the latest version!"); + case 1 -> printUpdateBanner(version.getVersionNumber(), version.getId()); + } + }); + } + }, + 0L, + TimeUnit.HOURS.toMillis(12L) + ); } - public CompletableFuture checkUpdate(String loader){ + public void disable(){ + timer.cancel(); + } + + private CompletableFuture checkUpdate(){ return CompletableFuture.supplyAsync(() -> { PluginLogger logger = core.getPlugin().getPluginLogger(); if(core.getVersion().equals("UNKNOWN")){ @@ -115,6 +159,18 @@ public CompletableFuture checkUpdate(String loader){ }); } + private void printUpdateBanner(String version, String versionId){ + logger.info("=================================================================="); + logger.info("You are running an outdated version of AdvancedServerList!"); + logger.info(""); + logger.info("Your version: %s", core.getVersion()); + logger.info("Modrinth version: %s", version); + logger.info(""); + logger.info("You can download the latest release from here:"); + logger.info("https://modrinth.com/plugin/advancedserverlist/version/%s", versionId); + logger.info("=================================================================="); + } + public static class ModrinthVersion{ private String id; private String versionNumber; diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java b/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java index cf470f12..a91bc2e4 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/interfaces/core/PluginCore.java @@ -52,4 +52,6 @@ public interface PluginCore{ String getPlatformName(); String getPlatformVersion(); + + String getLoader(); } diff --git a/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java b/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java index f6520088..80ff899a 100644 --- a/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java +++ b/paper/src/main/java/ch/andre601/advancedserverlist/paper/PaperCore.java @@ -156,6 +156,11 @@ public String getPlatformVersion(){ return getServer().getVersion(); } + @Override + public String getLoader(){ + return "paper"; + } + @Override public List createPlayers(List lines, OfflinePlayer player, Placeholders... placeholders){ List players = new ArrayList<>(lines.size()); @@ -180,6 +185,5 @@ public List createPlayers(List lines, OfflinePlayer playe private void enable(){ this.core = new AdvancedServerList(this); - core.checkForUpdates("paper"); } } diff --git a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java index 5bf6eb64..0dcdf950 100644 --- a/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java +++ b/spigot/src/main/java/ch/andre601/advancedserverlist/spigot/SpigotCore.java @@ -71,7 +71,6 @@ public void onEnable(){ } this.core = new AdvancedServerList(this); - core.checkForUpdates("spigot"); } @Override @@ -142,6 +141,11 @@ public String getPlatformVersion(){ return getServer().getVersion(); } + @Override + public String getLoader(){ + return "spigot"; + } + @Override public List createPlayers(List lines, OfflinePlayer player, Placeholders... placeholders){ List players = new ArrayList<>(lines.size()); diff --git a/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java b/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java index 5081eef3..f44234c6 100644 --- a/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java +++ b/velocity/src/main/java/ch/andre601/advancedserverlist/velocity/VelocityCore.java @@ -75,7 +75,6 @@ public VelocityCore(ProxyServer proxy, @DataDirectory Path path, Metrics.Factory @Subscribe public void init(ProxyInitializeEvent event){ this.core = new AdvancedServerList(this); - core.checkForUpdates("velocity"); } @Subscribe @@ -163,6 +162,11 @@ public String getPlatformVersion(){ return getProxy().getVersion().getVersion(); } + @Override + public String getLoader(){ + return "velocity"; + } + public ProxyServer getProxy(){ return proxy; } From b82cf69d430e71b92c4ee5146957868d0132e9a6 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Mon, 5 Dec 2022 23:58:05 +0100 Subject: [PATCH 07/10] Simplify more. --- .../andre601/advancedserverlist/core/AdvancedServerList.java | 2 +- .../andre601/advancedserverlist/core/check/UpdateChecker.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java index 7dd44c45..d17633af 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/AdvancedServerList.java @@ -135,7 +135,7 @@ private void load(){ getPlugin().getPluginLogger().info("AdvancedServerList is ready!"); if(getFileHandler().getBoolean("check_updates")) - this.updateChecker = new UpdateChecker(this, plugin.getLoader()); + this.updateChecker = new UpdateChecker(this); } private void printBanner(){ diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java index b235befe..1e7577bb 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java @@ -64,10 +64,10 @@ public class UpdateChecker{ private final Timer timer = new Timer("AdvancedServerList Update-Thread"); - public UpdateChecker(AdvancedServerList core, String loader){ + public UpdateChecker(AdvancedServerList core){ this.core = core; this.logger = core.getPlugin().getPluginLogger(); - this.loader = loader; + this.loader = core.getPlugin().getLoader(); startUpdateChecker(); } From 9a30a6b679a0c01236c4306392c2b8f8b393e5b8 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Tue, 6 Dec 2022 00:01:24 +0100 Subject: [PATCH 08/10] Add info line --- .../ch/andre601/advancedserverlist/core/check/UpdateChecker.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java index 1e7577bb..ee3eef1e 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java @@ -77,6 +77,7 @@ public void startUpdateChecker(){ new TimerTask(){ @Override public void run(){ + logger.info("Checking for a new update..."); checkUpdate().whenComplete((version, throwable) -> { if(version == null || throwable != null){ logger.warn("Failed to look for any updates. See previous messages for reasons."); From 12d8d930dd22d81c14ab58addbacea024a57b512 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Tue, 6 Dec 2022 00:26:55 +0100 Subject: [PATCH 09/10] Use a ScheduledExecutorService instead of Timer --- .../core/check/UpdateCheckThread.java | 37 +++++++++++ .../core/check/UpdateChecker.java | 64 +++++++++---------- 2 files changed, 69 insertions(+), 32 deletions(-) create mode 100644 core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateCheckThread.java diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateCheckThread.java b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateCheckThread.java new file mode 100644 index 00000000..313442c6 --- /dev/null +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateCheckThread.java @@ -0,0 +1,37 @@ +/* + * MIT License + * + * Copyright (c) 2022 Andre_601 + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package ch.andre601.advancedserverlist.core.check; + +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.ThreadFactory; + +public class UpdateCheckThread implements ThreadFactory{ + @Override + public Thread newThread(@NotNull Runnable r){ + return new Thread(r, "AdvancedServerList Update-Thread"); + } +} diff --git a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java index ee3eef1e..57199675 100644 --- a/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java +++ b/core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java @@ -41,10 +41,7 @@ import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; -import java.util.Timer; -import java.util.TimerTask; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; public class UpdateChecker{ @@ -62,7 +59,7 @@ public class UpdateChecker{ .setLenient() .create(); - private final Timer timer = new Timer("AdvancedServerList Update-Thread"); + private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new UpdateCheckThread()); public UpdateChecker(AdvancedServerList core){ this.core = core; @@ -73,37 +70,40 @@ public UpdateChecker(AdvancedServerList core){ } public void startUpdateChecker(){ - timer.scheduleAtFixedRate( - new TimerTask(){ - @Override - public void run(){ - logger.info("Checking for a new update..."); - checkUpdate().whenComplete((version, throwable) -> { - if(version == null || throwable != null){ - logger.warn("Failed to look for any updates. See previous messages for reasons."); - return; - } - - int result = version.compare(core.getVersion()); - switch(result){ - case -2 -> { - logger.warn("Encountered an exception while comparing versions. Are they valid?"); - logger.warn("Own version: %s; New version: %s", core.getVersion(), version.getVersionNumber()); - } - case -1 -> logger.info("You seem to run a newer version compared to Modrinth. Are you running a dev build?"); - case 0 -> logger.info("No new update found. You're running the latest version!"); - case 1 -> printUpdateBanner(version.getVersionNumber(), version.getId()); - } - }); + executor.scheduleAtFixedRate(() -> { + logger.info("Checking for a new update..."); + checkUpdate().whenComplete((version, throwable) -> { + if(version == null || throwable != null){ + logger.warn("Failed to look for any updates. See previous messages for reasons."); + return; } - }, - 0L, - TimeUnit.HOURS.toMillis(12L) - ); + + int result = version.compare(core.getVersion()); + switch(result){ + case -2 -> { + logger.warn("Encountered an exception while comparing versions. Are they valid?"); + logger.warn("Own version: %s; New version: %s", core.getVersion(), version.getVersionNumber()); + } + case -1 -> logger.info("You seem to run a newer version compared to Modrinth. Are you running a dev build?"); + case 0 -> logger.info("No new update found. You're running the latest version!"); + case 1 -> printUpdateBanner(version.getVersionNumber(), version.getId()); + } + }); + }, 0L, 12L, TimeUnit.HOURS); } public void disable(){ - timer.cancel(); + executor.shutdown(); + try{ + if(!executor.awaitTermination(1, TimeUnit.SECONDS)){ + executor.shutdownNow(); + if(!executor.awaitTermination(1, TimeUnit.SECONDS)) + logger.warn("Scheduler didn't terminate in time!"); + } + }catch(InterruptedException ex){ + executor.shutdownNow(); + Thread.currentThread().interrupt(); + } } private CompletableFuture checkUpdate(){ From ad3ba0c8b30fb3f62276909d96ee070415e8bb55 Mon Sep 17 00:00:00 2001 From: Andre601 <11576465+Andre601@users.noreply.github.com> Date: Tue, 6 Dec 2022 00:49:52 +0100 Subject: [PATCH 10/10] Update config description --- core/src/main/resources/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/src/main/resources/config.yml b/core/src/main/resources/config.yml index 27a39ce9..8eb116cf 100644 --- a/core/src/main/resources/config.yml +++ b/core/src/main/resources/config.yml @@ -14,7 +14,7 @@ disable_cache: false # # Should AdvancedServerList check for new versions? # -# When set to true will AdvancedServerList connect to modrinth.com to check for any new versions for the platform it is used -# on. This check only happens when the plugin enables. +# When set to true will AdvancedServerList check for a new version on modrinth.com every 12 hours and inform you in the +# console about whether it found any new update or not. # check_updates: true \ No newline at end of file