LibsDisguises/src/me/libraryaddict/disguise/utilities/UpdateChecker.java
2014-06-29 17:07:59 +12:00

59 lines
1.9 KiB
Java

package me.libraryaddict.disguise.utilities;
import java.io.BufferedReader;
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) {
String version = getSpigotVersion();
if (version != null) {
if (checkHigher(currentVersion, version)) {
latestVersion = version;
}
}
}
public String getLatestVersion() {
return latestVersion;
}
/**
* Asks spigot for the version
*/
private String getSpigotVersion() {
try {
HttpURLConnection con = (HttpURLConnection) new URL("http://www.spigotmc.org/api/general.php").openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.getOutputStream().write(
("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=81").getBytes("UTF-8"));
String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
if (version.length() <= 7) {
return version;
}
} catch (Exception ex) {
System.out.print("[LibsDisguises] Failed to check for a update on spigot. Now checking bukkit..");
}
return null;
}
private 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;
}
}