diff options
| author | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2021-06-25 13:53:09 +0100 |
|---|---|---|
| committer | LMBishop <13875753+LMBishop@users.noreply.github.com> | 2021-06-25 13:53:09 +0100 |
| commit | 332e085499e820767df6cb60337ed0768e59f815 (patch) | |
| tree | 0597560bb8e53797359041b4f1c52f8298e8145b /common/src/main/java/com/leonardobishop | |
| parent | cfc0e55c1a0c81087eb7af0d8929829941d9e7c3 (diff) | |
Improve version checker to check previous versions
- Closes #189
Diffstat (limited to 'common/src/main/java/com/leonardobishop')
| -rw-r--r-- | common/src/main/java/com/leonardobishop/quests/common/updater/Updater.java | 35 |
1 files changed, 34 insertions, 1 deletions
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 |
