Skip to content

Commit

Permalink
```refactor(mcbot): reorganize modules and improve command handling- …
Browse files Browse the repository at this point in the history
…Restructure project modules from 'core' to 'common' and 'plugins'.

- Move command handling to 'plugins' module and update related event packages.- Improve command utility functions and variable handling.
- Ensure consistent package naming conventions across the project.

BREAKING CHANGE: All imports related to command handling and event listeners will need
to be updated to reflect the new package structure. For example, commands are now located
under 'cn.evole.mods.mcbot.plugins.cmd' and events under 'cn.evole.mods.mcbot.common.event'.
```
  • Loading branch information
cnlimiter committed Aug 16, 2024
1 parent 3185b0b commit 89d36a9
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 30 deletions.
8 changes: 4 additions & 4 deletions common/src/main/java/cn/evole/mods/mcbot/McBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import cn.evole.mods.mcbot.api.config.ConfigManager;
import cn.evole.mods.mcbot.api.event.server.ServerGameEvents;
import cn.evole.mods.mcbot.common.config.*;
import cn.evole.mods.mcbot.core.cmd.CmdHandler;
import cn.evole.mods.mcbot.core.event.IBotEvent;
import cn.evole.mods.mcbot.core.event.IChatEvent;
import cn.evole.mods.mcbot.core.event.IPlayerEvent;
import cn.evole.mods.mcbot.plugins.cmd.CmdHandler;
import cn.evole.mods.mcbot.common.event.IBotEvent;
import cn.evole.mods.mcbot.common.event.IChatEvent;
import cn.evole.mods.mcbot.common.event.IPlayerEvent;
import cn.evole.mods.mcbot.util.MsgThreadUtils;
import cn.evole.mods.mcbot.util.locale.I18n;
import cn.evole.mods.mcbot.util.onebot.CQUtils;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cn.evole.mods.mcbot.api.bot;

import cn.evole.mods.mcbot.common.config.ModConfig;
import cn.evole.mods.mcbot.core.event.ITickEvent;
import cn.evole.mods.mcbot.common.event.ITickEvent;
import cn.evole.mods.mcbot.util.MsgThreadUtils;
import cn.evole.onebot.sdk.action.ActionPath;
import com.google.gson.JsonObject;
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/cn/evole/mods/mcbot/api/cmd/Cmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class Cmd {
@Expose private List<String> allow_members;
@Expose private String permission;
@Expose private List<String> after_cmds;
@Expose private String answer;
@Expose private boolean enable;
}
15 changes: 15 additions & 0 deletions common/src/main/java/cn/evole/mods/mcbot/api/cmd/CmdApi.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
package cn.evole.mods.mcbot.api.cmd;

import cn.evole.mods.mcbot.util.CmdUtils;
import cn.evole.onebot.sdk.event.message.GroupMessageEvent;

/**
* @Project: McBot
* @Author: cnlimiter
* @CreateTime: 2024/8/12 01:56
* @Description:
*/
public class CmdApi {


public static void invokeCommandGroup(GroupMessageEvent event, String msg) {
String command = msg.substring(1);//去除前缀
String originCommand = command;
command = CmdUtils.cmdParse(command);





}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import cn.evole.mods.mcbot.Constants;
import cn.evole.mods.mcbot.common.config.ModConfig;
import cn.evole.mods.mcbot.core.event.IBotEvent;
import cn.evole.mods.mcbot.common.event.IBotEvent;
import cn.evole.onebot.client.OneBotClient;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evole.mods.mcbot.core.event;
package cn.evole.mods.mcbot.common.event;

import cn.evole.mods.mcbot.api.bot.BotApi;
import cn.evole.mods.mcbot.common.config.ModConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evole.mods.mcbot.core.event;
package cn.evole.mods.mcbot.common.event;

import cn.evole.mods.mcbot.api.bot.BotApi;
import cn.evole.mods.mcbot.common.config.ModConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evole.mods.mcbot.core.event;
package cn.evole.mods.mcbot.common.event;

import cn.evole.mods.mcbot.common.command.*;
import com.mojang.brigadier.CommandDispatcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evole.mods.mcbot.core.event;
package cn.evole.mods.mcbot.common.event;

import cn.evole.mods.mcbot.api.bot.BotApi;
import cn.evole.mods.mcbot.common.config.ModConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evole.mods.mcbot.core.event;
package cn.evole.mods.mcbot.common.event;

import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.MinecraftServer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package cn.evole.mods.mcbot.core.cmd;
package cn.evole.mods.mcbot.plugins.cmd;

import cn.evole.mods.mcbot.Constants;
import cn.evole.mods.mcbot.api.cmd.Cmd;
import cn.evole.onebot.sdk.util.GsonUtils;
import com.google.common.base.Stopwatch;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import lombok.val;
Expand All @@ -17,6 +15,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import static cn.evole.mods.mcbot.Constants.GSON;

/**
* @Project: McBot
* @Author: cnlimiter
Expand All @@ -26,9 +26,7 @@
public class CmdHandler {
private static final File dir = Constants.CONFIG_FOLDER.resolve("cmds").toFile();

private static final Gson GSON = GsonUtils.getNullGson();

private static Map<String, Cmd> cmds = new ConcurrentHashMap<>();
public static Map<String, Cmd> cmds = new ConcurrentHashMap<>();

public static void load() {
val stopwatch = Stopwatch.createStarted();
Expand All @@ -47,9 +45,9 @@ public static void load() {

private static void writeDefault() {
if (!dir.exists() && dir.mkdirs()) {
JsonObject json1 = GSON.fromJson("{'id': 'list', 'cmd': 'list', 'alies': ['服务器在线'], 'allow_members': [], permission: 'ALL', 'enable': true}", JsonObject.class);
JsonObject json2 = GSON.fromJson("{'id': 'say', 'cmd': 'say %', 'alies': ['转发'], 'allow_members': [], permission: 'OP', 'enable': true}", JsonObject.class);
JsonObject json3 = GSON.fromJson("{'id': 'bind', 'cmd': 'mcbot addBind %', 'alies': ['绑定'], 'allow_members': [], permission: 'ALL', 'enable': true}", JsonObject.class);
JsonObject json1 = GSON.fromJson("{'id': 'list', 'cmd': 'list', 'alies': ['服务器在线'], 'allow_members': [], permission: 'ALL', 'answer': 'NO', 'enable': true}", JsonObject.class);
JsonObject json2 = GSON.fromJson("{'id': 'say', 'cmd': 'say %', 'alies': ['转发'], 'allow_members': [], permission: 'OP', 'answer': '转发成功!', 'enable': true}", JsonObject.class);
JsonObject json3 = GSON.fromJson("{'id': 'bind', 'cmd': 'mcbot addBind %', 'alies': ['绑定'], 'allow_members': [], permission: 'ALL', 'answer': '绑定 % 成功!', 'enable': true}", JsonObject.class);

// 尝试创建和写入文件
try (FileWriter writerList = new FileWriter(new File(dir, "list.json"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//package cn.evole.mods.mcbot.plugins.cmd;
//
//import cn.evole.mods.mcbot.Constants;
//import com.google.common.base.Stopwatch;
//import com.google.gson.JsonObject;
//import lombok.val;
//
//import java.io.File;
//import java.io.FileWriter;
//import java.io.IOException;
//import java.util.Map;
//import java.util.concurrent.ConcurrentHashMap;
//import java.util.concurrent.TimeUnit;
//
//import static cn.evole.mods.mcbot.Constants.GSON;
//
///**
// * @Project: McBot
// * @Author: cnlimiter
// * @CreateTime: 2024/8/17 00:08
// * @Description:
// */
//public class VarHandler {
// private static final File dir = Constants.CONFIG_FOLDER.toFile();
//
// private static Map<String, String> vars = new ConcurrentHashMap<>();
//
// public static void load() {
// val stopwatch = Stopwatch.createStarted();
//
// writeDefault();
// clear();
//
// if (!dir.mkdirs() && dir.isDirectory()) {
// loadFiles();
// }
//
// stopwatch.stop();
//
// Constants.LOGGER.info("加载 {} 个变量,耗时 {} 毫秒", vars.size(), stopwatch.elapsed(TimeUnit.MILLISECONDS));
// }
//
// private static void writeDefault() {
// if (!dir.exists() && dir.mkdirs()) {
// JsonObject json1 = GSON.fromJson("{'player': 'list', 'cmd': 'list', 'alies': ['服务器在线'], 'allow_members': [], permission: 'ALL', 'enable': true}", JsonObject.class);
//
// // 尝试创建和写入文件
// try (FileWriter writerList = new FileWriter(new File(dir, "vars.json"))
// ) {
// GSON.toJson(json1, writerList);
// } catch (IOException e) {
// Constants.LOGGER.error("生成默认变量时出错", e);
// }
// }
// }
//}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evole.mods.mcbot.core.data;
package cn.evole.mods.mcbot.plugins.data;

import com.github.houbb.csv.annotation.Csv;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.evole.mods.mcbot.core.data;
package cn.evole.mods.mcbot.plugins.data;

import com.github.houbb.csv.annotation.Csv;
import lombok.AllArgsConstructor;
Expand Down
94 changes: 94 additions & 0 deletions common/src/main/java/cn/evole/mods/mcbot/util/CmdUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package cn.evole.mods.mcbot.util;

import cn.evole.mods.mcbot.api.cmd.Cmd;
import cn.evole.mods.mcbot.plugins.cmd.CmdHandler;
import cn.evole.onebot.sdk.event.message.GroupMessageEvent;
import com.google.common.collect.Maps;
import lombok.val;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @Project: McBot
* @Author: cnlimiter
* @CreateTime: 2024/8/16 23:38
* @Description:
*/
public class CmdUtils {

public static final Map<String, String> VARS = Maps.newConcurrentMap();
private final static String VAR_REGEX = "^(%(\\w+)+%)+";
public static String cmdParse(String command) {
// 找到最后一个空格的位置
int lastSpaceIndex = command.lastIndexOf(" ");
// 如果没有空格,则整个命令就是关键词
if (lastSpaceIndex == -1) {
return command;
}
// 返回最后一个空格之前的内容
return command.substring(0, lastSpaceIndex);
}

/**
* 变量解析
*
* @param event 消息事件
* @param cmd q群中指令
* @return 处理完的指令
*/
public static String varParse(GroupMessageEvent event, String cmd) {
VARS.putIfAbsent("user_id", event.getSender().getUserId());//初始化变量列表

val cmdStart = cmd.split(" ")[0];//部分指令头
String parseCmd = "";//定义最终命令用以返回
boolean useCmd = false;

for (Cmd cmd2 : CmdHandler.cmds.values()){//将含有昵称的指令替换为源命令
if (cmd2.getCmd().contains(cmdStart)) useCmd = true;//如果源命令包含部分指令头,则可以视为使用该源命令

for (String alies : cmd2.getAlies()){
if (cmd.contains(alies)) {
useCmd = true;//如果指令包含该Cmd中的任意一个alie,则可以视为使用该源命令
break;//跳出循环
}
}
if (useCmd) {
parseCmd = cmd2.getCmd();
break;//跳出循环
}
}

//正则查找变量
Pattern pattern = Pattern.compile(VAR_REGEX);
Matcher matcher = pattern.matcher(parseCmd);

while (matcher.find()) {//将 %变量% 替换为变量值
for (int i = 0; i < matcher.groupCount(); i++){
if (VARS.containsKey(matcher.group(i).replaceAll("%", ""))){
parseCmd = parseCmd.replace(matcher.group(i), VARS.get(matcher.group(i).replaceAll("%", "")));
}
}
}

val cmdEndSplits = parseCmd.split(" ");//拆分命令
val cmdSplits = cmd.split(" ");

for (int i = 0; i < cmdEndSplits.length; i++){
if (cmdEndSplits[i].equals("%")){//如果存在变量标志
cmdEndSplits[i] = cmdSplits[i];//则将指令中的变量对应内容传递到最终命令
}
}

val endCmd = new StringBuilder();
for (String key : cmdEndSplits) {
endCmd.append(key).append(" ");
}//拼接回命令

return endCmd.toString();



}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@
import cn.evole.onebot.sdk.event.message.GroupMessageEvent;
import cn.evole.onebot.sdk.event.message.MessageEvent;
import lombok.val;
import net.fabricmc.loader.api.FabricLoader;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cn.evole.mods.mcbot.Constants.*;

Expand Down Expand Up @@ -51,6 +47,9 @@ public class CQUtils {
for (ArrayMsg arrayMsg : msg){
try {
switch (arrayMsg.getType()){
case text -> {
message.append(arrayMsg.getData().get("text"));
}
case image -> {
val url = arrayMsg.getData().get("file");
if (ModConfig.get().getCommon().isImageOn() && Services.PLATFORM.isModLoaded("chatimage")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.api.event.server.ServerGameEvents;
import cn.evole.mods.mcbot.core.event.ICmdEvent;
import cn.evole.mods.mcbot.core.event.ITickEvent;
import cn.evole.mods.mcbot.common.event.ICmdEvent;
import cn.evole.mods.mcbot.common.event.ITickEvent;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
Expand Down
4 changes: 2 additions & 2 deletions forge/src/main/java/cn/evole/mods/mcbot/forge/McBotForge.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import cn.evole.mods.mcbot.Constants;
import cn.evole.mods.mcbot.McBot;
import cn.evole.mods.mcbot.api.event.server.ServerGameEvents;
import cn.evole.mods.mcbot.core.event.ICmdEvent;
import cn.evole.mods.mcbot.core.event.ITickEvent;
import cn.evole.mods.mcbot.common.event.ICmdEvent;
import cn.evole.mods.mcbot.common.event.ITickEvent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.event.RegisterCommandsEvent;
import net.minecraftforge.event.ServerChatEvent;
Expand Down

0 comments on commit 89d36a9

Please sign in to comment.