From 838e1909c7f720a58f99b93113cda742715a1ad7 Mon Sep 17 00:00:00 2001 From: qianmo <67282958@qq.com> Date: Wed, 27 Dec 2023 22:09:51 +0800 Subject: [PATCH 1/4] =?UTF-8?q?23/12/23=20=E6=9B=B4=E6=96=B0=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=206.0.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. fix(API): 修复了自动注册命令时 Autowire 失效的问题 --- .../java/com/ultikits/ultitools/manager/CommandManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/manager/CommandManager.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/manager/CommandManager.java index f6768af..cf9824a 100755 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/manager/CommandManager.java +++ b/UltiTools-API/src/main/java/com/ultikits/ultitools/manager/CommandManager.java @@ -51,7 +51,7 @@ public void registerAll(UltiToolsPlugin plugin, String packageName) { try { AbstractCommendExecutor commandExecutor = (AbstractCommendExecutor) clazz.getDeclaredConstructor().newInstance(); - register(commandExecutor); + register(plugin, commandExecutor); } catch (InstantiationException | InvocationTargetException | IllegalAccessException | From 7ff7b4137a0bacf0789de4a21196488bfb39971a Mon Sep 17 00:00:00 2001 From: wisdommen <41609381+wisdommen@users.noreply.github.com> Date: Fri, 29 Dec 2023 10:54:41 +1100 Subject: [PATCH 2/4] =?UTF-8?q?2023/12/29=E6=9B=B4=E6=96=B0=20API=E7=89=88?= =?UTF-8?q?=E6=9C=AC6.0.3=20[UltiTools-API]=201.=E5=B0=86api=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E6=8F=90=E5=8D=87=E5=88=B0=E4=BA=86603=20[BasicFuncti?= =?UTF-8?q?ons]=201.=E6=B7=BB=E5=8A=A0=E4=BA=86Ban=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BasicFunctions/pom.xml | 4 +- .../com/ultikits/plugins/BasicFunctions.java | 20 +++++++- .../plugins/commands/BanCommands.java | 41 ++++++++++++++++ .../ultikits/plugins/config/BasicConfig.java | 4 ++ .../plugins/config/JoinWelcomeConfig.java | 2 + .../ultikits/plugins/data/BanedUserData.java | 16 +++++++ .../plugins/listeners/BanListener.java | 17 +++++++ .../plugins/services/BanPlayerService.java | 37 +++++++++++++++ .../src/main/resources/config/config.yml | 2 + .../src/main/resources/lang/en.json | 5 +- Economy/pom.xml | 2 +- Home/pom.xml | 4 +- Home/src/main/resources/plugin.yml | 2 +- Sidebar/pom.xml | 4 +- UltiTools-API/pom.xml | 2 +- .../com/ultikits/ultitools/UltiTools.java | 47 +++++++++---------- .../utils/PluginInitiationUtils.java | 7 +-- UltiTools-API/src/main/resources/config.yml | 2 +- 18 files changed, 174 insertions(+), 44 deletions(-) create mode 100644 BasicFunctions/src/main/java/com/ultikits/plugins/commands/BanCommands.java create mode 100644 BasicFunctions/src/main/java/com/ultikits/plugins/data/BanedUserData.java create mode 100644 BasicFunctions/src/main/java/com/ultikits/plugins/listeners/BanListener.java create mode 100644 BasicFunctions/src/main/java/com/ultikits/plugins/services/BanPlayerService.java diff --git a/BasicFunctions/pom.xml b/BasicFunctions/pom.xml index e1696a8..4643b70 100644 --- a/BasicFunctions/pom.xml +++ b/BasicFunctions/pom.xml @@ -18,7 +18,7 @@ com.ultikits UltiTools-API - 6.0.0.202310142230 + 6.0.3 provided @@ -78,7 +78,7 @@ UltiTools-Basic-Functions All basic functions of the UltiTools access_key.txt - F:\SpigotServers\Servers\1.19.4\plugins\UltiTools\plugins + F:\SpigotServers\Servers\1.20.4\plugins\UltiTools\plugins diff --git a/BasicFunctions/src/main/java/com/ultikits/plugins/BasicFunctions.java b/BasicFunctions/src/main/java/com/ultikits/plugins/BasicFunctions.java index 88eb500..dc6303e 100644 --- a/BasicFunctions/src/main/java/com/ultikits/plugins/BasicFunctions.java +++ b/BasicFunctions/src/main/java/com/ultikits/plugins/BasicFunctions.java @@ -4,23 +4,35 @@ import com.ultikits.plugins.config.BasicConfig; import com.ultikits.plugins.config.JoinWelcomeConfig; import com.ultikits.plugins.listeners.BackListener; +import com.ultikits.plugins.listeners.BanListener; import com.ultikits.plugins.listeners.JoinWelcomeListener; import com.ultikits.plugins.listeners.WhitelistListener; import com.ultikits.ultitools.abstracts.AbstractConfigEntity; import com.ultikits.ultitools.abstracts.UltiToolsPlugin; +//import com.ultikits.ultitools.annotations.ContextEntry; +//import com.ultikits.ultitools.annotations.EnableAutoRegister; import lombok.Getter; import java.io.IOException; import java.util.Arrays; import java.util.List; +//@EnableAutoRegister( +// scanPackage = "com.ultikits.plugins", +// eventListener = false, +// cmdExecutor = false +//) public class BasicFunctions extends UltiToolsPlugin { @Getter private static BasicFunctions instance; + public BasicFunctions() { + super(); + instance = this; + } + @Override public boolean registerSelf() throws IOException { - instance = this; BasicConfig configEntity = getConfigManager().getConfigEntity(this, BasicConfig.class); if (configEntity.isEnableHeal()) { getCommandManager().register(new HealCommand(), "ultikits.tools.command.heal", i18n("指令治愈功能"), "heal", "h"); @@ -49,9 +61,13 @@ public boolean registerSelf() throws IOException { getCommandManager().register(new TpaCommands(), "ultikits.tools.command.tpa", i18n("传送请求功能"), "tpa"); getCommandManager().register(new TpaHereCommands(), "ultikits.tools.command.tphere", i18n("请求传送到此功能"), "tphere"); } - if (configEntity.isEnableSpeed()){ + if (configEntity.isEnableSpeed()) { getCommandManager().register(new SpeedCommands(), "ultikits.tools.command.speed", i18n("速度设置功能"), "speed"); } + if (configEntity.isEnableBan()) { + getCommandManager().register(new BanCommands(), "ultikits.tools.command.uban", i18n("封禁功能"), "uban"); + getListenerManager().register(this, new BanListener()); + } return true; } diff --git a/BasicFunctions/src/main/java/com/ultikits/plugins/commands/BanCommands.java b/BasicFunctions/src/main/java/com/ultikits/plugins/commands/BanCommands.java new file mode 100644 index 0000000..6276cbc --- /dev/null +++ b/BasicFunctions/src/main/java/com/ultikits/plugins/commands/BanCommands.java @@ -0,0 +1,41 @@ +package com.ultikits.plugins.commands; + +import com.ultikits.plugins.BasicFunctions; +import com.ultikits.plugins.services.BanPlayerService; +import com.ultikits.ultitools.abstracts.AbstractCommendExecutor; +import com.ultikits.ultitools.annotations.command.*; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; +import org.bukkit.command.CommandSender; +import org.springframework.beans.factory.annotation.Autowired; + + +@CmdTarget(CmdTarget.CmdTargetType.BOTH) +@CmdExecutor(permission = "ultikits.ban.command.all", description = "Ban功能", alias = {"uban"}) +public class BanCommands extends AbstractCommendExecutor { + @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") + @Autowired + private BanPlayerService banPlayerService = new BanPlayerService(); + + @CmdMapping(format = "ban ") + public void banPlayer(@CmdSender CommandSender sender, @CmdParam("player") String player, @CmdParam("reason") String reason) { + OfflinePlayer kickedPlayer = Bukkit.getOfflinePlayer(player); + banPlayerService.banPlayer(kickedPlayer, sender.getName(), reason); + if (kickedPlayer.isOnline()) { + kickedPlayer.getPlayer().kickPlayer(String.format(BasicFunctions.getInstance().i18n("你已被封禁! 原因: %s"), reason)); + } + sender.sendMessage(BasicFunctions.getInstance().i18n("§a封禁成功")); + } + + @CmdMapping(format = "unban ") + public void unBanPlayer(@CmdSender CommandSender sender, @CmdParam("player") String player) { + banPlayerService.unBanPlayer(Bukkit.getOfflinePlayer(player)); + sender.sendMessage(BasicFunctions.getInstance().i18n("§a解封成功")); + } + + + @Override + protected void handleHelp(CommandSender sender) { + + } +} diff --git a/BasicFunctions/src/main/java/com/ultikits/plugins/config/BasicConfig.java b/BasicFunctions/src/main/java/com/ultikits/plugins/config/BasicConfig.java index f26202a..322cea3 100644 --- a/BasicFunctions/src/main/java/com/ultikits/plugins/config/BasicConfig.java +++ b/BasicFunctions/src/main/java/com/ultikits/plugins/config/BasicConfig.java @@ -1,12 +1,14 @@ package com.ultikits.plugins.config; import com.ultikits.ultitools.abstracts.AbstractConfigEntity; +import com.ultikits.ultitools.annotations.ConfigEntity; import com.ultikits.ultitools.annotations.ConfigEntry; import lombok.Getter; import lombok.Setter; @Getter @Setter +@ConfigEntity("config/config.yml") public class BasicConfig extends AbstractConfigEntity { @ConfigEntry(path = "enableWhitelist", comment = "是否启用白名单") private boolean enableWhitelist = false; @@ -26,6 +28,8 @@ public class BasicConfig extends AbstractConfigEntity { private boolean enableTpa = true; @ConfigEntry(path = "enableSpeed", comment = "是否启用速度设置") private boolean enableSpeed = true; + @ConfigEntry(path = "enableBan", comment = "是否启用封禁") + private boolean enableBan = true; public BasicConfig(String configFilePath) { super(configFilePath); diff --git a/BasicFunctions/src/main/java/com/ultikits/plugins/config/JoinWelcomeConfig.java b/BasicFunctions/src/main/java/com/ultikits/plugins/config/JoinWelcomeConfig.java index 731b6ef..e937371 100644 --- a/BasicFunctions/src/main/java/com/ultikits/plugins/config/JoinWelcomeConfig.java +++ b/BasicFunctions/src/main/java/com/ultikits/plugins/config/JoinWelcomeConfig.java @@ -1,6 +1,7 @@ package com.ultikits.plugins.config; import com.ultikits.ultitools.abstracts.AbstractConfigEntity; +import com.ultikits.ultitools.annotations.ConfigEntity; import com.ultikits.ultitools.annotations.ConfigEntry; import lombok.Getter; import lombok.Setter; @@ -10,6 +11,7 @@ @Getter @Setter +@ConfigEntity("config/join.yml") public class JoinWelcomeConfig extends AbstractConfigEntity { @ConfigEntry(path = "sendMessageDelay", comment = "x秒后发送入服欢迎") diff --git a/BasicFunctions/src/main/java/com/ultikits/plugins/data/BanedUserData.java b/BasicFunctions/src/main/java/com/ultikits/plugins/data/BanedUserData.java new file mode 100644 index 0000000..1e6ffd6 --- /dev/null +++ b/BasicFunctions/src/main/java/com/ultikits/plugins/data/BanedUserData.java @@ -0,0 +1,16 @@ +package com.ultikits.plugins.data; + +import com.ultikits.ultitools.abstracts.AbstractDataEntity; +import com.ultikits.ultitools.annotations.Table; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@Table("baned_user") +@EqualsAndHashCode(callSuper = true) +public class BanedUserData extends AbstractDataEntity { + private String name; + private String reason; + private String operator; + private String time; +} diff --git a/BasicFunctions/src/main/java/com/ultikits/plugins/listeners/BanListener.java b/BasicFunctions/src/main/java/com/ultikits/plugins/listeners/BanListener.java new file mode 100644 index 0000000..0b748a3 --- /dev/null +++ b/BasicFunctions/src/main/java/com/ultikits/plugins/listeners/BanListener.java @@ -0,0 +1,17 @@ +package com.ultikits.plugins.listeners; + +import com.ultikits.plugins.services.BanPlayerService; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class BanListener implements Listener { + private BanPlayerService banPlayerService = new BanPlayerService(); + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + if (banPlayerService.isBaned(event.getPlayer())) { + event.getPlayer().kickPlayer("你已被封禁"); + } + } +} diff --git a/BasicFunctions/src/main/java/com/ultikits/plugins/services/BanPlayerService.java b/BasicFunctions/src/main/java/com/ultikits/plugins/services/BanPlayerService.java new file mode 100644 index 0000000..84851ed --- /dev/null +++ b/BasicFunctions/src/main/java/com/ultikits/plugins/services/BanPlayerService.java @@ -0,0 +1,37 @@ +package com.ultikits.plugins.services; + +import cn.hutool.core.date.LocalDateTimeUtil; +import com.ultikits.plugins.BasicFunctions; +import com.ultikits.plugins.data.BanedUserData; +import com.ultikits.ultitools.interfaces.DataOperator; +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.springframework.stereotype.Service; + +import java.util.Date; + +@Service +public class BanPlayerService { + + public boolean isBaned(Player player) { + DataOperator dataOperator = BasicFunctions.getInstance().getDataOperator(BanedUserData.class); + BanedUserData banedUserData = dataOperator.getById(player.getUniqueId().toString()); + return banedUserData != null; + } + + public void banPlayer(OfflinePlayer player, String operator, String reason) { + DataOperator dataOperator = BasicFunctions.getInstance().getDataOperator(BanedUserData.class); + BanedUserData banedUserData = new BanedUserData(); + banedUserData.setId(player.getUniqueId().toString()); + banedUserData.setName(player.getName()); + banedUserData.setReason(reason); + banedUserData.setOperator(operator); + banedUserData.setTime(LocalDateTimeUtil.format(LocalDateTimeUtil.of(new Date()), "yyyy-MM-dd HH:mm:ss")); + dataOperator.insert(banedUserData); + } + + public void unBanPlayer(OfflinePlayer player) { + DataOperator dataOperator = BasicFunctions.getInstance().getDataOperator(BanedUserData.class); + dataOperator.delById(player.getUniqueId().toString()); + } +} diff --git a/BasicFunctions/src/main/resources/config/config.yml b/BasicFunctions/src/main/resources/config/config.yml index 67713a7..004f480 100644 --- a/BasicFunctions/src/main/resources/config/config.yml +++ b/BasicFunctions/src/main/resources/config/config.yml @@ -16,3 +16,5 @@ enableJoinWelcome: true enableTpa: true # 是否启用速度设置功能 enableSpeed: true +# 是否启用封禁功能 +enableBan: true diff --git a/BasicFunctions/src/main/resources/lang/en.json b/BasicFunctions/src/main/resources/lang/en.json index c3a71fa..98f9f44 100644 --- a/BasicFunctions/src/main/resources/lang/en.json +++ b/BasicFunctions/src/main/resources/lang/en.json @@ -54,5 +54,8 @@ "请求传送至此功能": "Request teleport to here function", "/speed <0-10> 设置速度": "/speed <0-10> Set speed", "行走/飞行速度已设置为%s,默认速度为2": "Walking/flying speed has been set to %s, the default speed is 2", - "速度设置功能": "Speed setting function" + "速度设置功能": "Speed setting function", + "你已被封禁! 原因: %s": "You have been banned! Reason: %s", + "§a封禁成功!": "§aBanned successfully!", + "§a解封成功!": "§aUnbanned successfully!" } diff --git a/Economy/pom.xml b/Economy/pom.xml index 14593ee..26360ab 100644 --- a/Economy/pom.xml +++ b/Economy/pom.xml @@ -21,7 +21,7 @@ com.ultikits UltiTools-API - 6.0.0.202310142230 + 6.0.3 provided diff --git a/Home/pom.xml b/Home/pom.xml index 29bba1a..f69897e 100755 --- a/Home/pom.xml +++ b/Home/pom.xml @@ -18,7 +18,7 @@ com.ultikits UltiTools-API - 6.0.2 + 6.0.3 provided @@ -57,7 +57,7 @@ UltiTools-Home A Home Plugin access_key.txt - F:\SpigotServers\Servers\1.19.4\plugins\UltiTools\plugins + F:\SpigotServers\Servers\1.20.4\plugins\UltiTools\plugins diff --git a/Home/src/main/resources/plugin.yml b/Home/src/main/resources/plugin.yml index 6826f10..fa86150 100644 --- a/Home/src/main/resources/plugin.yml +++ b/Home/src/main/resources/plugin.yml @@ -2,5 +2,5 @@ name: UltiTools-Home version: '${project.version}' main: com.ultikits.plugins.home.PluginMain base-package: com.ultikits.plugins.home -api-version: 600 +api-version: 603 authors: [ wisdomme ] diff --git a/Sidebar/pom.xml b/Sidebar/pom.xml index 3968d82..1afb493 100644 --- a/Sidebar/pom.xml +++ b/Sidebar/pom.xml @@ -42,7 +42,7 @@ fr.mrmicky fastboard - 1.2.1 + 2.0.2 @@ -86,7 +86,7 @@ UltiTools-Sidebar Server Sidebar Display access_key.txt - F:\SpigotServers\Servers\1.19.4\plugins\UltiTools\plugins + F:\SpigotServers\Servers\1.20.4\plugins\UltiTools\plugins diff --git a/UltiTools-API/pom.xml b/UltiTools-API/pom.xml index fa08b42..6acc070 100755 --- a/UltiTools-API/pom.xml +++ b/UltiTools-API/pom.xml @@ -4,7 +4,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> UltiTools-API com.ultikits - 6.0.2 + 6.0.3 4.0.0 UltiTools-API This project is the base of the Ultitools plugin development. diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/UltiTools.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/UltiTools.java index c982d2e..18e18f5 100755 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/UltiTools.java +++ b/UltiTools-API/src/main/java/com/ultikits/ultitools/UltiTools.java @@ -1,12 +1,8 @@ package com.ultikits.ultitools; -import cn.hutool.core.util.IdUtil; -import cn.hutool.json.JSON; -import cn.hutool.json.JSONUtil; -import com.ultikits.ultitools.abstracts.UltiToolsPlugin; -import com.ultikits.ultitools.context.ContextConfig; import com.ultikits.ultitools.commands.PluginInstallCommands; import com.ultikits.ultitools.commands.UltiToolsCommands; +import com.ultikits.ultitools.context.ContextConfig; import com.ultikits.ultitools.entities.Language; import com.ultikits.ultitools.interfaces.DataStore; import com.ultikits.ultitools.interfaces.Localized; @@ -25,7 +21,6 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.io.*; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -79,7 +74,22 @@ public static UltiTools getInstance() { * @return the version of the UltiTools */ public static int getPluginVersion() { - return 601; + return 603; + } + + /** + * Retrieves the YAML configuration object containing environment variables. + * + * @return the YAML configuration object + */ + public static YamlConfiguration getEnv() { + YamlConfiguration config = new YamlConfiguration(); + try { + config.load(Objects.requireNonNull(getInstance().getTextResource("env.yml"))); + } catch (IOException | InvalidConfigurationException e) { + throw new RuntimeException(e); + } + return config; } /** @@ -149,12 +159,12 @@ public void onEnable() { String lanPath = "lang/" + getConfig().getString("language") + ".json"; InputStream in = getFileResource(lanPath); @SuppressWarnings("DataFlowIssue") - String result = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("")); - this.language = new Language(result); + String result = new BufferedReader(new InputStreamReader(in)).lines().collect(Collectors.joining("")); + this.language = new Language(result); // Spring context initialization pluginClassLoader = getClassLoader(); - context = new AnnotationConfigApplicationContext(); + context = new AnnotationConfigApplicationContext(); context.setClassLoader(pluginClassLoader); context.register(ContextConfig.class); context.refresh(); @@ -162,7 +172,7 @@ public void onEnable() { // initialize plugin modules pluginManager = new PluginManager(); - File file = new File(getDataFolder() + File.separator + "plugins"); + File file = new File(getDataFolder() + File.separator + "plugins"); if (!file.exists()) { //noinspection ResultOfMethodCallIgnored file.mkdirs(); @@ -264,19 +274,4 @@ public BukkitAudiences adventure() { } return this.adventure; } - - /** - * Retrieves the YAML configuration object containing environment variables. - * - * @return the YAML configuration object - */ - public static YamlConfiguration getEnv() { - YamlConfiguration config = new YamlConfiguration(); - try { - config.load(Objects.requireNonNull(getInstance().getTextResource("env.yml"))); - } catch (IOException | InvalidConfigurationException e) { - throw new RuntimeException(e); - } - return config; - } } diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/PluginInitiationUtils.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/PluginInitiationUtils.java index 6303ecc..6c7d3b2 100644 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/PluginInitiationUtils.java +++ b/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/PluginInitiationUtils.java @@ -1,10 +1,7 @@ package com.ultikits.ultitools.utils; import cn.hutool.core.net.NetUtil; -import cn.hutool.core.util.IdUtil; import cn.hutool.http.HttpResponse; -import cn.hutool.json.JSON; -import cn.hutool.json.JSONUtil; import com.ultikits.ultitools.UltiTools; import com.ultikits.ultitools.entities.TokenEntity; import com.ultikits.ultitools.webserver.controller.ConfigEditorController; @@ -12,8 +9,8 @@ import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; -import java.io.*; -import java.nio.charset.StandardCharsets; +import java.io.File; +import java.io.IOException; import java.util.List; import java.util.logging.Level; diff --git a/UltiTools-API/src/main/resources/config.yml b/UltiTools-API/src/main/resources/config.yml index d14301b..0d408ef 100755 --- a/UltiTools-API/src/main/resources/config.yml +++ b/UltiTools-API/src/main/resources/config.yml @@ -1,7 +1,7 @@ datasource: type: "json" -language: "en" +language: "zh" web-editor: enable: true From 4a5af224061cbb5d32504d5fd41342c4dfaf171d Mon Sep 17 00:00:00 2001 From: qianmo <67282958@qq.com> Date: Sat, 30 Dec 2023 16:54:58 +0800 Subject: [PATCH 3/4] =?UTF-8?q?23/12/23=20=E6=9B=B4=E6=96=B0=20=E7=89=88?= =?UTF-8?q?=E6=9C=AC=206.0.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. refactor(API) 重构了 PluginManager,简化了插件注册。 2. feat(API) 插件主类默认被注册为 Bean。 3. fix(API) 修复了在多个插件注册的情况下,包扫描的出现冲突的问题。 4. chore(API) 删除了 ExceptionUtils 和其他的一些无用的代码。 5. impr(Home) 修改了 Home 模块的部分代码,以适应新的 API。 --- .../com/ultikits/plugins/home/PluginMain.java | 28 +- .../plugins/home/context/ContextConfig.java | 9 - .../plugins/home/context/HomeBean.java | 33 -- .../home/services/HomeServiceImpl.java | 2 +- .../com/ultikits/ultitools/UltiTools.java | 7 +- .../ultitools/abstracts/UltiToolsPlugin.java | 26 +- .../ultitools/manager/PluginManager.java | 291 +++++++++++------- .../ultikits/ultitools/utils/CommonUtils.java | 25 ++ .../ultitools/utils/ExceptionUtils.java | 48 --- .../ultitools/utils/PackageScanUtils.java | 4 +- 10 files changed, 263 insertions(+), 210 deletions(-) delete mode 100644 Home/src/main/java/com/ultikits/plugins/home/context/ContextConfig.java delete mode 100644 Home/src/main/java/com/ultikits/plugins/home/context/HomeBean.java delete mode 100644 UltiTools-API/src/main/java/com/ultikits/ultitools/utils/ExceptionUtils.java diff --git a/Home/src/main/java/com/ultikits/plugins/home/PluginMain.java b/Home/src/main/java/com/ultikits/plugins/home/PluginMain.java index af975bf..d189de1 100644 --- a/Home/src/main/java/com/ultikits/plugins/home/PluginMain.java +++ b/Home/src/main/java/com/ultikits/plugins/home/PluginMain.java @@ -1,16 +1,27 @@ package com.ultikits.plugins.home; -import com.ultikits.plugins.home.context.ContextConfig; +import com.ultikits.plugins.home.config.HomeConfig; +import com.ultikits.plugins.home.entity.HomeEntity; import com.ultikits.ultitools.abstracts.UltiToolsPlugin; -import com.ultikits.ultitools.annotations.ContextEntry; import com.ultikits.ultitools.annotations.EnableAutoRegister; import com.ultikits.ultitools.annotations.I18n; +import com.ultikits.ultitools.interfaces.DataOperator; +import com.ultikits.ultitools.interfaces.DataStore; import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.stereotype.Component; -@EnableAutoRegister +@Component +@ComponentScan @I18n({"zh", "en"}) -@ContextEntry(ContextConfig.class) +@EnableAutoRegister public class PluginMain extends UltiToolsPlugin { + @SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection") + @Autowired + private DataStore dataStore; + @Getter private static PluginMain pluginMain; @@ -27,4 +38,13 @@ public boolean registerSelf() { @Override public void unregisterSelf() { } + + @Bean + public HomeConfig homeConfig() { + return getConfig("config/config.yml", HomeConfig.class); + } + @Bean + public DataOperator dataOperator() { + return dataStore.getOperator(pluginMain, HomeEntity.class); + } } diff --git a/Home/src/main/java/com/ultikits/plugins/home/context/ContextConfig.java b/Home/src/main/java/com/ultikits/plugins/home/context/ContextConfig.java deleted file mode 100644 index 4619bbb..0000000 --- a/Home/src/main/java/com/ultikits/plugins/home/context/ContextConfig.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.ultikits.plugins.home.context; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan("com.ultikits.plugins.home") -public class ContextConfig { -} diff --git a/Home/src/main/java/com/ultikits/plugins/home/context/HomeBean.java b/Home/src/main/java/com/ultikits/plugins/home/context/HomeBean.java deleted file mode 100644 index 9889bea..0000000 --- a/Home/src/main/java/com/ultikits/plugins/home/context/HomeBean.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.ultikits.plugins.home.context; - -import com.ultikits.plugins.home.PluginMain; -import com.ultikits.plugins.home.config.HomeConfig; -import com.ultikits.plugins.home.entity.HomeEntity; -import com.ultikits.ultitools.interfaces.DataOperator; -import com.ultikits.ultitools.interfaces.DataStore; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration -public class HomeBean { - final DataStore dataStore; - - public HomeBean(DataStore dataStore) { - this.dataStore = dataStore; - } - - @Bean - public PluginMain getPluginMain() { - return PluginMain.getPluginMain(); - } - - @Bean - public DataOperator getDataOperator() { - return dataStore.getOperator(PluginMain.getPluginMain(), HomeEntity.class); - } - - @Bean - public HomeConfig getConfig() { - return PluginMain.getPluginMain().getConfig("config/config.yml", HomeConfig.class); - } -} diff --git a/Home/src/main/java/com/ultikits/plugins/home/services/HomeServiceImpl.java b/Home/src/main/java/com/ultikits/plugins/home/services/HomeServiceImpl.java index 344e7f9..4faaa93 100644 --- a/Home/src/main/java/com/ultikits/plugins/home/services/HomeServiceImpl.java +++ b/Home/src/main/java/com/ultikits/plugins/home/services/HomeServiceImpl.java @@ -20,7 +20,7 @@ public class HomeServiceImpl implements HomeService { private final DataOperator dataOperator; private final TeleportService teleportService; - public HomeServiceImpl(HomeConfig homeConfig, DataOperator dataOperator, TeleportService teleportService) { + public HomeServiceImpl(DataOperator dataOperator, HomeConfig homeConfig, TeleportService teleportService) { this.homeConfig = homeConfig; this.dataOperator = dataOperator; this.teleportService = teleportService; diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/UltiTools.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/UltiTools.java index c982d2e..f82acf3 100755 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/UltiTools.java +++ b/UltiTools-API/src/main/java/com/ultikits/ultitools/UltiTools.java @@ -1,9 +1,5 @@ package com.ultikits.ultitools; -import cn.hutool.core.util.IdUtil; -import cn.hutool.json.JSON; -import cn.hutool.json.JSONUtil; -import com.ultikits.ultitools.abstracts.UltiToolsPlugin; import com.ultikits.ultitools.context.ContextConfig; import com.ultikits.ultitools.commands.PluginInstallCommands; import com.ultikits.ultitools.commands.UltiToolsCommands; @@ -25,7 +21,6 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.io.*; -import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; import java.util.Objects; @@ -260,7 +255,7 @@ private InputStream getFileResource(String filename) { */ public BukkitAudiences adventure() { if (this.adventure == null) { - throw new IllegalStateException("Tried to access Adventure when the plugin was disabled!"); + throw new IllegalStateException("[UltiTools-API] Tried to access Adventure when the plugin was disabled!"); } return this.adventure; } diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/abstracts/UltiToolsPlugin.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/abstracts/UltiToolsPlugin.java index 152a4e3..8f51e01 100755 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/abstracts/UltiToolsPlugin.java +++ b/UltiTools-API/src/main/java/com/ultikits/ultitools/abstracts/UltiToolsPlugin.java @@ -2,13 +2,16 @@ import cn.hutool.core.io.FileUtil; import com.ultikits.ultitools.UltiTools; +import com.ultikits.ultitools.annotations.EnableAutoRegister; import com.ultikits.ultitools.entities.Language; import com.ultikits.ultitools.interfaces.*; import com.ultikits.ultitools.manager.CommandManager; import com.ultikits.ultitools.manager.ConfigManager; import com.ultikits.ultitools.manager.ListenerManager; import com.ultikits.ultitools.manager.PluginManager; +import com.ultikits.ultitools.utils.CommonUtils; import lombok.Getter; +import lombok.Setter; import lombok.SneakyThrows; import org.bukkit.configuration.file.YamlConfiguration; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -46,8 +49,9 @@ public abstract class UltiToolsPlugin implements IPlugin, Localized, Configurabl private final int minUltiToolsVersion; @Getter private final String mainClass; + @Setter @Getter - private final AnnotationConfigApplicationContext context; + private AnnotationConfigApplicationContext context; @SneakyThrows public UltiToolsPlugin(){ @@ -75,8 +79,7 @@ public UltiToolsPlugin(){ language = new Language(file); } saveResources(); - - this.context = new AnnotationConfigApplicationContext(); + initConfig(); } @SneakyThrows @@ -103,8 +106,23 @@ public UltiToolsPlugin(String pluginName, String version, List authors, language = new Language(file); } saveResources(); + initConfig(); + } - this.context = new AnnotationConfigApplicationContext(); + public final void initConfig() { + EnableAutoRegister annotation = this.getClass().getAnnotation(EnableAutoRegister.class); + if (annotation != null && annotation.config()) { + for (String packageName : CommonUtils.getPluginPackages(this)) { + UltiTools.getInstance().getConfigManager().registerAll( + this, packageName, this.getClass().getClassLoader() + ); + } + } else { + List allConfigs = this.getAllConfigs(); + for (AbstractConfigEntity configEntity : allConfigs) { + UltiToolsPlugin.getConfigManager().register(this, configEntity); + } + } } private InputStream getInputStream() throws IOException { diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/manager/PluginManager.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/manager/PluginManager.java index 0976e42..901b598 100755 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/manager/PluginManager.java +++ b/UltiTools-API/src/main/java/com/ultikits/ultitools/manager/PluginManager.java @@ -1,17 +1,18 @@ package com.ultikits.ultitools.manager; import com.ultikits.ultitools.UltiTools; -import com.ultikits.ultitools.abstracts.AbstractConfigEntity; import com.ultikits.ultitools.abstracts.UltiToolsPlugin; import com.ultikits.ultitools.annotations.ContextEntry; import com.ultikits.ultitools.annotations.EnableAutoRegister; import com.ultikits.ultitools.interfaces.IPlugin; +import com.ultikits.ultitools.utils.CommonUtils; import lombok.Getter; import org.bukkit.Bukkit; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import java.io.File; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; @@ -25,106 +26,122 @@ public class PluginManager { @Getter private final List pluginList = new ArrayList<>(); - private URLClassLoader urlClassLoader; + private final List> pluginClassList = new ArrayList<>(); public void init() throws IOException { - Bukkit.getLogger().log(Level.INFO, "Initiating UltiTools plugins..."); String currentPath = System.getProperty("user.dir"); - String path = currentPath + File.separator + "plugins" + File.separator + "UltiTools" + File.separator + "plugins"; + String path = currentPath+ File.separator + "plugins" + File.separator + "UltiTools" + File.separator + "plugins"; File pluginFolder = new File(path); File[] plugins = pluginFolder.listFiles((file) -> file.getName().endsWith(".jar")); + if (plugins == null) { return; } - URL[] urls = new URL[plugins.length]; + Bukkit.getLogger().log(Level.INFO, "[UltiTools-API] Found " + plugins.length + " file(s):"); - for (int i = 0; i < plugins.length; i++) { - urls[i] = plugins[i].toURI().toURL(); + for (File file : plugins) { + Bukkit.getLogger().log(Level.INFO, " - " + file.getName()); } - // 将jar文件组成数组,来创建一个URLClassLoader - urlClassLoader = new URLClassLoader(urls, UltiTools.getInstance().getPluginClassLoader()); for (File file : plugins) { - try (JarFile jarFile = new JarFile(file)) { - Enumeration entryEnumeration = jarFile.entries(); - while (entryEnumeration.hasMoreElements()) { - // 获取JarEntry对象 - JarEntry entry = entryEnumeration.nextElement(); - // 获取当前JarEntry对象的路径+文件名 - if (!entry.getName().contains(".class") || entry.getName().contains("META-INF")) { - continue; - } - try { - Class aClass = urlClassLoader.loadClass(entry.getName().replace("/", ".").replace(".class", "")); - if (!IPlugin.class.isAssignableFrom(aClass)) { - continue; - } - UltiToolsPlugin plugin = (UltiToolsPlugin) aClass.getDeclaredConstructor().newInstance(); - if (plugin.getPluginName() != null) { - pluginList.add(plugin); - } - } catch (NoClassDefFoundError | InvocationTargetException | NoSuchMethodException ignored) { - } - } - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ignored) { + Class pluginClass = loadPluginMainClass(file); + if (pluginClass != null) { + pluginClassList.add(pluginClass); } } int success = 0; - if (pluginList.isEmpty()) { - Bukkit.getLogger().log(Level.INFO, "No UltiTools plugins found."); + if (pluginClassList.isEmpty()) { + Bukkit.getLogger().log(Level.INFO, "[UltiTools-API] No UltiTools plugin found."); return; } - Bukkit.getLogger().log(Level.INFO, String.format("%d UltiTools plugins found.", pluginList.size())); - for (int i = 0; i < pluginList.size(); i++) { - Bukkit.getLogger().log(Level.INFO, String.format("Now loading plugin %d", i + 1)); - UltiToolsPlugin plugin = pluginList.get(i); - if (register(plugin)) { + Bukkit.getLogger().log(Level.INFO, String.format("[UltiTools-API] %d UltiTools plugin(s) found.", pluginClassList.size())); + for (Class pluginClass : pluginClassList) { + if (register(pluginClass)) { success++; } } - Bukkit.getLogger().log(Level.INFO, String.format("Successfully loaded %d plugins! Failed %d!", success, pluginList.size() - success)); + Bukkit.getLogger().log(Level.INFO, "[UltiTools-API] Plugin Loading completed."); + Bukkit.getLogger().log( + Level.INFO, + String.format("[UltiTools-API] Succeeded loaded %d, Failed %d.", success, pluginClassList.size() - success) + ); } - public boolean register(UltiToolsPlugin plugin) { - if (!pluginList.contains(plugin)) { - pluginList.add(plugin); + public boolean register(Class pluginClass) { + UltiToolsPlugin plugin; + try { + plugin = initializePlugin(pluginClass); + } catch (Exception e) { + Bukkit.getLogger().log( + Level.WARNING, + String.format("[UltiTools-API] Cannot initialize plugin for %s", pluginClass.getName()) + ); + return false; } - if (plugin.getMinUltiToolsVersion() > UltiTools.getPluginVersion()) { - Bukkit.getLogger().log(Level.WARNING, String.format("%s load failed!UltiTools version is outdated!", plugin.getPluginName())); + boolean result = invokeRegisterSelf(plugin); + if (result) { + registerBukkit(plugin); + } + return result; + } + + public boolean register( + Class pluginClass, + String pluginName, + String version, + List authors, + List loadAfter, + int minUltiToolsVersion, + String mainClass + ) { + UltiToolsPlugin plugin; + try { + plugin = initializePlugin( + pluginClass, pluginName, version, authors, loadAfter, minUltiToolsVersion, mainClass + ); + } catch (Exception e) { + Bukkit.getLogger().log( + Level.WARNING, + String.format("[UltiTools-API] Cannot initialize plugin for %s", pluginClass.getName()) + ); return false; } + boolean result = invokeRegisterSelf(plugin); + if (result) { + registerBukkit(plugin); + } + return result; + } + + public boolean register(UltiToolsPlugin plugin) { try { - EnableAutoRegister annotation = plugin.getClass().getAnnotation(EnableAutoRegister.class); - if (annotation != null && annotation.config()) { - UltiTools.getInstance().getConfigManager().registerAll( - plugin, - annotation.scanPackage().isEmpty() ? - plugin.getClass().getPackage().getName() : - annotation.scanPackage(), - plugin.getClass().getClassLoader() - ); - } else { - List allConfigs = plugin.getAllConfigs(); - for (AbstractConfigEntity configEntity : allConfigs) { - UltiToolsPlugin.getConfigManager().register(plugin, configEntity); - } + AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext(); + plugin.setContext(pluginContext); + pluginContext.setParent(UltiTools.getInstance().getContext()); + pluginContext.registerShutdownHook(); + pluginContext.setClassLoader(plugin.getClass().getClassLoader()); + pluginContext.getBeanFactory().registerSingleton(plugin.getClass().getSimpleName(), plugin); + pluginContext.refresh(); + if (plugin.getClass().isAnnotationPresent(ContextEntry.class)) { + ContextEntry contextEntry = plugin.getClass().getAnnotation(ContextEntry.class); + Class clazz = contextEntry.value(); + pluginContext.register(clazz); + pluginContext.refresh(); + pluginContext.getAutowireCapableBeanFactory().autowireBean(plugin); } - initPluginContext(plugin); - boolean registerSelf = plugin.registerSelf(); - initAutoRegister(plugin); - if (registerSelf) { - Bukkit.getLogger().log(Level.INFO, String.format("%s loaded!Version: %s。", plugin.getPluginName(), plugin.getVersion())); - } else { - plugin.getContext().close(); - Bukkit.getLogger().log(Level.WARNING, String.format("%s load failed!Version: %s。", plugin.getPluginName(), plugin.getVersion())); - } - return registerSelf; } catch (Exception e) { - Bukkit.getLogger().log(Level.WARNING, e, String::new); - Bukkit.getLogger().log(Level.WARNING, String.format("%s load failed!", plugin.getPluginName())); + Bukkit.getLogger().log( + Level.WARNING, + String.format("[UltiTools-API] Cannot initialize plugin for %s", plugin.getPluginName()) + ); return false; } + boolean result = invokeRegisterSelf(plugin); + if (result) { + registerBukkit(plugin); + } + return result; } public void unregister(UltiToolsPlugin plugin) { @@ -134,58 +151,124 @@ public void unregister(UltiToolsPlugin plugin) { } public void close() { - Bukkit.getLogger().log(Level.INFO, "Unregistering all plugins..."); + Bukkit.getLogger().log(Level.INFO, "[UltiTools-API] Unregistering all plugins..."); for (UltiToolsPlugin plugin : pluginList) { unregister(plugin); } pluginList.clear(); - try { - urlClassLoader.close(); - } catch (IOException ignored) { - } + pluginClassList.clear(); } public void reload() { - Bukkit.getLogger().log(Level.INFO, "Reloading all plugins..."); + Bukkit.getLogger().log(Level.INFO, "[UltiTools-API] Reloading all plugins..."); for (UltiToolsPlugin plugin : pluginList) { plugin.reloadSelf(); } - Bukkit.getLogger().log(Level.INFO, "All plugins reloaded."); - Bukkit.getLogger().log(Level.WARNING, "This operation is only used for reloading plugin configuration. If (un)installing, please restart the server!"); + Bukkit.getLogger().log(Level.INFO, "[UltiTools-API] All plugins reloaded."); + Bukkit.getLogger().log( + Level.WARNING, + "[UltiTools-API] This operation is only used for reloading plugin configuration. If (un)installing, please restart the server!" + ); } - private void initPluginContext(UltiToolsPlugin plugin) { - plugin.getContext().setParent(UltiTools.getInstance().getContext()); - plugin.getContext().registerShutdownHook(); - plugin.getContext().setClassLoader(urlClassLoader); - if (plugin.getClass().isAnnotationPresent(ContextEntry.class)) { - Class contextEntry = plugin.getClass().getAnnotation(ContextEntry.class).value(); - plugin.getContext().register(contextEntry); - plugin.getContext().refresh(); - plugin.getContext().getAutowireCapableBeanFactory().autowireBean(plugin); + private Class loadPluginMainClass(File pluginJar) { + try { + @SuppressWarnings("resource") + URLClassLoader classLoader = new URLClassLoader( + new URL[]{pluginJar.toURI().toURL()}, + UltiTools.getInstance().getPluginClassLoader() + ); + try (JarFile jarFile = new JarFile(pluginJar)) { + Enumeration entryEnumeration = jarFile.entries(); + + while (entryEnumeration.hasMoreElements()) { + JarEntry entry = entryEnumeration.nextElement(); + + if (!entry.getName().contains(".class") || entry.getName().contains("META-INF")) { + continue; + } + + String className = entry + .getName() + .replace('/', '.') + .replace(".class", ""); + + try { + Class aClass = classLoader.loadClass(className); + if (IPlugin.class.isAssignableFrom(aClass)) { + return aClass.asSubclass(UltiToolsPlugin.class); + } + } catch (ClassNotFoundException | NoClassDefFoundError ignored) { + } + } + } catch (IOException ignored) { + } + } catch (MalformedURLException ignored) { + } + return null; + } + + + private boolean invokeRegisterSelf(UltiToolsPlugin plugin) { + if (plugin.getMinUltiToolsVersion() > UltiTools.getPluginVersion()) { + Bukkit.getLogger().log( + Level.WARNING, + String.format("[UltiTools-API] %s load failed!UltiTools version is outdated!", plugin.getPluginName()) + ); + plugin.getContext().close(); + return false; + } + try { + boolean registerSelf = plugin.registerSelf(); + registerBukkit(plugin); + if (registerSelf) { + pluginList.add(plugin); + Bukkit.getLogger().log( + Level.INFO, + String.format("[UltiTools-API] %s loaded!Version: %s。", plugin.getPluginName(), plugin.getVersion()) + ); + } else { + plugin.getContext().close(); + Bukkit.getLogger().log( + Level.WARNING, + String.format("[UltiTools-API] %s load failed!Version: %s。", plugin.getPluginName(), plugin.getVersion()) + ); + } + return registerSelf; + } catch (Exception e) { + Bukkit.getLogger().log(Level.WARNING, e, String::new); + Bukkit.getLogger().log(Level.WARNING, String.format("[UltiTools-API] %s load failed!", plugin.getPluginName())); + return false; } } - private void initAutoRegister(UltiToolsPlugin plugin) { + private UltiToolsPlugin initializePlugin(Class pluginClass, Object... constructorArgs) { + AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext(); + pluginContext.setParent(UltiTools.getInstance().getContext()); + pluginContext.registerShutdownHook(); + pluginContext.setClassLoader(pluginClass.getClassLoader()); + pluginContext.registerBean(pluginClass, constructorArgs); + pluginContext.refresh(); + UltiToolsPlugin plugin = pluginContext.getBean(pluginClass); + pluginContext.setDisplayName(plugin.getPluginName()); + pluginContext.setId(plugin.getPluginName()); + plugin.setContext(pluginContext); + return plugin; + } + + private void registerBukkit(UltiToolsPlugin plugin) { if (!plugin.getClass().isAnnotationPresent(EnableAutoRegister.class)) { return; } EnableAutoRegister annotation = plugin.getClass().getAnnotation(EnableAutoRegister.class); - if (annotation.cmdExecutor()) { - UltiTools.getInstance().getCommandManager().registerAll( - plugin, - annotation.scanPackage().isEmpty() ? - plugin.getClass().getPackage().getName() : - annotation.scanPackage() - ); - } - if (annotation.eventListener()) { - UltiTools.getInstance().getListenerManager().registerAll( - plugin, - annotation.scanPackage().isEmpty() ? - plugin.getClass().getPackage().getName() : - annotation.scanPackage() - ); + String[] packages = CommonUtils.getPluginPackages(plugin); + for (String packageName : packages) { + if (annotation.cmdExecutor()) { + UltiTools.getInstance().getCommandManager().registerAll(plugin, packageName); + } + if (annotation.eventListener()) { + UltiTools.getInstance().getListenerManager().registerAll(plugin, packageName); + } } } } diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/CommonUtils.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/CommonUtils.java index 1c2b31a..68c6685 100644 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/CommonUtils.java +++ b/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/CommonUtils.java @@ -4,6 +4,9 @@ import cn.hutool.json.JSON; import cn.hutool.json.JSONUtil; import com.ultikits.ultitools.UltiTools; +import com.ultikits.ultitools.abstracts.UltiToolsPlugin; +import com.ultikits.ultitools.annotations.EnableAutoRegister; +import org.springframework.context.annotation.ComponentScan; import java.io.File; import java.io.FileWriter; @@ -29,4 +32,26 @@ public static String getUltiToolsUUID() throws IOException { } return json.getByPath("uuid").toString(); } + + public static String[] getPluginPackages(UltiToolsPlugin plugin) { + Class pluginClass = plugin.getClass(); + String[] packages; + + if (!pluginClass.isAnnotationPresent(ComponentScan.class)) { + if (pluginClass.isAnnotationPresent(EnableAutoRegister.class)) { + EnableAutoRegister enableAutoRegister = pluginClass.getAnnotation(EnableAutoRegister.class); + packages = new String[]{enableAutoRegister.scanPackage()}; + } else { + packages = new String[]{pluginClass.getPackage().getName()}; + } + } else { + ComponentScan componentScan = pluginClass.getAnnotation(ComponentScan.class); + packages = (componentScan.value().length != 0) ? componentScan.value() : + (componentScan.basePackages().length != 0) ? componentScan.basePackages() : + new String[]{pluginClass.getPackage().getName()}; + } + + return packages; + } + } diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/ExceptionUtils.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/ExceptionUtils.java deleted file mode 100644 index 790c8e6..0000000 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/ExceptionUtils.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.ultikits.ultitools.utils; - -import com.ultikits.ultitools.UltiTools; -import org.bukkit.Bukkit; - -import java.util.Objects; -import java.util.logging.Logger; - -public class ExceptionUtils { - - private static final Logger logger = UltiTools.getInstance().getLogger(); - - public static void catchException(Exception exception) { - for (StackTraceElement stackTraceElement : exception.getStackTrace()) { - if (stackTraceElement.getClassName().contains("ultitools")) { - logger.warning("==============================================================="); - logger.warning(" UltiTools Exception Catcher 异常捕获 "); - logger.warning(" 这可能是一个BUG "); - logger.warning("==============================================================="); - logger.warning( - "UltiTools版本号: " - + UltiTools.getInstance().getDescription().getVersion() - + " PlaceholderAPI版本号: " - + (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null - ? "--" : Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("PlaceholderAPI")).getDescription().getVersion()) - + " Vault版本号: " - + (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") == null - ? "--" : Objects.requireNonNull(Bukkit.getPluginManager().getPlugin("Vault")).getDescription().getVersion()) - ); - logger.warning("==============================================================="); - logger.warning("Bukkit Version: " + Bukkit.getBukkitVersion() + " Server Version: " + Bukkit.getVersion()); - logger.warning("==============================================================="); - logger.warning(exception.getLocalizedMessage()); - logger.warning("==============================================================="); - logger.warning(" StackTrace 调用栈 "); - logger.warning("ClassName FileName MethodName Line"); - logger.warning("==============================================================="); - for (StackTraceElement ste : exception.getStackTrace()) - logger.warning(ste.getClassName() + " " + ste.getFileName() + " " + ste.getMethodName() + " " + ste.getLineNumber()); - logger.warning("==============================================================="); - logger.warning(" 请将以上信息反馈至官方QQ群或GitHub Issue "); - logger.warning("==============================================================="); - break; - } - } - } -} - diff --git a/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/PackageScanUtils.java b/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/PackageScanUtils.java index 3255067..bc1da54 100644 --- a/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/PackageScanUtils.java +++ b/UltiTools-API/src/main/java/com/ultikits/ultitools/utils/PackageScanUtils.java @@ -1,10 +1,12 @@ package com.ultikits.ultitools.utils; import com.google.common.reflect.ClassPath; +import org.bukkit.Bukkit; import java.lang.annotation.Annotation; import java.util.HashSet; import java.util.Set; +import java.util.logging.Level; public class PackageScanUtils { public static Set> scanAnnotatedClasses( @@ -22,7 +24,7 @@ public static Set> scanAnnotatedClasses( } } } catch (Exception e) { - ExceptionUtils.catchException(e); + Bukkit.getLogger().log(Level.SEVERE, "Failed to scan annotated classes", e); } return classes; } From ce9d1c670ac12b1f426cee49afcd60e9be0192b6 Mon Sep 17 00:00:00 2001 From: QianMo SAMA <62180110+qianmo2233@users.noreply.github.com> Date: Sun, 31 Dec 2023 20:15:00 +0800 Subject: [PATCH 4/4] Update README.md fix img --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c92d26b..0d9eaef 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,10 @@
- +
+

UltiTools 6

UltiTools' Reborn