Skip to content
This repository was archived by the owner on Oct 23, 2025. It is now read-only.

Commit b441c79

Browse files
committed
feat(chat): 增加聊天事件处理功能
- 新增 onChat 类实现聊天事件监听 - 增加配置项支持聊天消息转发 - 优化配置文件结构和版本管理 - 添加 bStats 统计支持
1 parent 253b2c8 commit b441c79

6 files changed

Lines changed: 964 additions & 22 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = 'cn.huohuas001'
7-
version = '1.2.4'
7+
version = '1.2.5'
88

99
repositories {
1010
mavenCentral()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.huohuas001.huHoBot.GameEvent;
2+
3+
import cn.huohuas001.huHoBot.HuHoBot;
4+
import com.alibaba.fastjson2.JSONObject;
5+
import org.bukkit.configuration.file.FileConfiguration;
6+
import org.bukkit.event.EventHandler;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.player.AsyncPlayerChatEvent;
9+
10+
public class onChat implements Listener {
11+
public onChat() {
12+
}
13+
14+
@EventHandler
15+
public void onChat(AsyncPlayerChatEvent event) {
16+
String message = event.getMessage();
17+
String playerName = event.getPlayer().getName();
18+
HuHoBot plugin = HuHoBot.getPlugin();
19+
FileConfiguration config = plugin.getConfig();
20+
String format = config.getString("chatFormat.from_game");
21+
String prefix = config.getString("chatFormat.post_prefix");
22+
boolean isPostChat = config.getBoolean("chatFormat.post_chat");
23+
String serverId = config.getString("serverId");
24+
if (message.startsWith(prefix) && isPostChat) {
25+
JSONObject body = new JSONObject();
26+
body.put("serverId", serverId);
27+
String formated = format.replace("{name}", playerName).replace("{msg}", message.substring(prefix.length()));
28+
body.put("msg", formated);
29+
HuHoBot.getClientManager().getClient().sendMessage("chat", body);
30+
}
31+
32+
}
33+
}

src/main/java/cn/huohuas001/huHoBot/HuHoBot.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.huohuas001.huHoBot;
22

3+
import cn.huohuas001.huHoBot.GameEvent.onChat;
34
import cn.huohuas001.huHoBot.NetEvent.*;
45
import cn.huohuas001.huHoBot.Tools.*;
56
import com.alibaba.fastjson2.JSONObject;
@@ -91,7 +92,7 @@ public void onEnable() {
9192
this.customCommand = new CustomCommand(this);
9293
customCommand.loadCommandsFromConfig();
9394

94-
//getServer().getPluginManager().registerEvents(new PlayerLoginListener(), this);
95+
getServer().getPluginManager().registerEvents(new onChat(), this);
9596

9697
//初始化事件
9798
totalRegEvent();
@@ -100,15 +101,11 @@ public void onEnable() {
100101
clientManager = new WebsocketClientManager();
101102
clientManager.connectServer();
102103

104+
int pluginId = 25692;
105+
Metrics metrics = new Metrics(this, pluginId);
103106

104-
/*new UpdateChecker(this, 121838).getVersion(version -> {
105-
if (this.getDescription().getVersion().equals(version)) {
106-
logger.info("There is not a new update available.");
107-
} else {
108-
logger.warning(ChatColor.GOLD + "There is a new update available.");
109-
}
110-
});*/
111-
107+
// Optional: Add custom charts
108+
metrics.addCustomChart(new Metrics.SimplePie("chart_id", () -> "Use HuHoBot"));
112109

113110
logger.info("HuHoBot Loaded. By HuoHuas001");
114111
}
@@ -160,7 +157,7 @@ public boolean reloadBotConfig() {
160157
*/
161158
public boolean isBind() {
162159
FileConfiguration config = getConfig();
163-
return config.get("hashKey") != null;
160+
return config.get("hashKey") != null || (!config.get("hashKey").equals(""));
164161
}
165162

166163
/**
@@ -200,10 +197,11 @@ public void runCommand(String command) {
200197
* @param command 命令
201198
* @param packId 消息包ID
202199
*/
203-
public void
204-
runCommand(String command, String packId) {
205-
String sendCqMsg = ServerManager.sendCmd(command, true, true);
206-
clientManager.getClient().respone("已执行.\n" + sendCqMsg, "success", packId);
200+
public void runCommand(String command, String packId) {
201+
HuHoBot.getScheduler().runTaskAsynchronously(() -> {
202+
String sendCmdMsg = ServerManager.sendCmd(command, true, true);
203+
clientManager.getClient().respone("已执行.\n" + sendCmdMsg, "success", packId);
204+
});
207205
}
208206

209207
/**

src/main/java/cn/huohuas001/huHoBot/Tools/ConfigManager.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@ public ConfigManager(HuHoBot plugin) {
1919
this.plugin = plugin;
2020
this.configFile = new File(plugin.getDataFolder(), "config.yml");
2121
this.oldConfigFile = new File(plugin.getDataFolder(), "config_old.yml");
22-
this.version = 2;
22+
this.version = 3;
2323
}
2424

2525
//检测是否该修改配置文件
2626
public boolean checkConfig() {
2727
if (configFile.exists()) {
2828
int version = plugin.getConfig().contains("version") ? plugin.getConfig().getInt("version") : -1;
29-
if (this.version > version) {
30-
return false;
31-
}
29+
return this.version > version;
3230
}
3331
return true;
3432
}
@@ -71,6 +69,7 @@ public void migrateConfig() {
7169
plugin.saveDefaultConfig();
7270
plugin.reloadConfig();
7371
FileConfiguration newConfig = plugin.getConfig();
72+
newConfig.set("version", this.version);
7473

7574
// 3. 迁移旧配置数据
7675
if (oldConfigFile.exists()) {
@@ -82,27 +81,36 @@ public void migrateConfig() {
8281

8382
// 迁移聊天格式
8483
migrateNested(oldConfig, newConfig, "chatFormatGame", "chatFormat.from_game");
84+
migrateNested(oldConfig, newConfig, "chatFormat.from_game", "chatFormat.from_game");
8585
migrateNested(oldConfig, newConfig, "chatFormatGroup", "chatFormat.from_group");
86+
migrateNested(oldConfig, newConfig, "chatFormat.from_group", "chatFormat.from_group");
87+
if (!newConfig.contains("chatFormat.post_chat")) {
88+
newConfig.set("chatFormat.post_chat", true); // 设置默认值
89+
}
90+
if (!newConfig.contains("chatFormat.post_prefix")) {
91+
newConfig.set("chatFormat.post_prefix", ""); // 设置默认值
92+
}
8693

8794
// 迁移MOTD设置
8895
migrateServerUrl(oldConfig, newConfig);
8996

9097
// 迁移白名单命令
9198
migrateNested(oldConfig, newConfig, "addWhiteListCmd", "whiteList.add");
99+
migrateNested(oldConfig, newConfig, "whiteList.add", "whiteList.add");
92100
migrateNested(oldConfig, newConfig, "delWhiteListCmd", "whiteList.del");
101+
migrateNested(oldConfig, newConfig, "whiteList.del", "whiteList.del");
93102

94103
// 迁移自定义命令(保持结构不变)
95104
if (oldConfig.isConfigurationSection("customCommand")) {
96105
newConfig.set("customCommand", oldConfig.get("customCommand"));
97106
}
98107

99-
// 设置新版本号
100-
newConfig.set("version", this.version);
101108

102109
// 保存更新后的配置
103-
plugin.saveConfig();
104110
plugin.getLogger().info("配置文件迁移完成");
105111
}
112+
113+
plugin.saveConfig();
106114
} catch (IOException e) {
107115
plugin.getLogger().severe("配置文件迁移失败: " + e.getMessage());
108116
plugin.getLogger().severe(e.getStackTrace().toString());

0 commit comments

Comments
 (0)