From 7097311284eb8704ac1de09e30bb63b2f1018522 Mon Sep 17 00:00:00 2001 From: HappyAreaBean Date: Tue, 9 Jan 2024 23:27:01 +0800 Subject: [PATCH] feat: updater checker and update notify on join --- .../happyareabean/sjm/SimpleJoinMessage.java | 51 +++++++++++++++++++ .../sjm/listener/UpdateNotifyListener.java | 51 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/main/java/cc/happyareabean/sjm/listener/UpdateNotifyListener.java diff --git a/src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java b/src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java index 3b1a19a..aff5e5e 100644 --- a/src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java +++ b/src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java @@ -3,6 +3,7 @@ import cc.happyareabean.sjm.commands.SJMCommand; import cc.happyareabean.sjm.config.SJMConfig; import cc.happyareabean.sjm.listener.PlayerJoinListener; +import cc.happyareabean.sjm.listener.UpdateNotifyListener; import cc.happyareabean.sjm.utils.AdventureWebEditorAPI; import cc.happyareabean.sjm.utils.Constants; import lombok.Getter; @@ -11,13 +12,20 @@ import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bstats.bukkit.Metrics; import org.bukkit.plugin.java.JavaPlugin; +import org.inventivetalent.update.spiget.SpigetUpdate; +import org.inventivetalent.update.spiget.UpdateCallback; +import org.inventivetalent.update.spiget.comparator.VersionComparator; import revxrsal.commands.bukkit.BukkitCommandHandler; import java.io.File; import java.net.URI; +import java.util.Arrays; public class SimpleJoinMessage extends JavaPlugin { + public static String NEXT_VERSION = ""; + public static String DOWNLOAD_URL = "https://www.spigotmc.org/resources/103413/"; + @Getter private static SimpleJoinMessage instance; @Getter public static final MiniMessage MINIMESSAGE = MiniMessage.miniMessage(); @Getter public static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacy('&'); @@ -36,6 +44,7 @@ public void onEnable() { getLogger().info("Registering listener..."); getServer().getPluginManager().registerEvents(new PlayerJoinListener(), this); + getServer().getPluginManager().registerEvents(new UpdateNotifyListener(), this); getLogger().info("Registering commands..."); commandHandler = BukkitCommandHandler.create(this); @@ -63,6 +72,48 @@ public void onEnable() { new Metrics(this, 15462); getLogger().info("SimpleJoinMessage version " + getDescription().getVersion() + " has been successfully enabled!"); + + checkUpdate(); + } + + public void checkUpdate() { + String version = getDescription().getVersion(); + if (version.endsWith("-SNAPSHOT")) { + Arrays.asList( + "******************************************", + "You are currently using development build of SimpleJoinMessage!", + "Please report issues here: https://go.happyareabean.cc/sjm/issues", + "******************************************" + ).forEach(s -> getLogger().warning(s)); + return; + } + + SpigetUpdate updater = new SpigetUpdate(this, 103413); + updater.setVersionComparator(VersionComparator.SEM_VER); + updater.checkForUpdate(new UpdateCallback() { + @Override + public void updateAvailable(String newVersion, String downloadUrl, boolean hasDirectDownload) { + NEXT_VERSION = newVersion; + + Arrays.asList( + "******************************************", + "", + "There is a new version of SimpleJoinMessage available!", + "", + "Your Version: " + version, + "New Version: " + NEXT_VERSION, + "", + "Download at " + downloadUrl, + "", + "******************************************" + ).forEach(s -> getLogger().warning(s)); + } + + @Override + public void upToDate() { + getLogger().info(String.format("SimpleJoinMessage is up to date! (%s)", version)); + } + }); } public void checkSupportedPlugin() { diff --git a/src/main/java/cc/happyareabean/sjm/listener/UpdateNotifyListener.java b/src/main/java/cc/happyareabean/sjm/listener/UpdateNotifyListener.java new file mode 100644 index 0000000..c410b19 --- /dev/null +++ b/src/main/java/cc/happyareabean/sjm/listener/UpdateNotifyListener.java @@ -0,0 +1,51 @@ +package cc.happyareabean.sjm.listener; + +import cc.happyareabean.sjm.SimpleJoinMessage; +import cc.happyareabean.sjm.utils.Constants; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.event.ClickEvent; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import revxrsal.commands.annotation.Named; + +import static net.kyori.adventure.text.Component.newline; +import static net.kyori.adventure.text.Component.space; +import static net.kyori.adventure.text.Component.text; +import static net.kyori.adventure.text.format.NamedTextColor.WHITE; + +public class UpdateNotifyListener implements Listener { + + @EventHandler + public void onJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); + if (!player.hasPermission("sjm.admin")) return; + if (SimpleJoinMessage.NEXT_VERSION.isEmpty()) return; + + Audience aPlayer = SimpleJoinMessage.getAdventure().player(player); + aPlayer.sendMessage(Component.textOfChildren( + Constants.PREFIX, + text("New version", WHITE), + space(), + text(SimpleJoinMessage.NEXT_VERSION, NamedTextColor.YELLOW, TextDecoration.BOLD), + space(), + text("is available!", WHITE), + newline(), + Constants.PREFIX, + text("You are currently running"), + space(), + text(SimpleJoinMessage.getInstance().getDescription().getVersion()), + text("."), + newline(), + Constants.PREFIX, + text("Click here to download update!", NamedTextColor.GOLD, TextDecoration.BOLD) + .clickEvent(ClickEvent.openUrl(SimpleJoinMessage.DOWNLOAD_URL)) + ).color(NamedTextColor.GRAY)); + + } + +}