2013-11-22 21:10:20 +01:00
|
|
|
package me.libraryaddict.disguise.utilities;
|
2013-05-31 10:55:55 +02:00
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
2014-01-18 21:18:58 +01:00
|
|
|
import java.net.URLConnection;
|
2013-05-31 10:55:55 +02:00
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2014-01-18 21:18:58 +01:00
|
|
|
import org.json.simple.JSONArray;
|
|
|
|
import org.json.simple.JSONObject;
|
|
|
|
import org.json.simple.JSONValue;
|
|
|
|
|
2013-05-31 10:55:55 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-01-18 21:18:58 +01:00
|
|
|
public void checkUpdate(String currentVersion) {
|
|
|
|
String version = getSpigotVersion();
|
|
|
|
if (version == null) {
|
|
|
|
version = getBukkitVersion();
|
|
|
|
}
|
|
|
|
if (checkHigher(currentVersion, version)) {
|
2013-05-31 10:55:55 +02:00
|
|
|
latestVersion = version;
|
2014-01-18 21:18:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asks bukkit for the version
|
|
|
|
*/
|
|
|
|
private String getBukkitVersion() {
|
|
|
|
try {
|
|
|
|
URLConnection conn = new URL("https://api.curseforge.com/servermods/files?projectIds=72490").openConnection();
|
|
|
|
conn.addRequestProperty("User-Agent", "Lib's Disguises Update Checker");
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
|
JSONArray array = (JSONArray) JSONValue.parse(reader.readLine());
|
|
|
|
if (!array.isEmpty()) {
|
|
|
|
JSONObject latest = (JSONObject) array.get(array.size() - 1);
|
|
|
|
String version = (String) latest.get("name");
|
|
|
|
version = version.substring(version.lastIndexOf(" ") + 1);
|
|
|
|
if (version.length() <= 7) {
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.out.print("[LibsDisguises] Failed to check for a update on bukkit.");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
2013-05-31 10:55:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String getLatestVersion() {
|
|
|
|
return latestVersion;
|
|
|
|
}
|
|
|
|
|
2014-01-18 21:18:58 +01:00
|
|
|
/**
|
|
|
|
* Asks spigot for the version
|
|
|
|
*/
|
|
|
|
private String getSpigotVersion() {
|
2013-05-31 10:55:55 +02:00
|
|
|
try {
|
|
|
|
HttpURLConnection con = (HttpURLConnection) new URL("http://www.spigotmc.org/api/general.php").openConnection();
|
|
|
|
con.setDoOutput(true);
|
|
|
|
con.setRequestMethod("POST");
|
2014-01-18 21:18:58 +01:00
|
|
|
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;
|
2014-01-07 18:44:36 +01:00
|
|
|
}
|
2014-01-18 21:18:58 +01:00
|
|
|
} catch (Exception ex) {
|
|
|
|
System.out.print("[LibsDisguises] Failed to check for a update on spigot. Now checking bukkit..");
|
2013-05-31 10:55:55 +02:00
|
|
|
ex.printStackTrace();
|
|
|
|
}
|
2014-01-18 21:18:58 +01:00
|
|
|
return null;
|
2013-05-31 10:55:55 +02:00
|
|
|
}
|
|
|
|
|
2014-01-18 21:18:58 +01:00
|
|
|
private String toReadable(String version) {
|
2013-05-31 10:55:55 +02:00
|
|
|
String[] split = Pattern.compile(".", Pattern.LITERAL).split(version.replace("v", ""));
|
|
|
|
version = "";
|
|
|
|
for (String s : split)
|
|
|
|
version += String.format("%4s", s);
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|