-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
```refactor(mcbot): reorganize modules and improve command handling- …
…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
Showing
18 changed files
with
193 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
common/src/main/java/cn/evole/mods/mcbot/api/cmd/CmdApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...vole/mods/mcbot/core/event/IBotEvent.java → ...le/mods/mcbot/common/event/IBotEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ole/mods/mcbot/core/event/IChatEvent.java → ...e/mods/mcbot/common/event/IChatEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...vole/mods/mcbot/core/event/ICmdEvent.java → ...le/mods/mcbot/common/event/ICmdEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...e/mods/mcbot/core/event/IPlayerEvent.java → ...mods/mcbot/common/event/IPlayerEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ole/mods/mcbot/core/event/ITickEvent.java → ...e/mods/mcbot/common/event/ITickEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
common/src/main/java/cn/evole/mods/mcbot/plugins/cmd/VarHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
// } | ||
// } | ||
//} |
2 changes: 1 addition & 1 deletion
2
...vole/mods/mcbot/core/data/ChatRecord.java → ...e/mods/mcbot/plugins/data/ChatRecord.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.../evole/mods/mcbot/core/data/UserBind.java → ...ole/mods/mcbot/plugins/data/UserBind.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
common/src/main/java/cn/evole/mods/mcbot/util/CmdUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters