diff --git a/resources/plugin.yml b/resources/plugin.yml index 4ec5dab..6e0591f 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -1,7 +1,7 @@ name: MobArena author: garbagemule main: com.garbagemule.MobArena.MobArena -version: 0.96.2.1 +version: 0.96.2.2 softdepend: [Multiverse-Core,Towny,Heroes,MagicSpells,Vault] commands: ma: diff --git a/src/com/garbagemule/MobArena/util/VersionChecker.java b/src/com/garbagemule/MobArena/util/VersionChecker.java index 102efea..0ac69cc 100644 --- a/src/com/garbagemule/MobArena/util/VersionChecker.java +++ b/src/com/garbagemule/MobArena/util/VersionChecker.java @@ -21,25 +21,83 @@ public class VersionChecker final Updater cache = updater; Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { public void run() { - // Check for updates if (cache.getResult() == UpdateResult.UPDATE_AVAILABLE) { - // Notify on the main thread - Bukkit.getScheduler().runTask(plugin, new Runnable() { - public void run() { - if (player == null) { - Messenger.info(updater.getLatestName() + " is now available!"); - Messenger.info("Your version: v" + plugin.getDescription().getVersion()); - } else if (player.isOnline()) { - Messenger.tell(player, updater.getLatestName() + " is now available!"); - Messenger.tell(player, "Your version: v" + plugin.getDescription().getVersion()); - } - } - }); + final String latest = getLatestVersionString(); + final String current = plugin.getDescription().getVersion(); + + if (latest == null || current == null) { + String msg = "Update checker failed. Please check manually!"; + message(plugin, player, msg); + } + + else if (isUpdateAvailable(latest, current)) { + String msg1 = "MobArena v" + latest + " is now available!"; + String msg2 = "Your version: v" + current; + message(plugin, player, msg1, msg2); + } } } }); } + private static String getLatestVersionString() { + String latestName = updater.getLatestName(); + if (!latestName.matches("MobArena v.*")) { + return null; + } + return latestName.substring("MobArena v".length()); + } + + private static boolean isUpdateAvailable(String latestVersion, String currentVersion) { + // Split into major.minor(.patch(.build)) + String[] latestParts = latestVersion.split("\\."); + String[] currentParts = currentVersion.split("\\."); + + // Figure out how many numbers to compare + int parts = Math.max(latestParts.length, currentParts.length); + + // Check each part + for (int i = 0; i < parts; i++) { + int latest = getPart(latestParts, i); + int current = getPart(currentParts, i); + + // Return early if current is more recent + if (current > latest) { + return false; + } + + // And also if latest is more recent + if (latest > current) { + return true; + } + } + + // Otherwise, we're completely up-to-date! + return false; + } + + private static int getPart(String[] parts, int i) { + // Out of bounds or not an int? Bail with 0. + if (i >= parts.length || !parts[i].matches("[0-9]+")) { + return 0; + } + return Integer.parseInt(parts[i]); + } + + private static void message(MobArena plugin, final Player player, final String... messages) { + Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { + public void run() { + for (String message : messages) { + if (player == null) { + Messenger.info(message); + } else if (player.isOnline()) { + Messenger.tell(player, message); + } + } + } + }, (player == null) ? 0 : 60); // Message player after login spam + } + public static void shutdown() { updater = null; }