From 2bbd36efa75c79f29183018f2f93911e3988fbb5 Mon Sep 17 00:00:00 2001 From: cnlimiter Date: Wed, 11 Sep 2024 23:38:39 +0800 Subject: [PATCH] refactor(common): refactor event and utility classes for clarity and consistency Refactor several core utility and event handling classes to improve code clarity and consistency. Update formatting in CQUtils, refine error handling in I18n, and simplify event formatting in IBotEvent. Also, introduce ActionPath enum and ActionHandler class for better management of action-related logic. --- .../mods/mcbot/api/action/ActionPath.java | 20 +++++++++++++++++++ .../mcbot/api/event/TypeTrackedEvent.java | 5 ++--- .../mods/mcbot/common/event/IBotEvent.java | 3 +-- .../mcbot/plugins/action/ActionHandler.java | 16 +++++++++++++++ .../cn/evole/mods/mcbot/util/locale/I18n.java | 7 ++++--- .../evole/mods/mcbot/util/onebot/CQUtils.java | 8 +++++--- 6 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 common/src/main/java/cn/evole/mods/mcbot/api/action/ActionPath.java create mode 100644 common/src/main/java/cn/evole/mods/mcbot/plugins/action/ActionHandler.java diff --git a/common/src/main/java/cn/evole/mods/mcbot/api/action/ActionPath.java b/common/src/main/java/cn/evole/mods/mcbot/api/action/ActionPath.java new file mode 100644 index 0000000..24d4ae2 --- /dev/null +++ b/common/src/main/java/cn/evole/mods/mcbot/api/action/ActionPath.java @@ -0,0 +1,20 @@ +package cn.evole.mods.mcbot.api.action; + +/** + * @Project: McBot + * @Author: cnlimiter + * @CreateTime: 2024/9/11 23:34 + * @Description: 用于自定义服务器事件行为 + */ +public enum ActionPath { + PLAYER_ADVANCEMENT, + PLAYER_CHANGE_DIMENSION, + PLAYER_DEATH, + PLAYER_LOGGED_IN, + PLAYER_LOGGED_OUT, + PLAYER_USE_CMD, + PLAYER_CHAT, + SERVER_TICK, + SERVER_STOP, + SERVER_START +} diff --git a/common/src/main/java/cn/evole/mods/mcbot/api/event/TypeTrackedEvent.java b/common/src/main/java/cn/evole/mods/mcbot/api/event/TypeTrackedEvent.java index 50f41a1..42ca921 100644 --- a/common/src/main/java/cn/evole/mods/mcbot/api/event/TypeTrackedEvent.java +++ b/common/src/main/java/cn/evole/mods/mcbot/api/event/TypeTrackedEvent.java @@ -1,6 +1,7 @@ package cn.evole.mods.mcbot.api.event; import com.google.common.collect.Maps; +import lombok.Getter; import java.util.Map; import java.util.function.Function; @@ -11,6 +12,7 @@ * @CreateTime: 2024/8/11 20:31 * @Description: from ... */ +@Getter public class TypeTrackedEvent extends Event { private final Map, T> listenerTypes = Maps.newHashMap(); @@ -28,7 +30,4 @@ public void register(Class type, T listener) { listenerTypes.put(type, listener); } - public Map, T> getListenerTypes() { - return listenerTypes; - } } diff --git a/common/src/main/java/cn/evole/mods/mcbot/common/event/IBotEvent.java b/common/src/main/java/cn/evole/mods/mcbot/common/event/IBotEvent.java index bae23de..fd13765 100644 --- a/common/src/main/java/cn/evole/mods/mcbot/common/event/IBotEvent.java +++ b/common/src/main/java/cn/evole/mods/mcbot/common/event/IBotEvent.java @@ -1,6 +1,5 @@ package cn.evole.mods.mcbot.common.event; -import cn.evole.mods.mcbot.Constants; import cn.evole.mods.mcbot.api.bot.BotApi; import cn.evole.mods.mcbot.api.cmd.CmdApi; import cn.evole.mods.mcbot.api.data.ChatRecordApi; @@ -56,7 +55,7 @@ private void onGroupMessage(GroupMessageEvent event, String send) { String finalMsg = ModConfig.get().getCmd().isGamePrefixOn() ? ModConfig.get().getCmd().isIdGamePrefixOn() - ? String.format("§b[§l%s§r(§5%s§b)]§a<%s>§f %s", ModConfig.get().getCmd().getQqGamePrefix(), event.getGroupId(), groupNick, send) + ? String.format("§b[§l%s§r(§5%s§r)§b]§a<%s>§f %s", ModConfig.get().getCmd().getQqGamePrefix(), event.getGroupId(), groupNick, send) : String.format("§b[§l%s§b]§a<%s>§f %s", ModConfig.get().getCmd().getQqGamePrefix(), groupNick, send) : String.format("§a<%s>§f %s", groupNick, send); diff --git a/common/src/main/java/cn/evole/mods/mcbot/plugins/action/ActionHandler.java b/common/src/main/java/cn/evole/mods/mcbot/plugins/action/ActionHandler.java new file mode 100644 index 0000000..036243b --- /dev/null +++ b/common/src/main/java/cn/evole/mods/mcbot/plugins/action/ActionHandler.java @@ -0,0 +1,16 @@ +package cn.evole.mods.mcbot.plugins.action; + +import cn.evole.mods.mcbot.Constants; + +import java.io.File; + +/** + * @Project: McBot + * @Author: cnlimiter + * @CreateTime: 2024/9/11 23:31 + * @Description: + */ +public class ActionHandler { + private static final File dir = Constants.CONFIG_FOLDER.resolve("actions").toFile(); + +} diff --git a/common/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java b/common/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java index c5862c3..abb917b 100644 --- a/common/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java +++ b/common/src/main/java/cn/evole/mods/mcbot/util/locale/I18n.java @@ -65,11 +65,12 @@ public static String get(String key, Object... args) { if (translation1 != null) { return String.format(translation1, args); } else { - String translation2 = Language.getInstance().getOrDefault(key); - if (!translation2.equals(key)) { + String key2 = key.replaceAll("mcbot.", ""); + String translation2 = Language.getInstance().getOrDefault(key2); + if (!translation2.equals(key2)) { return String.format(translation2, args); } else { - return "TranslateError{\"key\":\"" + key + "\",\"args\":" + Arrays.toString(args) + "}"; + return "TranslateError{\"key\":\"" + key2 + "\",\"args\":" + Arrays.toString(args) + "}"; } } } catch (Exception e) { diff --git a/common/src/main/java/cn/evole/mods/mcbot/util/onebot/CQUtils.java b/common/src/main/java/cn/evole/mods/mcbot/util/onebot/CQUtils.java index b067640..c497c39 100644 --- a/common/src/main/java/cn/evole/mods/mcbot/util/onebot/CQUtils.java +++ b/common/src/main/java/cn/evole/mods/mcbot/util/onebot/CQUtils.java @@ -59,7 +59,9 @@ public class CQUtils { } case image -> { val url = arrayMsg.getData().get("file"); - if (ModConfig.get().getCommon().isImageOn() && Services.PLATFORM.isModLoaded("chatimage")) { + if (ModConfig.get().getCommon().isImageOn() + && Services.PLATFORM.isModLoaded("chatimage") + ) { message.append(String.format("[[CICode,url=%s,name=来自QQ的图片]]", url.replaceAll("&", "&")//转义字符转义 )); @@ -69,9 +71,9 @@ public class CQUtils { } case at -> { val qq = arrayMsg.getData().get("qq"); - if (!qq.equalsIgnoreCase("@")) { + if (!qq.equalsIgnoreCase("all")) { if (event instanceof GroupMessageEvent groupMessageEvent) - message.append(String.format("[@%s]", groupMessageEvent.getSender().getNickname())); + message.append(String.format("[@%s]", arrayMsg.getData().get("name"))); else message.append("[@]"); } else { message.append("[@全体]");