2013-11-22 20:52:15 +01:00
|
|
|
package me.libraryaddict.disguise.utils;
|
2013-05-31 10:55:55 +02:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
public class UpdateChecker {
|
|
|
|
private String latestVersion;
|
|
|
|
|
|
|
|
private boolean checkHigher(String currentVersion, String newVersion) {
|
|
|
|
String current = toReadable(currentVersion);
|
|
|
|
String newVers = toReadable(newVersion);
|
|
|
|
return current.compareTo(newVers) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void checkUpdate(String currentVersion) throws Exception {
|
2013-06-02 06:10:37 +02:00
|
|
|
String version = getVersion("98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4", 81);
|
2013-05-31 10:55:55 +02:00
|
|
|
if (checkHigher(currentVersion, version))
|
|
|
|
latestVersion = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getLatestVersion() {
|
|
|
|
return latestVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getVersion(String key, int resourceId) {
|
|
|
|
String version = null;
|
|
|
|
try {
|
|
|
|
HttpURLConnection con = (HttpURLConnection) new URL("http://www.spigotmc.org/api/general.php").openConnection();
|
|
|
|
con.setDoOutput(true);
|
|
|
|
con.setRequestMethod("POST");
|
|
|
|
con.getOutputStream().write(("key=" + key + "&resource=" + resourceId).getBytes("UTF-8"));
|
|
|
|
version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toReadable(String version) {
|
|
|
|
String[] split = Pattern.compile(".", Pattern.LITERAL).split(version.replace("v", ""));
|
|
|
|
version = "";
|
|
|
|
for (String s : split)
|
|
|
|
version += String.format("%4s", s);
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|