diff --git a/pom.xml b/pom.xml index bf4a816..2ba81c6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.artillexstudios AxSellwands - 1.4.0 + 1.5.0 jar AxSellwands @@ -156,7 +156,7 @@ com.artillexstudios.axapi axapi - 1.4.242 + 1.4.259 compile all diff --git a/src/main/java/com/artillexstudios/axsellwands/AxSellwands.java b/src/main/java/com/artillexstudios/axsellwands/AxSellwands.java index a4d1ee9..66eca86 100644 --- a/src/main/java/com/artillexstudios/axsellwands/AxSellwands.java +++ b/src/main/java/com/artillexstudios/axsellwands/AxSellwands.java @@ -9,7 +9,7 @@ import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.settings.loader.LoaderSettings; import com.artillexstudios.axapi.libs.boostedyaml.boostedyaml.settings.updater.UpdaterSettings; import com.artillexstudios.axapi.nms.NMSHandlers; -import com.artillexstudios.axapi.utils.FastFieldAccessor; +import com.artillexstudios.axapi.reflection.FastFieldAccessor; import com.artillexstudios.axapi.utils.FeatureFlags; import com.artillexstudios.axapi.utils.MessageUtils; import com.artillexstudios.axapi.utils.StringUtils; @@ -23,6 +23,7 @@ import com.artillexstudios.axsellwands.utils.CommandMessages; import com.artillexstudios.axsellwands.utils.FileUtils; import com.artillexstudios.axsellwands.utils.NumberUtils; +import com.artillexstudios.axsellwands.utils.UpdateNotifier; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; @@ -129,6 +130,8 @@ public void enable() { getServer().getPluginManager().registerEvents(new InventoryClickListener(), this); Bukkit.getConsoleSender().sendMessage(StringUtils.formatToString("&#FF5500[AxSellwands] Loaded plugin!")); + + if (CONFIG.getBoolean("update-notifier.enabled", true)) new UpdateNotifier(this, 5725); } public void updateFlags() { diff --git a/src/main/java/com/artillexstudios/axsellwands/utils/UpdateNotifier.java b/src/main/java/com/artillexstudios/axsellwands/utils/UpdateNotifier.java new file mode 100644 index 0000000..03c9aa1 --- /dev/null +++ b/src/main/java/com/artillexstudios/axsellwands/utils/UpdateNotifier.java @@ -0,0 +1,101 @@ +package com.artillexstudios.axsellwands.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.axsellwands.AxSellwands.CONFIG; +import static com.artillexstudios.axsellwands.AxSellwands.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/lang.yml b/src/main/resources/lang.yml index 92e8333..a17e757 100644 --- a/src/main/resources/lang.yml +++ b/src/main/resources/lang.yml @@ -20,7 +20,7 @@ nothing-sold: "&#FFAA55Nothing in the container is sellable!" cooldown: "&#FFAA55You must wait another &f%time% seconds &#FFAA55before using this sellwand again!" -no-permission: "&#FFAA55You don't have a permission to use a sellwant on this container!" +no-permission: "&#FFAA55You don't have a permission to use a sellwand on this container!" disallowed-container: "&#FFAA55This sellwand can't be used on this type of container!" @@ -73,5 +73,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: "&#FFAA00There is a new version of the plugin available! &#DDDDDD(&#FFFFFFcurrent: &#FF0000%current% &#DDDDDD| &#FFFFFFlatest: �FF00%latest%&#DDDDDD)" + # do not change this -version: 2 \ No newline at end of file +version: 3 \ No newline at end of file