Bring version checker back up to par.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
name: MobArena
|
name: MobArena
|
||||||
author: garbagemule
|
author: garbagemule
|
||||||
main: com.garbagemule.MobArena.MobArena
|
main: com.garbagemule.MobArena.MobArena
|
||||||
version: 0.96.2.1
|
version: 0.96.2.2
|
||||||
softdepend: [Multiverse-Core,Towny,Heroes,MagicSpells,Vault]
|
softdepend: [Multiverse-Core,Towny,Heroes,MagicSpells,Vault]
|
||||||
commands:
|
commands:
|
||||||
ma:
|
ma:
|
||||||
|
|||||||
@@ -21,25 +21,83 @@ public class VersionChecker
|
|||||||
final Updater cache = updater;
|
final Updater cache = updater;
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
// Check for updates
|
|
||||||
if (cache.getResult() == UpdateResult.UPDATE_AVAILABLE) {
|
if (cache.getResult() == UpdateResult.UPDATE_AVAILABLE) {
|
||||||
// Notify on the main thread
|
final String latest = getLatestVersionString();
|
||||||
Bukkit.getScheduler().runTask(plugin, new Runnable() {
|
final String current = plugin.getDescription().getVersion();
|
||||||
public void run() {
|
|
||||||
if (player == null) {
|
if (latest == null || current == null) {
|
||||||
Messenger.info(updater.getLatestName() + " is now available!");
|
String msg = "Update checker failed. Please check manually!";
|
||||||
Messenger.info("Your version: v" + plugin.getDescription().getVersion());
|
message(plugin, player, msg);
|
||||||
} else if (player.isOnline()) {
|
}
|
||||||
Messenger.tell(player, updater.getLatestName() + " is now available!");
|
|
||||||
Messenger.tell(player, "Your version: v" + plugin.getDescription().getVersion());
|
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() {
|
public static void shutdown() {
|
||||||
updater = null;
|
updater = null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user