diff --git a/src/main/java/io/github/_4drian3d/unsignedvelocity/updatechecker/UpdateChecker.java b/src/main/java/io/github/_4drian3d/unsignedvelocity/updatechecker/UpdateChecker.java index 5db7138..b45af7c 100644 --- a/src/main/java/io/github/_4drian3d/unsignedvelocity/updatechecker/UpdateChecker.java +++ b/src/main/java/io/github/_4drian3d/unsignedvelocity/updatechecker/UpdateChecker.java @@ -30,18 +30,16 @@ private static Version getLatestVersion() throws Exception { } } - private static boolean isUpdateAvailable(Version latestVersion) { - return Version.parse(Constants.VERSION).isEqualOrLowerThan(latestVersion); - } - public void checkForUpdates() { try { + Version currentVersion = Version.parse(Constants.VERSION); Version latestVersion = getLatestVersion(); - if (isUpdateAvailable(latestVersion)) { - logger.info("There is an update available for UnSignedVelocity: {} (Your version: {})", latestVersion, Constants.VERSION); + if (currentVersion.isLowerThan(latestVersion)) { + logger.info(miniMessage().deserialize("<#6892bd>There is an update available for UnSignedVelocity<#6892bd>: " + latestVersion + " (Your version: " + currentVersion + ")")); + } else if (currentVersion.isHigherThan(latestVersion)) { + logger.info(miniMessage().deserialize("<#6892bd>You are using a development build of UnSignedVelocity<#6892bd>: " + currentVersion + " (Latest release: " + latestVersion + ")")); } else { - logger.info(miniMessage().deserialize( - "<#6892bd>You are using the latest version of UnSignedVelocity")); + logger.info(miniMessage().deserialize("<#6892bd>You are using the latest version of UnSignedVelocity")); } } catch (Exception e) { logger.error("Cannot check for updates", e); diff --git a/src/main/java/io/github/_4drian3d/unsignedvelocity/updatechecker/Version.java b/src/main/java/io/github/_4drian3d/unsignedvelocity/updatechecker/Version.java index f4f0f6b..f758a93 100644 --- a/src/main/java/io/github/_4drian3d/unsignedvelocity/updatechecker/Version.java +++ b/src/main/java/io/github/_4drian3d/unsignedvelocity/updatechecker/Version.java @@ -1,19 +1,26 @@ package io.github._4drian3d.unsignedvelocity.updatechecker; -public record Version(int major, int minor, int patch) implements Comparable { +import java.util.Optional; + +public record Version(int major, int minor, int patch, Optional preRelease) implements Comparable { public static Version parse(String version) { - String[] parts = version.split("\\."); - if (parts.length != 3) { + String[] parts = version.split("[.-]"); + if (parts.length != 3 && parts.length != 4) { throw new IllegalArgumentException("Invalid version format"); } int major = Integer.parseInt(parts[0]); int minor = Integer.parseInt(parts[1]); int patch = Integer.parseInt(parts[2]); - return new Version(major, minor, patch); + Optional preRelease = parts.length == 4 ? Optional.of(parts[3]) : Optional.empty(); + return new Version(major, minor, patch, preRelease); + } + + public boolean isHigherThan(Version version) { + return this.compareTo(version) > 0; } - public boolean isEqualOrLowerThan(Version version) { + public boolean isLowerThan(Version version) { return this.compareTo(version) < 0; } @@ -25,11 +32,50 @@ public int compareTo(Version other) { if (this.minor != other.minor) { return Integer.compare(this.minor, other.minor); } - return Integer.compare(this.patch, other.patch); + if (this.patch != other.patch) { + return Integer.compare(this.patch, other.patch); + } + if (this.preRelease.isPresent() && other.preRelease.isPresent()) { + return comparePreRelease(this.preRelease.get(), other.preRelease.get()); + } else if (this.preRelease.isPresent()) { + return -1; + } else if (other.preRelease.isPresent()) { + return 1; + } + return 0; + } + + private int comparePreRelease(String preRelease1, String preRelease2) { + String[] parts1 = preRelease1.split("\\."); + String[] parts2 = preRelease2.split("\\."); + + for (int i = 0; i < Math.min(parts1.length, parts2.length); i++) { + int comparison = comparePreReleasePart(parts1[i], parts2[i]); + if (comparison != 0) { + return comparison; + } + } + + return Integer.compare(parts1.length, parts2.length); + } + + private int comparePreReleasePart(String part1, String part2) { + boolean isNumeric1 = part1.matches("\\d+"); + boolean isNumeric2 = part2.matches("\\d+"); + + if (isNumeric1 && isNumeric2) { + return Integer.compare(Integer.parseInt(part1), Integer.parseInt(part2)); + } else if (isNumeric1) { + return -1; + } else if (isNumeric2) { + return 1; + } else { + return part1.compareTo(part2); + } } @Override public String toString() { - return major + "." + minor + "." + patch; + return major + "." + minor + "." + patch + preRelease.map(s -> "-" + s).orElse(""); } }