Only advise updates for newer versions

This commit is contained in:
PikaMug 2024-06-01 06:05:26 -04:00
parent d3ee8c6c4b
commit 2a48e147d1
3 changed files with 30 additions and 3 deletions

View File

@ -233,7 +233,7 @@ public class BukkitQuestsPlugin extends JavaPlugin implements Quests {
// 12 - Attempt to check for updates
new BukkitUpdateChecker(this, 3711).getVersion(version -> {
if (!getDescription().getVersion().split("-")[0].equalsIgnoreCase(version)) {
if (BukkitUpdateChecker.compareVersions(getDescription().getVersion().split("-")[0], version)) {
getLogger().info(ChatColor.DARK_GREEN + BukkitLang.get("updateTo").replace("<version>",
version).replace("<url>", ChatColor.AQUA + getDescription().getWebsite()));
}

View File

@ -872,7 +872,7 @@ public class BukkitPlayerListener implements Listener {
final Player player = event.getPlayer();
if (player.hasPermission("quests.admin.update")) {
new BukkitUpdateChecker(plugin, 3711).getVersion(version -> {
if (!plugin.getDescription().getVersion().split("-")[0].equalsIgnoreCase(version)) {
if (BukkitUpdateChecker.compareVersions(plugin.getDescription().getVersion().split("-")[0], version)) {
event.getPlayer().sendMessage(ChatColor.GRAY + "[" + ChatColor.YELLOW + "Quests" + ChatColor.GRAY
+ "] " + ChatColor.GREEN + BukkitLang.get(player, "updateTo").replace("<version>",
version).replace("<url>", ChatColor.DARK_AQUA + "" + ChatColor.UNDERLINE

View File

@ -37,10 +37,37 @@ public class BukkitUpdateChecker {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (IOException e) {
} catch (final IOException e) {
this.plugin.getLogger().info("Update check failed: " + e.getMessage());
}
});
}
}
/**
* Compares two valid semantic versions, i.e. 5.0.3
*
* @param currentVersion Current resource version
* @param compareVersion Resource version to compare against
* @return true if compared version is higher
*/
public static boolean compareVersions(final String currentVersion, final String compareVersion) {
if (currentVersion == null || compareVersion == null) {
return false;
}
final String[] currentParts = currentVersion.split("\\.");
final String[] compareParts = compareVersion.split("\\.");
final int length = Math.max(currentParts.length, compareParts.length);
if (length > 3) {
return false;
}
for (int i = 0; i < length; i++) {
final int currentPart = Integer.parseInt(currentParts[i]);
final int comparePart = Integer.parseInt(compareParts[i]);
if (comparePart > currentPart) {
return true;
}
}
return false;
}
}