Bring version checker back up to par.

This commit is contained in:
garbagemule 2014-01-22 15:34:48 +01:00
parent 88340beaf6
commit 2d3ba801d5
2 changed files with 72 additions and 14 deletions

View File

@ -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:

View File

@ -21,23 +21,81 @@ 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() {
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(updater.getLatestName() + " is now available!");
Messenger.info("Your version: v" + plugin.getDescription().getVersion());
Messenger.info(message);
} else if (player.isOnline()) {
Messenger.tell(player, updater.getLatestName() + " is now available!");
Messenger.tell(player, "Your version: v" + plugin.getDescription().getVersion());
Messenger.tell(player, message);
}
}
});
}
}
});
}, (player == null) ? 0 : 60); // Message player after login spam
}
public static void shutdown() {