From 46ac53683dd39d434fca2f18201f76f7acd450a2 Mon Sep 17 00:00:00 2001 From: BenceX100 <52270269+BenceX100@users.noreply.github.com> Date: Sun, 23 Jun 2024 19:22:16 +0200 Subject: [PATCH] 1.6.0 --- pom.xml | 6 +- .../com/artillexstudios/axtrade/AxTrade.java | 3 + .../axtrade/commands/Commands.java | 2 +- .../axtrade/utils/ItemBuilderUtil.java | 2 +- .../axtrade/utils/ShulkerUtils.java | 2 +- .../axtrade/utils/UpdateNotifier.java | 101 ++++++++++++++++++ src/main/resources/config.yml | 9 +- src/main/resources/lang.yml | 4 +- 8 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/artillexstudios/axtrade/utils/UpdateNotifier.java diff --git a/pom.xml b/pom.xml index 30509c7..d9715b2 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.artillexstudios AxTrade - 1.5.1 + 1.6.0 jar AxTrade @@ -112,7 +112,7 @@ com.artillexstudios.axapi axapi - 1.4.242 + 1.4.259 compile all @@ -125,7 +125,7 @@ dev.triumphteam triumph-gui - 3.1.7 + 3.1.10 compile diff --git a/src/main/java/com/artillexstudios/axtrade/AxTrade.java b/src/main/java/com/artillexstudios/axtrade/AxTrade.java index 862fbe5..89e9f36 100644 --- a/src/main/java/com/artillexstudios/axtrade/AxTrade.java +++ b/src/main/java/com/artillexstudios/axtrade/AxTrade.java @@ -18,6 +18,7 @@ import com.artillexstudios.axtrade.listeners.PlayerInteractEntityListener; import com.artillexstudios.axtrade.listeners.TradeListeners; import com.artillexstudios.axtrade.trade.TradeTicker; import com.artillexstudios.axtrade.utils.NumberUtils; +import com.artillexstudios.axtrade.utils.UpdateNotifier; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; @@ -73,5 +74,7 @@ public final class AxTrade extends AxPlugin { Commands.registerCommand(); Bukkit.getConsoleSender().sendMessage(StringUtils.formatToString("�FFDD[AxTrade] Loaded plugin!")); + + if (CONFIG.getBoolean("update-notifier.enabled", true)) new UpdateNotifier(this, 5943); } } \ No newline at end of file diff --git a/src/main/java/com/artillexstudios/axtrade/commands/Commands.java b/src/main/java/com/artillexstudios/axtrade/commands/Commands.java index 9ba10e4..dac6d61 100644 --- a/src/main/java/com/artillexstudios/axtrade/commands/Commands.java +++ b/src/main/java/com/artillexstudios/axtrade/commands/Commands.java @@ -1,7 +1,7 @@ package com.artillexstudios.axtrade.commands; import com.artillexstudios.axapi.nms.NMSHandlers; -import com.artillexstudios.axapi.utils.FastFieldAccessor; +import com.artillexstudios.axapi.reflection.FastFieldAccessor; import com.artillexstudios.axapi.utils.StringUtils; import com.artillexstudios.axtrade.AxTrade; import com.artillexstudios.axtrade.hooks.HookManager; diff --git a/src/main/java/com/artillexstudios/axtrade/utils/ItemBuilderUtil.java b/src/main/java/com/artillexstudios/axtrade/utils/ItemBuilderUtil.java index 7a5dc47..2b767c5 100644 --- a/src/main/java/com/artillexstudios/axtrade/utils/ItemBuilderUtil.java +++ b/src/main/java/com/artillexstudios/axtrade/utils/ItemBuilderUtil.java @@ -1,7 +1,7 @@ package com.artillexstudios.axtrade.utils; import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.block.implementation.Section; -import com.artillexstudios.axapi.utils.ClassUtils; +import com.artillexstudios.axapi.reflection.ClassUtils; import com.artillexstudios.axapi.utils.ItemBuilder; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; diff --git a/src/main/java/com/artillexstudios/axtrade/utils/ShulkerUtils.java b/src/main/java/com/artillexstudios/axtrade/utils/ShulkerUtils.java index 81c9c23..cff9d50 100644 --- a/src/main/java/com/artillexstudios/axtrade/utils/ShulkerUtils.java +++ b/src/main/java/com/artillexstudios/axtrade/utils/ShulkerUtils.java @@ -1,6 +1,6 @@ package com.artillexstudios.axtrade.utils; -import com.artillexstudios.axapi.utils.ClassUtils; +import com.artillexstudios.axapi.reflection.ClassUtils; import org.bukkit.block.Barrel; import org.bukkit.block.ShulkerBox; import org.bukkit.inventory.ItemStack; diff --git a/src/main/java/com/artillexstudios/axtrade/utils/UpdateNotifier.java b/src/main/java/com/artillexstudios/axtrade/utils/UpdateNotifier.java new file mode 100644 index 0000000..ffb5ad1 --- /dev/null +++ b/src/main/java/com/artillexstudios/axtrade/utils/UpdateNotifier.java @@ -0,0 +1,101 @@ +package com.artillexstudios.axtrade.utils; + +import com.artillexstudios.axapi.AxPlugin; +import com.artillexstudios.axapi.scheduler.Scheduler; +import com.artillexstudios.axapi.utils.NumberUtils; +import com.artillexstudios.axapi.utils.StringUtils; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.jetbrains.annotations.Nullable; + +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.HashMap; + +import static com.artillexstudios.axtrade.AxTrade.CONFIG; +import static com.artillexstudios.axtrade.AxTrade.LANG; +import static java.time.temporal.ChronoUnit.SECONDS; + +public class UpdateNotifier implements Listener { + private final int id; + private final String current; + private final AxPlugin instance; + private String latest = null; + private boolean newest = true; + + public UpdateNotifier(AxPlugin instance, int id) { + this.id = id; + this.current = instance.getDescription().getVersion(); + this.instance = instance; + + instance.getServer().getPluginManager().registerEvents(this, instance); + + long time = 30L * 60L * 20L; + Scheduler.get().runAsyncTimer(t -> { + this.latest = readVersion(); + this.newest = isLatest(current); + + if (latest == null || newest) return; + Bukkit.getConsoleSender().sendMessage(getMessage()); + t.cancel(); + }, 50L, time); + } + + @EventHandler + public void onJoin(PlayerJoinEvent event) { + if (latest == null || newest) return; + if (!CONFIG.getBoolean("update-notifier.on-join", true)) return; + if (!event.getPlayer().hasPermission(instance.getName().toLowerCase() + ".update-notify")) return; + Scheduler.get().runLaterAsync(t -> { + event.getPlayer().sendMessage(getMessage()); + }, 50L); + } + + private String getMessage() { + HashMap map = new HashMap<>(); + map.put("%current%", current); + map.put("%latest%", latest); + return StringUtils.formatToString(CONFIG.getString("prefix") + LANG.getString("update-notifier"), map); + } + + @Nullable + private String readVersion() { + try { + final HttpClient client = HttpClient.newHttpClient(); + final HttpRequest request = HttpRequest.newBuilder() + .uri(new URI("https://api.polymart.org/v1/getResourceInfoSimple/?resource_id=" + id + "&key=version")) + .timeout(Duration.of(10, SECONDS)) + .GET() + .build(); + + final HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + return response.body().toString(); + } catch (Exception ex) { + return null; + } + } + + public String getLatest() { + return latest; + } + + public boolean isLatest(String current) { + return getWeight(latest) <= getWeight(current); + } + + private int getWeight(String version) { + if (version == null) return 0; + String[] s = version.split("\\."); + if (!NumberUtils.isInt(s[0]) || !NumberUtils.isInt(s[1]) || !NumberUtils.isInt(s[2])) return 0; + int res = 0; + res += Integer.parseInt(s[0]) * 1000000; + res += Integer.parseInt(s[1]) * 1000; + res += Integer.parseInt(s[2]); + return res; + } +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 3da3eea..37be0a0 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -52,5 +52,12 @@ blacklisted-items: material: "barrier" name-contains: "Banned item's name" +# should be plugin notify you if there is a new update? +update-notifier: + # if enabled, it will display the message in the console + enabled: true + # if enabled, it will broadcast the update message to all players who have the .update-notify permission + on-join: true + # do not change this -version: 2 \ No newline at end of file +version: 3 \ No newline at end of file diff --git a/src/main/resources/lang.yml b/src/main/resources/lang.yml index 42618f0..3729c11 100644 --- a/src/main/resources/lang.yml +++ b/src/main/resources/lang.yml @@ -97,5 +97,7 @@ commands: invalid-player: "&#FF0000The player &#BB0000%player% &#FF0000can not be found!" invalid-selector: "&#FF0000You can not use this selector in this command!" +update-notifier: "&#AAFFCCThere is a new version of the plugin available! &#DDDDDD(&#FFFFFFcurrent: &#FF0000%current% &#DDDDDD| &#FFFFFFlatest: �FF00%latest%&#DDDDDD)" + # do not change this -version: 3 \ No newline at end of file +version: 4 \ No newline at end of file