From 73faa4408e4537498e967a69631afa80e8349a11 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Tue, 3 Aug 2021 12:14:59 +0200 Subject: [PATCH] Makes the version comparator more generic --- .../minecraftserverlauncher/utility/Updater.java | 15 ++++++--------- .../utility/UpdaterTest.java | 2 ++ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java index 5189d5f..e745264 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/Updater.java @@ -87,17 +87,14 @@ public final class Updater { 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]) { + for (int i = 0; i < Math.max(oldVersionParts.length, newVersionParts.length); i++) { + int oldVersionNumber = oldVersionParts.length > i ? Integer.parseInt(oldVersionParts[i]) : 0; + int newVersionNumber = newVersionParts.length > i ? Integer.parseInt(newVersionParts[i]) : 0; + if (newVersionNumber > oldVersionNumber) { return true; + } else if (newVersionNumber < oldVersionNumber) { + return false; } - return newVersionInts[1] == oldVersionInts[1] && newVersionInts[2] > oldVersionInts[2]; } return false; } diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/UpdaterTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/UpdaterTest.java index 8eeb611..f4f40d8 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/UpdaterTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/UpdaterTest.java @@ -16,6 +16,8 @@ public class UpdaterTest { 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")); + assertTrue(Updater.isVersionHigher("1.2", "1.2.1")); + assertTrue(Updater.isVersionHigher("1.1.2", "1.2")); } }