Skip to content

Commit 5ed7580

Browse files
committed
feat(bot): 优化重连机制并更新配置版本
- 重构了 ClientManager 中的重连逻辑,改用异步任务调度替代同步等待 - 添加了连接状态检查功能,避免重复连接尝试 - 更新配置管理器,将版本从5升级到6 - 移除过时的 CommandExecutionSort 配置项 - 添加新的 CommandSender 配置项,默认值为 Hybrid - 更新 Gradle 模块配置,添加 BungeeCord 和 Velocity 服务器支持
1 parent ae43afc commit 5ed7580

4 files changed

Lines changed: 71 additions & 50 deletions

File tree

.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/Bot/src/main/kotlin/cn/huohuas001/bot/ClientManager.kt

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -160,44 +160,65 @@ object ClientManager {
160160
private fun performReconnect() {
161161
val plugin = BotShared.getPlugin()
162162

163-
while (shouldReconnect && ReconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
164-
ReconnectAttempts++
163+
if (!shouldReconnect) {
164+
isReconnecting.set(false)
165+
return
166+
}
165167

166-
// 检查是否已经有活跃连接
167-
if (isOpen()) {
168-
plugin.log_info("检测到已有活跃连接,停止重连")
169-
break
170-
}
168+
// 检查次数是否超限
169+
if (ReconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
170+
plugin.log_warning("重连尝试已达到最大次数,将不再尝试重新连接。")
171+
isReconnecting.set(false)
172+
ReconnectAttempts = 0
173+
return
174+
}
171175

172-
plugin.log_info("正在尝试重新连接,这是第($ReconnectAttempts/$MAX_RECONNECT_ATTEMPTS)次连接")
176+
ReconnectAttempts++
173177

174-
// 尝试连接
175-
if (connectServer()) {
176-
// 连接成功,等待一段时间确认连接稳定
177-
Thread.sleep(1000)
178-
if (isOpen()) {
179-
plugin.log_info("重连成功!")
180-
break
181-
}
182-
}
178+
// 检查是否已经有活跃连接
179+
if (isOpen()) {
180+
plugin.log_info("检测到已有活跃连接,停止重连")
181+
isReconnecting.set(false)
182+
ReconnectAttempts = 0
183+
return
184+
}
183185

184-
// 如果不是最后一次尝试,等待后继续
185-
if (ReconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
186-
try {
187-
Thread.sleep(RECONNECT_DELAY * 1000) // 等待重连延迟
188-
} catch (e: InterruptedException) {
189-
break
190-
}
186+
plugin.log_info("正在尝试重新连接,这是第($ReconnectAttempts/$MAX_RECONNECT_ATTEMPTS)次连接")
187+
188+
// 尝试连接
189+
if (connectServer()) {
190+
// 连接发起成功,1秒后检查连接状态
191+
currentTask = plugin.submitLater(20L) {
192+
checkConnectionStatus()
191193
}
194+
} else {
195+
// 连接发起失败,进入下一次等待
196+
scheduleNextAttempt()
192197
}
198+
}
193199

194-
// 重连结束,无论成功与否都要重置状态
195-
if (ReconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
196-
plugin.log_warning("重连尝试已达到最大次数,将不再尝试重新连接。")
200+
private fun checkConnectionStatus() {
201+
val plugin = BotShared.getPlugin()
202+
if (isOpen()) {
203+
plugin.log_info("重连成功!")
204+
isReconnecting.set(false)
205+
ReconnectAttempts = 0
206+
} else {
207+
scheduleNextAttempt()
197208
}
209+
}
198210

199-
isReconnecting.set(false)
200-
ReconnectAttempts = 0
211+
private fun scheduleNextAttempt() {
212+
if (ReconnectAttempts < MAX_RECONNECT_ATTEMPTS) {
213+
val plugin = BotShared.getPlugin()
214+
// 等待重连延迟
215+
currentTask = plugin.submitLater(RECONNECT_DELAY * 20L) {
216+
performReconnect()
217+
}
218+
} else {
219+
// 再次调用以触发结束逻辑
220+
performReconnect()
221+
}
201222
}
202223

203224
fun clientReconnect() {

server/Spigot/src/main/kotlin/cn/huohuas001/huhobot/spigot/managers/ConfigManager.kt

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ConfigManager(private val plugin: HuHoBotSpigot) {
1616

1717
private val configFile: File = File(plugin.dataFolder, "config.yml")
1818
private val oldConfigFile: File = File(plugin.dataFolder, "config_old.yml")
19-
private val version: Int = 5
19+
private val version: Int = 6
2020

2121
init {
2222
if (checkConfig()) {
@@ -33,20 +33,19 @@ class ConfigManager(private val plugin: HuHoBotSpigot) {
3333
return true
3434
}
3535

36-
// 添加从版本4到版本5的迁移方法
37-
private fun migrateFromV4ToV5(oldConfig: FileConfiguration, newConfig: FileConfiguration) {
38-
// 添加新的 CommandExecutionSort 配置项,默认值为标准执行顺序
39-
if (!newConfig.contains("CommandExecutionSort")) {
40-
newConfig.set("CommandExecutionSort", listOf(
41-
"NATIVE",
42-
"DEDICATED_SERVER",
43-
"MINECRAFT_SERVER",
44-
"SIMULATE_CONSOLE"
45-
))
46-
plugin.pluginLogger.info("已添加新的配置项: CommandExecutionSort")
36+
// 添加从版本5到版本6的迁移方法
37+
private fun migrateFromV5ToV6(oldConfig: FileConfiguration, newConfig: FileConfiguration) {
38+
// 移除旧的 CommandExecutionSort 配置项
39+
if (newConfig.contains("CommandExecutionSort")) {
40+
newConfig.set("CommandExecutionSort", null)
41+
plugin.pluginLogger.info("已移除过时的配置项: CommandExecutionSort")
4742
}
4843

49-
// 可以在这里添加其他从版本4到版本5的迁移逻辑
44+
// 添加新的 CommandSender 配置项,默认值为 Hybrid
45+
if (!newConfig.contains("CommandSender")) {
46+
newConfig.set("CommandSender", "Hybrid")
47+
plugin.pluginLogger.info("已添加新的配置项: CommandSender")
48+
}
5049
}
5150

5251
fun migrateConfig() {
@@ -70,15 +69,14 @@ class ConfigManager(private val plugin: HuHoBotSpigot) {
7069

7170
// 根据旧版本号执行相应的迁移
7271
when (oldVersion) {
73-
(version-1) -> {
74-
// 从版本3迁移到版本4
75-
migrateFromV4ToV5(oldConfig, newConfig)
76-
var oldVersion = version-1
77-
plugin.pluginLogger.info("配置文件从版本$oldVersion 升级到$version")
72+
5 -> {
73+
// 从版本5迁移到版本6
74+
migrateFromV5ToV6(oldConfig, newConfig)
75+
plugin.pluginLogger.info("配置文件从版本5升级到6")
7876
}
7977
// 如果还有其他旧版本,可以继续添加
80-
in 0..3 -> {
81-
// 对于更旧的版本,可以复用之前的迁移逻辑或直接使用默认配置
78+
in 0..4 -> {
79+
// 对于更旧的版本,直接使用默认配置
8280
plugin.pluginLogger.warning("检测到较旧的配置版本($oldVersion),将使用默认配置")
8381
}
8482
else -> {

server/Spigot/src/main/resources/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ customCommand:
3131

3232
CommandSender: "Hybrid"
3333

34-
version: 5
34+
version: 6

0 commit comments

Comments
 (0)