Skip to content

Commit

Permalink
refactor(common): refactor event and utility classes for clarity and …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
cnlimiter committed Sep 11, 2024
1 parent 475617f commit 2bbd36e
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -11,6 +12,7 @@
* @CreateTime: 2024/8/11 20:31
* @Description: from <a href="https://github.com/AHilyard/Iceberg/blob/1.21-multi/common/src/main/java/com/anthonyhilyard/iceberg/events/TypeTrackedEvent.java">...</a>
*/
@Getter
public class TypeTrackedEvent<S, T> extends Event<T> {
private final Map<Class<? extends S>, T> listenerTypes = Maps.newHashMap();

Expand All @@ -28,7 +30,4 @@ public void register(Class<? extends S> type, T listener) {
listenerTypes.put(type, listener);
}

public Map<Class<? extends S>, T> getListenerTypes() {
return listenerTypes;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("&amp;", "&")//转义字符转义
));
Expand All @@ -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("[@全体]");
Expand Down

0 comments on commit 2bbd36e

Please sign in to comment.