From 4a33fae7b584f639e6af0d4ada60aaf01e7fbed8 Mon Sep 17 00:00:00 2001 From: jameslfc19 Date: Thu, 16 Apr 2020 12:40:40 +0100 Subject: [PATCH] Update Checker Added update checker and config to enable/disable it. --- .../minecraft/chests/ChestsPlusPlus.java | 28 +++++++ .../minecraft/chests/Settings.java | 44 +++++++++++ .../chests/versionchecker/UpdateCheck.java | 74 +++++++++++++++++++ .../versionchecker/VersionResponse.java | 7 ++ 4 files changed, 153 insertions(+) create mode 100644 src/main/java/com/jamesdpeters/minecraft/chests/Settings.java create mode 100644 src/main/java/com/jamesdpeters/minecraft/chests/versionchecker/UpdateCheck.java create mode 100644 src/main/java/com/jamesdpeters/minecraft/chests/versionchecker/VersionResponse.java diff --git a/src/main/java/com/jamesdpeters/minecraft/chests/ChestsPlusPlus.java b/src/main/java/com/jamesdpeters/minecraft/chests/ChestsPlusPlus.java index 3287399..f25998c 100644 --- a/src/main/java/com/jamesdpeters/minecraft/chests/ChestsPlusPlus.java +++ b/src/main/java/com/jamesdpeters/minecraft/chests/ChestsPlusPlus.java @@ -6,8 +6,11 @@ import com.jamesdpeters.minecraft.chests.listeners.HopperListener; import com.jamesdpeters.minecraft.chests.listeners.InventoryListener; import com.jamesdpeters.minecraft.chests.serialize.InventoryStorage; import com.jamesdpeters.minecraft.chests.serialize.LinkedChest; +import com.jamesdpeters.minecraft.chests.versionchecker.UpdateCheck; import fr.minuskube.inv.InventoryManager; import org.bstats.bukkit.Metrics; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.plugin.java.JavaPlugin; @@ -28,6 +31,8 @@ public class ChestsPlusPlus extends JavaPlugin { int pluginId = 7166; Metrics metrics = new Metrics(this, pluginId); + Settings.initConfig(this); + PLUGIN = this; new RemoteChestCommand().register(this); getServer().getPluginManager().registerEvents(new ChestLinkListener(),this); @@ -39,6 +44,29 @@ public class ChestsPlusPlus extends JavaPlugin { INVENTORY_MANAGER = new InventoryManager(this); INVENTORY_MANAGER.init(); + if(Settings.isUpdateCheckEnabled()) { + String SPIGOT_URL = "https://www.spigotmc.org/resources/chests-chest-linking-hopper-filtering-remote-chests-menus.71355/"; + UpdateCheck + .of(this) + .resourceId(71355) + .currentVersion("1.15 v1.2.2") + .handleResponse((versionResponse, version) -> { + switch (versionResponse) { + case FOUND_NEW: + getLogger().warning("New version of the plugin has been found: " + version); + getLogger().warning("Download at: https://www.spigotmc.org/resources/chests-chest-linking-hopper-filtering-remote-chests-menus.71355/"); + Bukkit.broadcastMessage(ChatColor.RED + "[Chests++] New version of the plugin was found: " + version); + break; + case LATEST: + getLogger().info("Plugin is up to date! Thank you for supporting Chests++!"); + break; + case UNAVAILABLE: + Bukkit.broadcastMessage("Unable to perform an update check."); + } + }) + .check(); + } + getLogger().info("Chests++ enabled!"); } diff --git a/src/main/java/com/jamesdpeters/minecraft/chests/Settings.java b/src/main/java/com/jamesdpeters/minecraft/chests/Settings.java new file mode 100644 index 0000000..36ba8e0 --- /dev/null +++ b/src/main/java/com/jamesdpeters/minecraft/chests/Settings.java @@ -0,0 +1,44 @@ +package com.jamesdpeters.minecraft.chests; + +import org.bukkit.configuration.Configuration; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.plugin.Plugin; + +import java.util.concurrent.TimeUnit; + +public class Settings { + + private static String CHECK_UPDATE = "update-checker"; + + private static Settings cf; + private FileConfiguration configuration; + private Plugin plugin; + + public static void initConfig(Plugin plugin){ + cf = new Settings(); + cf.plugin = plugin; + cf.configuration = plugin.getConfig(); + + //DEFAULT VALUES + cf.configuration.addDefault(CHECK_UPDATE,true); + + cf.configuration.options().copyDefaults(true); + cf.plugin.saveConfig(); + } + + private static void save(){ + cf.plugin.saveConfig();; + } + + public static void reloadConfig(){ + cf.configuration = cf.plugin.getConfig(); + } + + /** + * GETTERS AND SETTERS + */ + public static boolean isUpdateCheckEnabled() { + return cf.configuration.getBoolean(CHECK_UPDATE); + } +} diff --git a/src/main/java/com/jamesdpeters/minecraft/chests/versionchecker/UpdateCheck.java b/src/main/java/com/jamesdpeters/minecraft/chests/versionchecker/UpdateCheck.java new file mode 100644 index 0000000..b25f852 --- /dev/null +++ b/src/main/java/com/jamesdpeters/minecraft/chests/versionchecker/UpdateCheck.java @@ -0,0 +1,74 @@ +package com.jamesdpeters.minecraft.chests.versionchecker; + +import com.google.common.base.Preconditions; +import com.google.common.io.Resources; +import com.google.common.net.HttpHeaders; +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; + +import javax.net.ssl.HttpsURLConnection; +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.Objects; +import java.util.function.BiConsumer; + +public class UpdateCheck { + + private static final String SPIGOT_URL = "https://api.spigotmc.org/legacy/update.php?resource=%d"; + + private final JavaPlugin javaPlugin; + + private String currentVersion; + private int resourceId = -1; + private BiConsumer versionResponse; + + private UpdateCheck(JavaPlugin javaPlugin) { + this.javaPlugin = Objects.requireNonNull(javaPlugin, "javaPlugin"); + this.currentVersion = javaPlugin.getDescription().getVersion(); + } + + public static UpdateCheck of(JavaPlugin javaPlugin) { + return new UpdateCheck(javaPlugin); + } + + public UpdateCheck currentVersion(String currentVersion) { + this.currentVersion = currentVersion; + return this; + } + + public UpdateCheck resourceId(int resourceId) { + this.resourceId = resourceId; + return this; + } + + public UpdateCheck handleResponse(BiConsumer versionResponse) { + this.versionResponse = versionResponse; + return this; + } + + public void check() { + Objects.requireNonNull(this.javaPlugin, "javaPlugin"); + Objects.requireNonNull(this.currentVersion, "currentVersion"); + Preconditions.checkState(this.resourceId != -1, "resource id not set"); + Objects.requireNonNull(this.versionResponse, "versionResponse"); + + Bukkit.getScheduler().runTaskAsynchronously(this.javaPlugin, () -> { + try { + HttpURLConnection httpURLConnection = (HttpsURLConnection) new URL(String.format(SPIGOT_URL, this.resourceId)).openConnection(); + httpURLConnection.setRequestMethod("GET"); + httpURLConnection.setRequestProperty(HttpHeaders.USER_AGENT, "Mozilla/5.0"); + + String fetchedVersion = Resources.toString(httpURLConnection.getURL(), Charset.defaultCharset()); + + boolean latestVersion = fetchedVersion.equalsIgnoreCase(this.currentVersion); + + Bukkit.getScheduler().runTask(this.javaPlugin, () -> this.versionResponse.accept(latestVersion ? VersionResponse.LATEST : VersionResponse.FOUND_NEW, latestVersion ? this.currentVersion : fetchedVersion)); + } catch (IOException exception) { + exception.printStackTrace(); + Bukkit.getScheduler().runTask(this.javaPlugin, () -> this.versionResponse.accept(VersionResponse.UNAVAILABLE, null)); + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/com/jamesdpeters/minecraft/chests/versionchecker/VersionResponse.java b/src/main/java/com/jamesdpeters/minecraft/chests/versionchecker/VersionResponse.java new file mode 100644 index 0000000..cd472d8 --- /dev/null +++ b/src/main/java/com/jamesdpeters/minecraft/chests/versionchecker/VersionResponse.java @@ -0,0 +1,7 @@ +package com.jamesdpeters.minecraft.chests.versionchecker; + +public enum VersionResponse { + LATEST, + FOUND_NEW, + UNAVAILABLE +}