From 332e085499e820767df6cb60337ed0768e59f815 Mon Sep 17 00:00:00 2001 From: LMBishop <13875753+LMBishop@users.noreply.github.com> Date: Fri, 25 Jun 2021 13:53:09 +0100 Subject: Improve version checker to check previous versions - Closes #189 --- .../quests/common/updater/Updater.java | 35 +++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'common') diff --git a/common/src/main/java/com/leonardobishop/quests/common/updater/Updater.java b/common/src/main/java/com/leonardobishop/quests/common/updater/Updater.java index 0bc8bd94..28f21713 100644 --- a/common/src/main/java/com/leonardobishop/quests/common/updater/Updater.java +++ b/common/src/main/java/com/leonardobishop/quests/common/updater/Updater.java @@ -9,6 +9,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; /** * The updater checks for updates on Spigot, and prints to the logger if one is found. @@ -17,6 +18,7 @@ public class Updater { private static final int PROJECT_ID = 23696; private final String installedVersion; + private final int[] tokenizedInstalledVersion; private final boolean enabled; private final Quests plugin; @@ -28,6 +30,7 @@ public class Updater { public Updater(Quests plugin, String installedVersion, boolean enabled) { this.plugin = plugin; this.installedVersion = installedVersion; + this.tokenizedInstalledVersion = tokenize(installedVersion); this.enabled = enabled; try { this.api = new URL(getApiUrl()); @@ -62,7 +65,22 @@ public class Updater { lastCheck = System.currentTimeMillis(); URLConnection con = api.openConnection(); returnedVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); - if (!returnedVersion.equals(installedVersion)) { + + int[] tokenizedReturnedVersion = tokenize(returnedVersion); + + boolean newVersion = false; + for (int i = 0; i < tokenizedReturnedVersion.length; i++) { + if (tokenizedInstalledVersion.length <= i && tokenizedReturnedVersion[i] != 0) { + newVersion = true; + break; + } + if (tokenizedReturnedVersion[i] > tokenizedInstalledVersion[i]) { + newVersion = true; + break; + } + } + + if (newVersion) { plugin.getQuestsLogger().info("A new version " + returnedVersion + " was found on Spigot (your version: " + installedVersion + "). Please update me! <3 - Link: " + getUpdateLink()); updateReady = true; } else { @@ -78,4 +96,19 @@ public class Updater { return updateReady; } + private int[] tokenize(String s) { + String[] numericVersion = s.split(Pattern.quote("-")); + String[] tokenizedVersion = numericVersion[0].split(Pattern.quote(".")); + int[] intTokenizedVersion = new int[tokenizedVersion.length]; + for (int i = 0; i < tokenizedVersion.length; i++) { + try { + intTokenizedVersion[i] = Integer.parseInt(tokenizedVersion[i]); + } catch (NumberFormatException ignored) { + intTokenizedVersion[i] = 0; + } + } + + return intTokenizedVersion; + } + } \ No newline at end of file -- cgit v1.2.3-70-g09d2