From 85febdf41b509078de5a9a1d584f043161011413 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 3 Aug 2021 11:50:32 +0200 Subject: [PATCH] Improves version checking to prevent and update notice when running an unreleased version --- .../utility/Updater.java | 27 ++++++++++++++++++- .../utility/UpdaterTest.java | 21 +++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/knarcraft/minecraftserverlauncher/utility/UpdaterTest.java diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java index 323c40b..5189d5f 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java @@ -51,7 +51,7 @@ public final class Updater { JsonObject jsonObject = new JsonParser().parse(data).getAsJsonObject(); String latest = jsonObject.getAsJsonObject("latest").get(updateChannel).getAsString(); - if (!oldType.equals(updateChannel) || !oldVer.equals(latest)) { + if (!oldType.equals(updateChannel) || isVersionHigher(oldVer, latest)) { JsonArray versionList = jsonObject.getAsJsonArray("versions"); String url = ""; for (JsonElement elem : versionList) { @@ -77,6 +77,31 @@ public final class Updater { } } + /** + * Decides whether one version number is higher than another + * + * @param oldVersion

The old version to check

+ * @param newVersion

The new version to check

+ * @return

True if the new version is higher than the old one

+ */ + public static boolean isVersionHigher(String oldVersion, String newVersion) { + String[] oldVersionParts = oldVersion.split("\\."); + String[] newVersionParts = newVersion.split("\\."); + int[] oldVersionInts = new int[]{Integer.parseInt(oldVersionParts[0]), Integer.parseInt(oldVersionParts[1]), + Integer.parseInt(oldVersionParts[2])}; + int[] newVersionInts = new int[]{Integer.parseInt(newVersionParts[0]), Integer.parseInt(newVersionParts[1]), + Integer.parseInt(newVersionParts[2])}; + if (newVersionInts[0] > oldVersionInts[0]) { + return true; + } else if (newVersionInts[0] == oldVersionInts[0]) { + if (newVersionInts[1] > oldVersionInts[1]) { + return true; + } + return newVersionInts[1] == oldVersionInts[1] && newVersionInts[2] > oldVersionInts[2]; + } + return false; + } + /** * Updates the software * diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/UpdaterTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/UpdaterTest.java new file mode 100644 index 0000000..8eeb611 --- /dev/null +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/UpdaterTest.java @@ -0,0 +1,21 @@ +package net.knarcraft.minecraftserverlauncher.utility; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class UpdaterTest { + + @Test + public void isVersionHigherTest() { + assertTrue(Updater.isVersionHigher("1.1.1", "1.1.2")); + assertFalse(Updater.isVersionHigher("1.1.1", "1.1.1")); + assertFalse(Updater.isVersionHigher("1.1.1", "1.1.0")); + assertTrue(Updater.isVersionHigher("1.1.1", "1.2.1")); + assertFalse(Updater.isVersionHigher("1.2.1", "1.1.6")); + assertTrue(Updater.isVersionHigher("1.2.1", "2.1.6")); + assertTrue(Updater.isVersionHigher("1.2.1", "2.0.0")); + } + +}