2013-02-18 22:54:47 +01:00
|
|
|
package com.gmail.nossr50.util;
|
|
|
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.UnknownHostException;
|
|
|
|
|
|
|
|
import org.json.simple.JSONObject;
|
|
|
|
import org.json.simple.parser.JSONParser;
|
|
|
|
import org.json.simple.parser.ParseException;
|
|
|
|
|
|
|
|
import com.gmail.nossr50.mcMMO;
|
|
|
|
import com.gmail.nossr50.config.Config;
|
|
|
|
|
2013-03-01 06:52:01 +01:00
|
|
|
public class UpdateChecker {
|
|
|
|
private UpdateChecker() {}
|
2013-02-18 22:54:47 +01:00
|
|
|
|
|
|
|
public static boolean updateAvailable() throws Exception {
|
|
|
|
String checkType = "release";
|
|
|
|
if (Config.getInstance().getPreferBeta()) {
|
|
|
|
checkType = "latest";
|
|
|
|
}
|
|
|
|
String version = mcMMO.p.getDescription().getVersion();
|
2013-03-01 06:52:01 +01:00
|
|
|
URL url = new URL("http://api.bukget.org/api2/bukkit/plugin/mcmmo/" + checkType);
|
2013-02-18 22:54:47 +01:00
|
|
|
InputStreamReader isr;
|
|
|
|
try {
|
|
|
|
isr = new InputStreamReader(url.openStream());
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
catch (UnknownHostException e) {
|
2013-02-18 22:54:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
String newVersion;
|
|
|
|
try {
|
|
|
|
JSONParser jp = new JSONParser();
|
|
|
|
Object o = jp.parse(isr);
|
|
|
|
|
|
|
|
if (!(o instanceof JSONObject)) {
|
|
|
|
isr.close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject jo = (JSONObject) o;
|
|
|
|
jo = (JSONObject) jo.get("versions");
|
|
|
|
newVersion = (String) jo.get("version");
|
|
|
|
|
|
|
|
String[] oldTokens = version.replaceAll("(?i)(-)(.+?)(-)", "").split("[.]|[b]");
|
|
|
|
String[] newTokens = newVersion.replaceAll("(?i)(-)(.+?)(-)", "").split("[.]|[b]");
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
Integer newVer = Integer.parseInt(newTokens[i]);
|
|
|
|
Integer oldVer = Integer.parseInt(oldTokens[i]);
|
|
|
|
if (oldVer < newVer) {
|
|
|
|
isr.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2013-03-01 06:52:01 +01:00
|
|
|
}
|
|
|
|
catch (ParseException e) {
|
2013-02-18 22:54:47 +01:00
|
|
|
isr.close();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|