From 01dd3ef5875781b5ed79ed59bb4990dfcfdd4499 Mon Sep 17 00:00:00 2001 From: libraryaddict Date: Sun, 19 Jan 2014 09:18:58 +1300 Subject: [PATCH] Updated the update checker so it uses bukkit as well (ew) --- .../disguise/utilities/ReflectionManager.java | 2 +- .../disguise/utilities/UpdateChecker.java | 62 +++++++++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java index a85c0768..86850aaf 100644 --- a/src/me/libraryaddict/disguise/utilities/ReflectionManager.java +++ b/src/me/libraryaddict/disguise/utilities/ReflectionManager.java @@ -51,8 +51,8 @@ public class ReflectionManager { } private static String bukkitVersion = Bukkit.getServer().getClass().getName().split("\\.")[3]; - private static Class itemClass; private static Method damageAndIdleSoundMethod; + private static Class itemClass; static { for (Method method : getNmsClass("EntityLiving").getDeclaredMethods()) { diff --git a/src/me/libraryaddict/disguise/utilities/UpdateChecker.java b/src/me/libraryaddict/disguise/utilities/UpdateChecker.java index b32b452b..efc2c3d9 100644 --- a/src/me/libraryaddict/disguise/utilities/UpdateChecker.java +++ b/src/me/libraryaddict/disguise/utilities/UpdateChecker.java @@ -1,12 +1,16 @@ package me.libraryaddict.disguise.utilities; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; +import java.net.URLConnection; import java.util.regex.Pattern; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; + public class UpdateChecker { private String latestVersion; @@ -16,34 +20,66 @@ public class UpdateChecker { return current.compareTo(newVers) < 0; } - public void checkUpdate(String currentVersion) throws Exception { - String version = getVersion("98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4", 81); - if (checkHigher(currentVersion, version)) + public void checkUpdate(String currentVersion) { + String version = getSpigotVersion(); + if (version == null) { + version = getBukkitVersion(); + } + if (checkHigher(currentVersion, version)) { latestVersion = version; + } + } + + /** + * 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; } public String getLatestVersion() { return latestVersion; } - private String getVersion(String key, int resourceId) { - String version = null; + /** + * 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=" + key + "&resource=" + resourceId).getBytes("UTF-8")); - version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine(); - if (version.length() > 7) { - version = null; + 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 (IOException ex) { + } catch (Exception ex) { + System.out.print("[LibsDisguises] Failed to check for a update on spigot. Now checking bukkit.."); ex.printStackTrace(); } - return version; + return null; } - public String toReadable(String version) { + private String toReadable(String version) { String[] split = Pattern.compile(".", Pattern.LITERAL).split(version.replace("v", "")); version = ""; for (String s : split)