2016-06-18 17:16:32 +02:00
|
|
|
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;
|
|
|
|
|
2016-11-28 22:10:55 +01:00
|
|
|
public class UpdateChecker {
|
2016-06-18 17:16:32 +02:00
|
|
|
|
|
|
|
private String latestVersion;
|
|
|
|
|
2016-11-28 22:10:55 +01:00
|
|
|
private boolean checkHigher(String currentVersion, String newVersion) {
|
2016-06-18 17:16:32 +02:00
|
|
|
String current = toReadable(currentVersion);
|
|
|
|
String newVers = toReadable(newVersion);
|
|
|
|
return current.compareTo(newVers) < 0;
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:10:55 +01:00
|
|
|
public void checkUpdate(String currentVersion) {
|
2016-06-18 17:16:32 +02:00
|
|
|
String version = getSpigotVersion();
|
|
|
|
|
|
|
|
if (version == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!checkHigher(currentVersion, version))
|
|
|
|
return;
|
|
|
|
|
|
|
|
latestVersion = version;
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:10:55 +01:00
|
|
|
public String getLatestVersion() {
|
2016-06-18 17:16:32 +02:00
|
|
|
return latestVersion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asks spigot for the version
|
|
|
|
*/
|
2016-11-28 22:10:55 +01:00
|
|
|
private String getSpigotVersion() {
|
|
|
|
try {
|
2016-06-18 17:16:32 +02:00
|
|
|
HttpURLConnection con = (HttpURLConnection) new URL("http://www.spigotmc.org/api/general.php").openConnection();
|
|
|
|
con.setDoOutput(true);
|
|
|
|
con.setRequestMethod("POST");
|
|
|
|
con.getOutputStream().write(
|
2016-11-28 22:10:55 +01:00
|
|
|
("key=98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4&resource=32453").getBytes("UTF-8"));
|
2016-06-18 17:16:32 +02:00
|
|
|
String version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
|
2017-08-09 21:16:10 +02:00
|
|
|
if (version.length() <= 10) {
|
2016-06-18 17:16:32 +02:00
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|
2016-11-28 22:10:55 +01:00
|
|
|
catch (Exception ex) {
|
2016-06-18 17:16:32 +02:00
|
|
|
System.out.print("[LibsDisguises] Failed to check for a update on spigot.");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-11-28 22:10:55 +01:00
|
|
|
private String toReadable(String version) {
|
2016-06-18 17:16:32 +02:00
|
|
|
String[] split = Pattern.compile(".", Pattern.LITERAL).split(version.replace("v", ""));
|
|
|
|
version = "";
|
2016-11-28 22:10:55 +01:00
|
|
|
for (String s : split) {
|
2016-06-18 17:16:32 +02:00
|
|
|
version += String.format("%4s", s);
|
|
|
|
}
|
|
|
|
return version;
|
|
|
|
}
|
|
|
|
}
|