From 99c577c36134619b5a0f914f4a32137ed548fe85 Mon Sep 17 00:00:00 2001 From: carm Date: Mon, 7 Feb 2022 03:27:59 +0800 Subject: [PATCH] =?UTF-8?q?[2.4.0]=20=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=9B=B4=E6=96=B0=E6=9C=BA=E5=88=B6=EF=BC=8C?= =?UTF-8?q?=E5=B9=B6=E5=85=81=E8=AE=B8=E5=BC=80=E5=85=B3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README-en.md | 2 + README.md | 4 +- pom.xml | 18 +- .../java/cc/carm/plugin/userprefix/Main.java | 208 +++++++++--------- .../configuration/PluginConfig.java | 94 ++++---- .../userprefix/hooker/UpdateChecker.java | 35 +++ src/main/resources/config.yml | 5 + src/main/resources/en_US/config.yml | 3 + 8 files changed, 219 insertions(+), 150 deletions(-) create mode 100644 src/main/java/cc/carm/plugin/userprefix/hooker/UpdateChecker.java diff --git a/README-en.md b/README-en.md index d98633e..d0ffcca 100644 --- a/README-en.md +++ b/README-en.md @@ -1,5 +1,7 @@ ![BANNER](.documentation/images/banner.png) +README LANGUAGES [ [中文](README.md) | [**English**](README-en.md) ] + # UserPrefix Plugin [![CodeFactor](https://www.codefactor.io/repository/github/carmjos/userprefix/badge?s=b76fec1f64726b5f19989aace6adb5f85fdab840)](https://www.codefactor.io/repository/github/carmjos/userprefix) diff --git a/README.md b/README.md index 839b20d..54b2596 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ ![BANNER](.documentation/images/banner.png) +README LANGUAGES [ [**中文**](README.md) | [English](README-en.md) ] + # 用户前缀系统插件 [![CodeFactor](https://www.codefactor.io/repository/github/carmjos/userprefix/badge?s=b76fec1f64726b5f19989aace6adb5f85fdab840)](https://www.codefactor.io/repository/github/carmjos/userprefix) @@ -13,8 +15,6 @@ 本插件基于Spigot实现,**理论上支持全版本**。 -The **English version** of the introduction is [here](README-en.md). - > 本插件已在 [MCBBS](https://www.mcbbs.net/forum.php?mod=viewthread&tid=1261503) 与 [SpigotMC](https://www.spigotmc.org/resources/userprefix-hex-color-support-all-version.96277/) 上发布。 ## 示例 diff --git a/pom.xml b/pom.xml index 4158a9f..07fdc1b 100644 --- a/pom.xml +++ b/pom.xml @@ -13,7 +13,7 @@ cc.carm.plugin userprefix - 2.3.3 + 2.4.0 UserPrefix 轻便、高效、实时的用户前缀系统。 @@ -127,6 +127,13 @@ compile + + cc.carm.lib + githubreleases4j + 1.3.1 + compile + + junit junit @@ -209,9 +216,16 @@ org.bstats - cc.carm.plugin.userprefix.bstats + + cc.carm.lib.githubreleases4j + cc.carm.plugin.userprefix.lib.github + + + org.json + cc.carm.plugin.userprefix.lib.json + diff --git a/src/main/java/cc/carm/plugin/userprefix/Main.java b/src/main/java/cc/carm/plugin/userprefix/Main.java index 42ff7ab..786edbf 100644 --- a/src/main/java/cc/carm/plugin/userprefix/Main.java +++ b/src/main/java/cc/carm/plugin/userprefix/Main.java @@ -3,6 +3,7 @@ package cc.carm.plugin.userprefix; import cc.carm.plugin.userprefix.command.UserPrefixAdminCommand; import cc.carm.plugin.userprefix.command.UserPrefixCommand; import cc.carm.plugin.userprefix.configuration.PluginConfig; +import cc.carm.plugin.userprefix.hooker.UpdateChecker; import cc.carm.plugin.userprefix.hooker.UserPrefixExpansion; import cc.carm.plugin.userprefix.listener.ChatListener; import cc.carm.plugin.userprefix.listener.UserListener; @@ -29,132 +30,139 @@ import org.jetbrains.annotations.Nullable; public class Main extends JavaPlugin { - private static Main instance; + private static Main instance; - @Override - public void onEnable() { - instance = this; - showPluginName(); - log(getName() + " " + getDescription().getVersion() + " &7开始加载..."); - long startTime = System.currentTimeMillis(); + @Override + public void onEnable() { + instance = this; + showPluginName(); + log(getName() + " " + getDescription().getVersion() + " &7开始加载..."); + long startTime = System.currentTimeMillis(); - log("加载配置文件..."); - ConfigManager.initConfig(); - PrefixManager.init(); + log("加载配置文件..."); + ConfigManager.initConfig(); + PrefixManager.init(); - log("注册指令..."); - registerCommand("UserPrefix", new UserPrefixCommand()); - registerCommand("UserPrefixAdmin", new UserPrefixAdminCommand()); + log("注册指令..."); + registerCommand("UserPrefix", new UserPrefixCommand()); + registerCommand("UserPrefixAdmin", new UserPrefixAdminCommand()); - log("注册监听器..."); - regListener(new UserListener()); - regListener(new ChatListener()); - ServiceManager.getService().getEventBus().subscribe(this, UserDataRecalculateEvent.class, UserNodeUpdateProcessor::process); + log("注册监听器..."); + regListener(new UserListener()); + regListener(new ChatListener()); + ServiceManager.getService().getEventBus().subscribe(this, UserDataRecalculateEvent.class, UserNodeUpdateProcessor::process); - if (MessageUtil.hasPlaceholderAPI()) { - log("注册变量..."); - new UserPrefixExpansion(getInstance()).register(); - } else { - log("未安装 PlaceholderAPI 不进行变量注册..."); - log("若您想使用变量进行前缀的显示,请安装PlaceholderAPI!"); - } + if (MessageUtil.hasPlaceholderAPI()) { + log("注册变量..."); + new UserPrefixExpansion(getInstance()).register(); + } else { + log("未安装 PlaceholderAPI 不进行变量注册..."); + log("若您想使用变量进行前缀的显示,请安装PlaceholderAPI!"); + } - if (PluginConfig.METRICS.get()) { - log("启用统计数据..."); - Metrics metrics = new Metrics(this, 13776); - metrics.addCustomChart(new SingleLineChart("active_prefixes", () -> PrefixManager.getPrefixes().size())); - metrics.addCustomChart(new SimplePie("custom_storage", () -> PluginConfig.CustomStorage.ENABLE.get() ? "ENABLE" : "DISABLE")); - metrics.addCustomChart(new SimplePie("lp_version", () -> ServiceManager.getService().getPluginMetadata().getVersion())); - metrics.addCustomChart(new SimplePie("papi_version", () -> { - Plugin plugin = Bukkit.getPluginManager().getPlugin("PlaceholderAPI"); - if (plugin == null) return "Not installed"; - else return plugin.getDescription().getVersion(); - })); - } + if (PluginConfig.METRICS.get()) { + log("启用统计数据..."); + Metrics metrics = new Metrics(this, 13776); + metrics.addCustomChart(new SingleLineChart("active_prefixes", () -> PrefixManager.getPrefixes().size())); + metrics.addCustomChart(new SimplePie("custom_storage", () -> PluginConfig.CustomStorage.ENABLE.get() ? "ENABLE" : "DISABLE")); + metrics.addCustomChart(new SimplePie("lp_version", () -> ServiceManager.getService().getPluginMetadata().getVersion())); + metrics.addCustomChart(new SimplePie("papi_version", () -> { + Plugin plugin = Bukkit.getPluginManager().getPlugin("PlaceholderAPI"); + if (plugin == null) return "Not installed"; + else return plugin.getDescription().getVersion(); + })); + } - log("加载完成 ,共耗时 " + (System.currentTimeMillis() - startTime) + " ms 。"); + if (PluginConfig.CHECK_UPDATE.get()) { + log("开始检查更新..."); + UpdateChecker.checkUpdate(getDescription().getVersion()); + } else { + log("已禁用检查更新,跳过。"); + } - showAD(); + log("加载完成 ,共耗时 " + (System.currentTimeMillis() - startTime) + " ms 。"); - if (Bukkit.getOnlinePlayers().size() > 0) { - Bukkit.getOnlinePlayers().forEach(UserManager::initPlayer); // 适配热重载 - } + showAD(); + + if (Bukkit.getOnlinePlayers().size() > 0) { + Bukkit.getOnlinePlayers().forEach(UserManager::initPlayer); // 适配热重载 + } - } + } - @Override - public void onDisable() { - showPluginName(); - log(getName() + " " + getDescription().getVersion() + " 开始卸载..."); - long startTime = System.currentTimeMillis(); + @Override + public void onDisable() { + showPluginName(); + log(getName() + " " + getDescription().getVersion() + " 开始卸载..."); + long startTime = System.currentTimeMillis(); - log("卸载监听器..."); - Bukkit.getServicesManager().unregisterAll(this); + log("卸载监听器..."); + Bukkit.getServicesManager().unregisterAll(this); - log("卸载完成 ,共耗时 " + (System.currentTimeMillis() - startTime) + " ms 。"); + log("卸载完成 ,共耗时 " + (System.currentTimeMillis() - startTime) + " ms 。"); - showAD(); + showAD(); - } + } - /** - * 注册监听器 - * - * @param listener 监听器 - */ - public static void regListener(Listener listener) { - Bukkit.getPluginManager().registerEvents(listener, getInstance()); - } + /** + * 注册监听器 + * + * @param listener 监听器 + */ + public static void regListener(Listener listener) { + Bukkit.getPluginManager().registerEvents(listener, getInstance()); + } - public static void log(String message) { - Bukkit.getConsoleSender().sendMessage(ColorParser.parse("[" + getInstance().getName() + "] " + message)); - } + public static void log(String message) { + Bukkit.getConsoleSender().sendMessage(ColorParser.parse("[" + getInstance().getName() + "] " + message)); + } - public static void debug(String message) { - if (PluginConfig.DEBUG.get()) { - log("[DEBUG] " + message); - } - } + public static void debug(String message) { + if (PluginConfig.DEBUG.get()) { + log("[DEBUG] " + message); + } + } - public static void error(String message) { - log("&c[ERROR] &r" + message); - } + public static void error(String message) { + log("&c[ERROR] &r" + message); + } - public static JavaPlugin getInstance() { - return instance; - } + public static JavaPlugin getInstance() { + return instance; + } - public static void registerCommand(String commandName, - @NotNull CommandExecutor executor) { - registerCommand(commandName, executor, null); - } + public static void registerCommand(String commandName, + @NotNull CommandExecutor executor) { + registerCommand(commandName, executor, null); + } - public static void registerCommand(String commandName, - @NotNull CommandExecutor executor, - @Nullable TabCompleter tabCompleter) { - PluginCommand command = Bukkit.getPluginCommand(commandName); - if (command == null) return; - command.setExecutor(executor); - if (tabCompleter != null) command.setTabCompleter(tabCompleter); - } + public static void registerCommand(String commandName, + @NotNull CommandExecutor executor, + @Nullable TabCompleter tabCompleter) { + PluginCommand command = Bukkit.getPluginCommand(commandName); + if (command == null) return; + command.setExecutor(executor); + if (tabCompleter != null) command.setTabCompleter(tabCompleter); + } - private void showPluginName() { - log("&b _ _ &f _____ __ _ "); - log("&b| | | | &f| __ \\ / _|(_) "); - log("&b| | | | ___ ___ _ __ &f| |__) |_ __ ___ | |_ _ __ __"); - log("&b| | | |/ __| / _ \\| '__|&f| ___/| '__|/ _ \\| _|| |\\ \\/ /"); - log("&b| |__| |\\__ \\| __/| | &f| | | | | __/| | | | > < "); - log("&b \\____/ |___/ \\___||_| &f|_| |_| \\___||_| |_|/_/\\_\\"); - log("&8 "); - log("&8> &f" + getDescription().getWebsite()); - } + private void showPluginName() { + log("&b _ _ &f _____ __ _ "); + log("&b| | | | &f| __ \\ / _|(_) "); + log("&b| | | | ___ ___ _ __ &f| |__) |_ __ ___ | |_ _ __ __"); + log("&b| | | |/ __| / _ \\| '__|&f| ___/| '__|/ _ \\| _|| |\\ \\/ /"); + log("&b| |__| |\\__ \\| __/| | &f| | | | | __/| | | | > < "); + log("&b \\____/ |___/ \\___||_| &f|_| |_| \\___||_| |_|/_/\\_\\"); + log("&8 "); + log("&8> &f" + getDescription().getWebsite()); + } - private void showAD() { - log("&7感谢您使用 &3&lUserPrefix " + getDescription().getVersion() + "&7!"); - log("&7本插件由 &b&lYourCraft &7提供长期支持与维护。"); - } + private void showAD() { + log("&7感谢您使用 &3&lUserPrefix " + getDescription().getVersion() + "&7!"); + log("&7本插件由 &b&lYourCraft &7提供长期支持与维护。"); + } } diff --git a/src/main/java/cc/carm/plugin/userprefix/configuration/PluginConfig.java b/src/main/java/cc/carm/plugin/userprefix/configuration/PluginConfig.java index cdef3e9..1a9a5df 100644 --- a/src/main/java/cc/carm/plugin/userprefix/configuration/PluginConfig.java +++ b/src/main/java/cc/carm/plugin/userprefix/configuration/PluginConfig.java @@ -9,77 +9,79 @@ import org.bukkit.inventory.ItemStack; public class PluginConfig { - public static ConfigValue DEBUG = new ConfigValue<>("debug", Boolean.class, false); + public static ConfigValue DEBUG = new ConfigValue<>("debug", Boolean.class, false); - public static ConfigValue METRICS = new ConfigValue<>("metrics", Boolean.class, true); + public static ConfigValue METRICS = new ConfigValue<>("metrics", Boolean.class, true); - public static class CustomStorage { + public static final ConfigValue CHECK_UPDATE = new ConfigValue<>("check-update", Boolean.class, true); - public static ConfigValue ENABLE = new ConfigValue<>("custom-storage.enable", Boolean.class, false); + public static class CustomStorage { - public static ConfigValue PATH = new ConfigValue<>("custom-storage.path", String.class, "prefixes/"); + public static ConfigValue ENABLE = new ConfigValue<>("custom-storage.enable", Boolean.class, false); - } + public static ConfigValue PATH = new ConfigValue<>("custom-storage.path", String.class, "prefixes/"); - public static class Functions { + } - public static ConfigValue NAME_PREFIX = new ConfigValue<>("functions.OnNamePrefix", Boolean.class, true); - public static ConfigValue AUTO_USE = new ConfigValue<>("functions.autoUsePrefix", Boolean.class, true); + public static class Functions { - public static class Chat { + public static ConfigValue NAME_PREFIX = new ConfigValue<>("functions.OnNamePrefix", Boolean.class, true); + public static ConfigValue AUTO_USE = new ConfigValue<>("functions.autoUsePrefix", Boolean.class, true); - public static ConfigValue ENABLE = new ConfigValue<>("functions.chat.enable", Boolean.class, false); - public static ConfigValue FORMAT = new ConfigValue<>("functions.chat.format", String.class, "<%1$s> %2$s"); + public static class Chat { - } + public static ConfigValue ENABLE = new ConfigValue<>("functions.chat.enable", Boolean.class, false); + public static ConfigValue FORMAT = new ConfigValue<>("functions.chat.format", String.class, "<%1$s> %2$s"); - } + } - public static class GUI { + } - public static ConfigValue TITLE = new ConfigValue<>("GUI.title", String.class, "&f&l我的前缀 &8| 列表"); + public static class GUI { - public static class Items { + public static ConfigValue TITLE = new ConfigValue<>("GUI.title", String.class, "&f&l我的前缀 &8| 列表"); - public static ConfigValue NEXT_PAGE = new ConfigValue<>("GUI.items.next-page", ItemStack.class, - new ItemStackFactory(Material.ARROW) - .setDisplayName("下一页") - .addLore("&7&o右键可前往最后一页哦") - .toItemStack() - ); - public static ConfigValue PREVIOUS_PAGE = new ConfigValue<>("GUI.items.previous-page", ItemStack.class, - new ItemStackFactory(Material.ARROW) - .setDisplayName("上一页") - .addLore("&7&o右键可前往第一页哦") - .toItemStack() - ); + public static class Items { - } + public static ConfigValue NEXT_PAGE = new ConfigValue<>("GUI.items.next-page", ItemStack.class, + new ItemStackFactory(Material.ARROW) + .setDisplayName("下一页") + .addLore("&7&o右键可前往最后一页哦") + .toItemStack() + ); + public static ConfigValue PREVIOUS_PAGE = new ConfigValue<>("GUI.items.previous-page", ItemStack.class, + new ItemStackFactory(Material.ARROW) + .setDisplayName("上一页") + .addLore("&7&o右键可前往第一页哦") + .toItemStack() + ); - } + } - public static class Messages { + } - public static ConfigMessageList SELECTED = new ConfigMessageList("selected"); - public static ConfigMessageList EXPIRED = new ConfigMessageList("expired"); - public static ConfigMessageList REMOVED = new ConfigMessageList("removed"); + public static class Messages { - public static ConfigMessageList RELOAD = new ConfigMessageList("reload"); - public static ConfigMessageList HELP = new ConfigMessageList("help"); + public static ConfigMessageList SELECTED = new ConfigMessageList("selected"); + public static ConfigMessageList EXPIRED = new ConfigMessageList("expired"); + public static ConfigMessageList REMOVED = new ConfigMessageList("removed"); - public static ConfigMessageList LIST_TITLE = new ConfigMessageList("list-title"); - public static ConfigMessageList LIST_VALUE = new ConfigMessageList("list-value"); + public static ConfigMessageList RELOAD = new ConfigMessageList("reload"); + public static ConfigMessageList HELP = new ConfigMessageList("help"); - } + public static ConfigMessageList LIST_TITLE = new ConfigMessageList("list-title"); + public static ConfigMessageList LIST_VALUE = new ConfigMessageList("list-value"); - public static class Sounds { + } - public static ConfigSound GUI_OPEN = new ConfigSound("Sounds.openGUI"); - public static ConfigSound GUI_CLICK = new ConfigSound("Sounds.guiClick"); - public static ConfigSound PREFIX_CHANGE = new ConfigSound("Sounds.prefixChange"); - public static ConfigSound PREFIX_EXPIRED = new ConfigSound("Sounds.prefixExpired"); + public static class Sounds { - } + public static ConfigSound GUI_OPEN = new ConfigSound("Sounds.openGUI"); + public static ConfigSound GUI_CLICK = new ConfigSound("Sounds.guiClick"); + public static ConfigSound PREFIX_CHANGE = new ConfigSound("Sounds.prefixChange"); + public static ConfigSound PREFIX_EXPIRED = new ConfigSound("Sounds.prefixExpired"); + + } } diff --git a/src/main/java/cc/carm/plugin/userprefix/hooker/UpdateChecker.java b/src/main/java/cc/carm/plugin/userprefix/hooker/UpdateChecker.java new file mode 100644 index 0000000..30f15b0 --- /dev/null +++ b/src/main/java/cc/carm/plugin/userprefix/hooker/UpdateChecker.java @@ -0,0 +1,35 @@ +package cc.carm.plugin.userprefix.hooker; + +import cc.carm.lib.githubreleases4j.GithubReleases4J; +import cc.carm.plugin.userprefix.Main; +import org.bukkit.scheduler.BukkitRunnable; + +public class UpdateChecker { + + public static void checkUpdate(String currentVersion) { + new BukkitRunnable() { + @Override + public void run() { + String downloadURL = GithubReleases4J.getReleasesURL("CarmJos", "UserPrefix"); + Integer behindVersions = GithubReleases4J.getVersionBehind( + "CarmJos", "UserPrefix", currentVersion + ); + + if (behindVersions == null) { + Main.error("检查更新失败,请您定期查看插件是否更新,避免安全问题。"); + Main.error("下载地址 " + downloadURL); + } else if (behindVersions == 0) { + Main.log("检查完成,当前已是最新版本。"); + } else if (behindVersions > 0) { + Main.log("发现新版本! 目前已落后 " + behindVersions + " 个版本。"); + Main.log("最新版下载地址 " + downloadURL); + } else { + Main.error("检查更新失败! 当前版本未知,请您使用原生版本以避免安全问题。"); + Main.error("最新版下载地址 " + downloadURL); + } + } + }.runTaskAsynchronously(Main.getInstance()); + } + + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 68d3fb6..2a78461 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,6 +7,11 @@ debug: false # 当然,您也可以选择在这里关闭,或在plugins/bStats下的配置文件中关闭。 metrics: true +# 检查更新设定 +# 该选项用于插件判断是否要检查更新,若您不希望插件检查更新并提示您,可以选择关闭。 +# 检查更新为异步操作,绝不会影响性能与使用体验。 +check-update: true + custom-storage: # 自定义存储位置 # 默认存储位置为 “插件文件夹”/prefixes diff --git a/src/main/resources/en_US/config.yml b/src/main/resources/en_US/config.yml index 7837e0c..820f2b2 100644 --- a/src/main/resources/en_US/config.yml +++ b/src/main/resources/en_US/config.yml @@ -4,6 +4,9 @@ debug: false #DEBUG OUT PUT metrics: true #Metrics stats (to help developer know the stats) +# Auto check the updates. +check-update: true + custom-storage: # Custom storage location # default location is "./prefixes"