diff --git a/build.gradle b/build.gradle index 8a4fd66..e42dcd4 100644 --- a/build.gradle +++ b/build.gradle @@ -66,6 +66,7 @@ dependencies { implementation project(":API") implementation 'com.bgsoftware.common.reflection:ReflectionUtils:b3' + implementation 'com.bgsoftware.common.updater:Updater:b1' implementation 'com.bgsoftware.common.config:CommentedConfiguration:b1' implementation 'com.bgsoftware.common.dependencies:DependenciesManager:b2' implementation 'com.bgsoftware.common.nmsloader:NMSLoader:b3' diff --git a/src/main/java/com/bgsoftware/wildloaders/Updater.java b/src/main/java/com/bgsoftware/wildloaders/Updater.java deleted file mode 100644 index 3804143..0000000 --- a/src/main/java/com/bgsoftware/wildloaders/Updater.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.bgsoftware.wildloaders; - -import javax.net.ssl.HttpsURLConnection; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.net.URL; - -public final class Updater { - - private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin(); - - private static String latestVersion, versionDescription; - - static{ - setLatestVersion(); - } - - //Just so no one would be able to call the constructor - private Updater(){} - - public static boolean isOutdated(){ - return !plugin.getDescription().getVersion().startsWith(latestVersion); - } - - public static String getLatestVersion(){ - return latestVersion; - } - - static String getVersionDescription(){ - return versionDescription; - } - - private static void setLatestVersion(){ - try { - HttpsURLConnection connection = (HttpsURLConnection) new URL("https://bg-software.com/versions.json").openConnection(); - - connection.setRequestMethod("GET"); - connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)"); - connection.setDoInput(true); - - try(InputStream reader = connection.getInputStream()){ - Class jsonObjectClass, gsonClass; - - try{ - jsonObjectClass = Class.forName("net.minecraft.util.com.google.gson.JsonObject"); - gsonClass = Class.forName("net.minecraft.util.com.google.gson.Gson"); - }catch(ClassNotFoundException ex){ - jsonObjectClass = Class.forName("com.google.gson.JsonObject"); - gsonClass = Class.forName("com.google.gson.Gson"); - } - - Object jsonObject = gsonClass.getMethod("fromJson", Reader.class, Class.class) - .invoke(gsonClass.newInstance(), new InputStreamReader(reader), jsonObjectClass); - - Object jsonElement = jsonObjectClass.getMethod("get", String.class).invoke(jsonObject, "wildloaders"); - Object plugin = jsonElement.getClass().getMethod("getAsJsonObject").invoke(jsonElement); - - Object versionElement = plugin.getClass().getMethod("get", String.class).invoke(plugin, "version"); - Object descriptionElement = plugin.getClass().getMethod("get", String.class).invoke(plugin, "description"); - - latestVersion = (String) versionElement.getClass().getMethod("getAsString").invoke(versionElement); - versionDescription = (String) descriptionElement.getClass().getMethod("getAsString").invoke(descriptionElement); - } - } catch(Exception ex){ - //Something went wrong... - latestVersion = plugin.getDescription().getVersion(); - } - } - -} diff --git a/src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java b/src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java index ab2eee7..08fdfe8 100644 --- a/src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java +++ b/src/main/java/com/bgsoftware/wildloaders/WildLoadersPlugin.java @@ -4,6 +4,7 @@ import com.bgsoftware.common.dependencies.DependenciesManager; import com.bgsoftware.common.nmsloader.INMSLoader; import com.bgsoftware.common.nmsloader.NMSHandlersFactory; import com.bgsoftware.common.nmsloader.NMSLoadException; +import com.bgsoftware.common.updater.Updater; import com.bgsoftware.wildloaders.api.WildLoaders; import com.bgsoftware.wildloaders.api.WildLoadersAPI; import com.bgsoftware.wildloaders.command.CommandsHandler; @@ -25,6 +26,8 @@ import java.lang.reflect.Field; public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders { + private final Updater updater = new Updater(this, "wildloaders"); + private static WildLoadersPlugin plugin; private SettingsHandler settingsHandler; @@ -77,10 +80,10 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders { Locale.reload(); - if (Updater.isOutdated()) { + if (updater.isOutdated()) { log(""); - log("A new version is available (v" + Updater.getLatestVersion() + ")!"); - log("Version's description: \"" + Updater.getVersionDescription() + "\""); + log("A new version is available (v" + updater.getLatestVersion() + ")!"); + log("Version's description: \"" + updater.getVersionDescription() + "\""); log(""); } @@ -150,6 +153,10 @@ public final class WildLoadersPlugin extends JavaPlugin implements WildLoaders { return dataHandler; } + public Updater getUpdater() { + return updater; + } + public static void log(String message) { plugin.getLogger().info(message); } diff --git a/src/main/java/com/bgsoftware/wildloaders/listeners/PlayersListener.java b/src/main/java/com/bgsoftware/wildloaders/listeners/PlayersListener.java index 9ff28fe..1f14a16 100644 --- a/src/main/java/com/bgsoftware/wildloaders/listeners/PlayersListener.java +++ b/src/main/java/com/bgsoftware/wildloaders/listeners/PlayersListener.java @@ -1,6 +1,5 @@ package com.bgsoftware.wildloaders.listeners; -import com.bgsoftware.wildloaders.Updater; import com.bgsoftware.wildloaders.WildLoadersPlugin; import com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC; import com.bgsoftware.wildloaders.utils.threads.Executor; @@ -37,9 +36,9 @@ public final class PlayersListener implements Listener { ChatColor.GRAY + "This server is using WildLoaders v" + plugin.getDescription().getVersion()), 5L); } - if (e.getPlayer().isOp() && Updater.isOutdated()) { + if (e.getPlayer().isOp() && plugin.getUpdater().isOutdated()) { Executor.sync(() -> e.getPlayer().sendMessage(ChatColor.GREEN + "" + ChatColor.BOLD + "WildLoaders" + - ChatColor.GRAY + " A new version is available (v" + Updater.getLatestVersion() + ")!"), 20L); + ChatColor.GRAY + " A new version is available (v" + plugin.getUpdater().getLatestVersion() + ")!"), 20L); } }