diff --git a/src/main/java/dev/heliosares/auxprotect/spigot/AuxProtectSpigot.java b/src/main/java/dev/heliosares/auxprotect/spigot/AuxProtectSpigot.java index 3be5667..fc53879 100644 --- a/src/main/java/dev/heliosares/auxprotect/spigot/AuxProtectSpigot.java +++ b/src/main/java/dev/heliosares/auxprotect/spigot/AuxProtectSpigot.java @@ -481,7 +481,7 @@ public class AuxProtectSpigot extends JavaPlugin implements IAuxProtect { debug("Checking for updates...", 1); String newVersion; try { - newVersion = UpdateChecker.getVersion(AuxProtectSpigot.this, 99147); + newVersion = UpdateChecker.getVersion(99147); } catch (IOException e) { print(e); return; diff --git a/src/main/java/dev/heliosares/auxprotect/utils/UpdateChecker.java b/src/main/java/dev/heliosares/auxprotect/utils/UpdateChecker.java index 3d309a1..593b7a8 100644 --- a/src/main/java/dev/heliosares/auxprotect/utils/UpdateChecker.java +++ b/src/main/java/dev/heliosares/auxprotect/utils/UpdateChecker.java @@ -1,75 +1,68 @@ package dev.heliosares.auxprotect.utils; -import org.bukkit.plugin.java.JavaPlugin; - +import javax.annotation.Nonnull; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Scanner; public class UpdateChecker { /** - * From: - * https://www.spigotmc.org/wiki/creating-an-update-checker-that-checks-for-updates + * Source */ - public static String getVersion(JavaPlugin plugin, int resourceId) throws IOException { - try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId) - .openStream(); Scanner scanner = new Scanner(inputStream)) { - if (scanner.hasNext()) { - return scanner.next(); - } + public static String getVersion(int resourceId) throws IOException { + try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) { + if (scanner.hasNext()) return scanner.next(); } return null; } - public static Integer[] versionAsIntArray(String version) { - String[] parts = version.split("[\\.\\-]+"); - if (version.startsWith("v")) { - version = version.substring(1); - } - ArrayList array = new ArrayList<>(); - for (String s : parts) { - try { - int part = 0; - if (s.startsWith("pre")) { - part = Integer.parseInt(s.substring(3)) - 1000000; - } else if (s.startsWith("rc")) { - part = Integer.parseInt(s.substring(3)) - 500000; - } else { - part = Integer.parseInt(s); - } - array.add(part); - } catch (NumberFormatException ignored) { - - } - } - return array.toArray(new Integer[0]); + public static int compareVersions(String ver1, String ver2) { + return new Version(ver2).compareTo(new Version(ver1)); } - /** - * @return -1 if ver1 is greater, 0 if equal, 1 if ver2 i greater - */ - public static int compareVersions(String ver1, String ver2) { - Integer[] ver1int = versionAsIntArray(ver1); - Integer[] ver2int = versionAsIntArray(ver2); - for (int i = 0; i < ver1int.length || i < ver2int.length; i++) { - int ver1part = 0; - int ver2part = 0; - if (i < ver1int.length) { - ver1part = ver1int[i]; - } - if (i < ver2int.length) { - ver2part = ver2int[i]; - } - if (ver1part > ver2part) { - return -1; - } - if (ver1part < ver2part) { - return 1; + private static class Version implements Comparable { + private final List version; + + public Version(String versionString) { + if (versionString.startsWith("v")) versionString = versionString.substring(1); + + String[] parts = versionString.split("[.-]+"); + ArrayList array = new ArrayList<>(); + for (String s : parts) { + try { + int part; + if (s.startsWith("pre")) { + part = Integer.parseInt(s.substring(3)) - 1000000; + } else if (s.startsWith("rc")) { + part = Integer.parseInt(s.substring(3)) - 500000; + } else { + s = s.replaceAll("\\w", ""); + part = Integer.parseInt(s); + } + array.add(part); + } catch (NumberFormatException ignored) { + array.add(0); + } } + version = Collections.unmodifiableList(array); + } + + @Override + public int compareTo(@Nonnull Version other) { + for (int i = 0; i < version.size() || i < other.version.size(); i++) { + int versionIntThis = 0; + int versionIntOther = 0; + if (i < version.size()) versionIntThis = version.get(i); + if (i < other.version.size()) versionIntOther = other.version.get(i); + if (versionIntThis < versionIntOther) return -1; + if (versionIntThis > versionIntOther) return 1; + } + return 0; } - return 0; } } \ No newline at end of file