From 30726108a42e38441077ab737158497a378a71ec Mon Sep 17 00:00:00 2001 From: Denis Istratov Date: Tue, 25 Jul 2023 03:07:21 +0300 Subject: [PATCH] 1.0.1 SNAPSHOT Update checker --- build.gradle | 3 +- src/main/java/me/redtea/nodropx/NoDropX.java | 23 ++++++++----- .../redtea/nodropx/command/NoDropCommand.java | 2 +- .../me/redtea/nodropx/util/UpdateChecker.java | 32 +++++++++++++++++++ src/main/resources/config.yml | 3 +- src/main/resources/text/mm/messages.yml | 2 +- 6 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 src/main/java/me/redtea/nodropx/util/UpdateChecker.java diff --git a/build.gradle b/build.gradle index 7986f4e..1aeab6a 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'me.redtea' -version = '1.0.0-SNAPSHOT' +version = '1.0.1-SNAPSHOT' repositories { mavenCentral() @@ -37,7 +37,6 @@ dependencies { implementation "net.kyori:adventure-api:4.14.0" implementation "net.kyori:adventure-platform-bukkit:4.3.0" implementation files("libs/MiniMessages.jar") - compileOnly files("libs/nodropxforgebridge-1.1.jar") implementation "dev.triumphteam:triumph-gui:3.1.2" } diff --git a/src/main/java/me/redtea/nodropx/NoDropX.java b/src/main/java/me/redtea/nodropx/NoDropX.java index 4c13f7e..d5c7e43 100644 --- a/src/main/java/me/redtea/nodropx/NoDropX.java +++ b/src/main/java/me/redtea/nodropx/NoDropX.java @@ -34,8 +34,7 @@ import me.redtea.nodropx.service.respawn.RespawnService; import me.redtea.nodropx.service.respawn.impl.RespawnServiceImpl; import me.redtea.nodropx.service.storage.StorageService; import me.redtea.nodropx.service.storage.impl.StorageServiceImpl; -import me.redtea.nodropxforgebridge.ApiProvider; -import me.redtea.nodropxforgebridge.Nodropxforgebridge; +import me.redtea.nodropx.util.UpdateChecker; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -46,7 +45,6 @@ import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.util.Arrays; -import java.util.Collection; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; @@ -132,13 +130,22 @@ public final class NoDropX extends JavaPlugin { commandManager.register(new NoDropCommand(messages, noDropAPI, guiFacade, this)); bStatsInit(); printCredits(); - - mod(); + checkUpdates(); } - //todo remove - private void mod() { - Nodropxforgebridge.noDropAPI = s -> getAPI().getCapacitySlots(s); + + private void checkUpdates() { + if(!(getConfig().contains("checkForUpdates") && getConfig().getBoolean("checkForUpdates"))) { + return; + } + new UpdateChecker(this, 111485).getVersion(version -> { + if (this.getDescription().getVersion().equals(version)) { + getLogger().info("There is not a new update available."); + } else { + getLogger().info("There is a new update available: " + version); + getLogger().info(". Download it from https://www.spigotmc.org/resources/nodropx.111485/"); + } + }); } diff --git a/src/main/java/me/redtea/nodropx/command/NoDropCommand.java b/src/main/java/me/redtea/nodropx/command/NoDropCommand.java index 2ccf562..a2cf585 100644 --- a/src/main/java/me/redtea/nodropx/command/NoDropCommand.java +++ b/src/main/java/me/redtea/nodropx/command/NoDropCommand.java @@ -31,7 +31,7 @@ public class NoDropCommand extends CommandBase { } @SubCommand("give") - @Permission("nodropx.give") + @Permission("R") @Completion({"#players", "#materials", "#range:1-64"}) public void onGive(final CommandSender sender, final Player player, diff --git a/src/main/java/me/redtea/nodropx/util/UpdateChecker.java b/src/main/java/me/redtea/nodropx/util/UpdateChecker.java new file mode 100644 index 0000000..53952d8 --- /dev/null +++ b/src/main/java/me/redtea/nodropx/util/UpdateChecker.java @@ -0,0 +1,32 @@ +package me.redtea.nodropx.util; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Scanner; +import java.util.function.Consumer; + +public class UpdateChecker { + private final JavaPlugin plugin; + private final int resourceId; + + public UpdateChecker(JavaPlugin plugin, int resourceId) { + this.plugin = plugin; + this.resourceId = resourceId; + } + + public void getVersion(final Consumer consumer) { + Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> { + try (InputStream is = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scann = new Scanner(is)) { + if (scann.hasNext()) { + consumer.accept(scann.next()); + } + } catch (IOException e) { + plugin.getLogger().info("Unable to check for updates: " + e.getMessage()); + } + }); + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 15f7d0a..ac87002 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,2 +1,3 @@ forceUseLegacy: false -forceUseMD5Colors: false \ No newline at end of file +forceUseMD5Colors: false +checkForUpdates: true \ No newline at end of file diff --git a/src/main/resources/text/mm/messages.yml b/src/main/resources/text/mm/messages.yml index 8362d99..e7fc57e 100644 --- a/src/main/resources/text/mm/messages.yml +++ b/src/main/resources/text/mm/messages.yml @@ -14,7 +14,7 @@ noExists: "[!] This player doesnt exists!" gui: title: "Personal storage" frame: - name: "&7" + name: "" material: GLASS_PANE prev: name: "Previous"