From 0717d667bdd78887a678794898491739169181b8 Mon Sep 17 00:00:00 2001 From: Tom Roman Date: Sun, 11 Jul 2021 15:56:40 +0100 Subject: [PATCH] Add Update Util --- .../java/network/palace/show/ShowPlugin.java | 6 ++++ .../network/palace/show/utils/UpdateUtil.java | 32 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/main/java/network/palace/show/utils/UpdateUtil.java diff --git a/src/main/java/network/palace/show/ShowPlugin.java b/src/main/java/network/palace/show/ShowPlugin.java index 0b7f4ca..8e60ead 100644 --- a/src/main/java/network/palace/show/ShowPlugin.java +++ b/src/main/java/network/palace/show/ShowPlugin.java @@ -12,6 +12,7 @@ import network.palace.show.npc.SoftNPCManager; import network.palace.show.utils.BuildUtil; import network.palace.show.utils.FileUtil; +import network.palace.show.utils.UpdateUtil; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -98,6 +99,11 @@ public void onEnable() { Metrics metrics = new Metrics(this, pluginId); + new UpdateUtil(this, 94141).getVersion(v -> { + if (!this.getDescription().getVersion().equalsIgnoreCase(v)) { + Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "A New update is available for Show! It is always recommended that you upgrade! Link: https://www.spigotmc.org/resources/show-make-huge-spectaculars-in-minecraft.94141/"); + } + }); Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Show is now enabled!"); Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Huge shoutout to Legobuilder0813 for making this work for The Palace Network. Time to let your awesome code shine"); diff --git a/src/main/java/network/palace/show/utils/UpdateUtil.java b/src/main/java/network/palace/show/utils/UpdateUtil.java new file mode 100644 index 0000000..da62e42 --- /dev/null +++ b/src/main/java/network/palace/show/utils/UpdateUtil.java @@ -0,0 +1,32 @@ +package network.palace.show.utils; +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.util.Consumer; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Scanner; + +public class UpdateUtil { + + private JavaPlugin plugin; + private int resourceId; + + public UpdateUtil(JavaPlugin plugin, int resourceId) { + this.plugin = plugin; + this.resourceId = resourceId; + } + + public void getVersion(final Consumer consumer) { + Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> { + try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) { + if (scanner.hasNext()) { + consumer.accept(scanner.next()); + } + } catch (IOException exception) { + this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage()); + } + }); + } +}