Tweak a few things on the UpdateChecker.

This commit is contained in:
GJ 2013-05-31 10:07:37 -04:00
parent f81a763d6c
commit d43ae17bd2

View File

@ -15,32 +15,26 @@ public class UpdateChecker {
private UpdateChecker() {} private UpdateChecker() {}
public static boolean updateAvailable() throws Exception { public static boolean updateAvailable() throws Exception {
String checkType = "release"; String checkType = Config.getInstance().getPreferBeta() ? "latest" : "release";
if (Config.getInstance().getPreferBeta()) {
checkType = "latest";
}
String version = mcMMO.p.getDescription().getVersion(); String version = mcMMO.p.getDescription().getVersion();
URL url = new URL("http://api.bukget.org/api2/bukkit/plugin/mcmmo/" + checkType);
InputStreamReader isr; InputStreamReader isr;
try { try {
isr = new InputStreamReader(url.openStream()); isr = new InputStreamReader(new URL("http://api.bukget.org/api2/bukkit/plugin/mcmmo/" + checkType).openStream());
} }
catch (UnknownHostException e) { catch (UnknownHostException e) {
return false; return false;
} }
String newVersion;
try { try {
JSONParser jp = new JSONParser(); Object o = new JSONParser().parse(isr);
Object o = jp.parse(isr);
if (!(o instanceof JSONObject)) { if (!(o instanceof JSONObject)) {
return false; return false;
} }
JSONObject jo = (JSONObject) o; JSONObject versions = (JSONObject) ((JSONObject) o).get("versions");
jo = (JSONObject) jo.get("versions"); String newVersion = (String) versions.get("version");
newVersion = (String) jo.get("version");
String[] oldTokens = version.replaceAll("(?i)(-)(.+?)(-)", "-").split("[.]|-b"); String[] oldTokens = version.replaceAll("(?i)(-)(.+?)(-)", "-").split("[.]|-b");
String[] newTokens = newVersion.replaceAll("(?i)(-)(.+?)(-)", "-").split("[.]|-b"); String[] newTokens = newVersion.replaceAll("(?i)(-)(.+?)(-)", "-").split("[.]|-b");
@ -48,16 +42,19 @@ public class UpdateChecker {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
Integer newVer = Integer.parseInt(newTokens[i]); Integer newVer = Integer.parseInt(newTokens[i]);
Integer oldVer; Integer oldVer;
try { try {
oldVer = Integer.parseInt(oldTokens[i]); oldVer = Integer.parseInt(oldTokens[i]);
} }
catch (NumberFormatException e) { catch (NumberFormatException e) {
oldVer = 0; oldVer = 0;
} }
if (oldVer < newVer) { if (oldVer < newVer) {
return true; return true;
} }
} }
return false; return false;
} }
catch (ParseException e) { catch (ParseException e) {