@baipiaodajun/mcbots 1.2.2 → 1.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/package.json +1 -1
- package/server.js +48 -22
package/README.md
CHANGED
package/package.json
CHANGED
package/server.js
CHANGED
|
@@ -354,9 +354,23 @@ class MinecraftBotManager {
|
|
|
354
354
|
console.log(`[${this.host}:${this.port}] 已达到最大机器人限制: ${this.maxBots}`);
|
|
355
355
|
return null;
|
|
356
356
|
}
|
|
357
|
-
|
|
358
|
-
|
|
357
|
+
|
|
358
|
+
if (!botName) {
|
|
359
|
+
if (this.pendingReconnect.size > 0) {
|
|
360
|
+
botName = this.pendingReconnect.keys().next().value;
|
|
361
|
+
this.pendingReconnect.delete(botName); // 用完就移除,避免重复分配
|
|
362
|
+
debugPrint(`[${this.host}:${this.port}] 优先重用待重连名字: ${botName} (剩余待重连: ${this.pendingReconnect.size})`);
|
|
363
|
+
} else {
|
|
364
|
+
botName = this.generateBotName();
|
|
365
|
+
debugPrint(`[${this.host}:${this.port}] 无待重连名字,生成新名字: ${botName}`);
|
|
366
|
+
}
|
|
367
|
+
} else {
|
|
368
|
+
if (this.pendingReconnect.has(botName)) {
|
|
369
|
+
this.pendingReconnect.delete(botName);
|
|
370
|
+
debugPrint(`[${this.host}:${this.port}] 使用指定的重连名字: ${botName} (已从待重连名单移除)`);
|
|
371
|
+
}
|
|
359
372
|
}
|
|
373
|
+
|
|
360
374
|
try {
|
|
361
375
|
console.log(`[${this.host}:${this.port}] 创建机器人: ${botName}`);
|
|
362
376
|
const bot = mineflayer.createBot({
|
|
@@ -387,6 +401,12 @@ class MinecraftBotManager {
|
|
|
387
401
|
|
|
388
402
|
// 设置机器人事件
|
|
389
403
|
setupBotEvents(bot, botName) {
|
|
404
|
+
const triggerDisconnect = (reason) => {
|
|
405
|
+
// 检查机器人是否还在活跃列表中,避免重复调用
|
|
406
|
+
if (this.activeBots.has(botName)) {
|
|
407
|
+
this.handleBotDisconnect(botName);
|
|
408
|
+
}
|
|
409
|
+
};
|
|
390
410
|
bot.on('login', () => {
|
|
391
411
|
console.log(`[${this.host}:${this.port}] 机器人 ${botName} 登录成功`);
|
|
392
412
|
if (this.timeout) {
|
|
@@ -409,25 +429,9 @@ class MinecraftBotManager {
|
|
|
409
429
|
debugPrint(`[${this.host}:${this.port}] ${botName} 收到消息: ${text}`);
|
|
410
430
|
});
|
|
411
431
|
|
|
412
|
-
bot.on('error', (
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
this.handleBotDisconnect(botName);
|
|
416
|
-
// } else {
|
|
417
|
-
// this.handleBotFailure(botName);
|
|
418
|
-
// }
|
|
419
|
-
});
|
|
420
|
-
|
|
421
|
-
bot.on('end', (reason) => {
|
|
422
|
-
console.log(`[${this.host}:${this.port}] 机器人 ${botName} 断开连接:`, reason);
|
|
423
|
-
// console.log(`[${this.host}:${this.port}] ${botName}:失败次数${this.failedAttempts}`);
|
|
424
|
-
this.handleBotDisconnect(botName);
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
bot.on('kicked', (reason) => {
|
|
428
|
-
console.log(`[${this.host}:${this.port}] 机器人 ${botName} 被踢出:`, reason);
|
|
429
|
-
this.handleBotDisconnect(botName);
|
|
430
|
-
});
|
|
432
|
+
bot.on('error', (err) => triggerDisconnect(`错误: ${err.message}`));
|
|
433
|
+
bot.on('end', (reason) => triggerDisconnect(`断开: ${reason}`));
|
|
434
|
+
bot.on('kicked', (reason) => triggerDisconnect(`被踢: ${reason}`));
|
|
431
435
|
}
|
|
432
436
|
|
|
433
437
|
// 设置机器人行为
|
|
@@ -460,12 +464,34 @@ class MinecraftBotManager {
|
|
|
460
464
|
}
|
|
461
465
|
|
|
462
466
|
handleBotDisconnect(botName) {
|
|
463
|
-
|
|
467
|
+
// 获取当前的机器人实例
|
|
468
|
+
const bot = this.activeBots.get(botName);
|
|
469
|
+
|
|
470
|
+
if (bot) {
|
|
464
471
|
debugPrint(`[${this.host}:${this.port}] 从活跃列表中移除机器人: ${botName}`);
|
|
472
|
+
|
|
473
|
+
// --- 核心修复:物理销毁机器人对象 ---
|
|
474
|
+
try {
|
|
475
|
+
// 1. 移除所有事件监听器,防止它继续打印日志或触发回调
|
|
476
|
+
bot.removeAllListeners();
|
|
477
|
+
|
|
478
|
+
// 2. 尝试优雅退出
|
|
479
|
+
if (typeof bot.quit === 'function') bot.quit();
|
|
480
|
+
|
|
481
|
+
// 3. 强制销毁底层 socket 确保连接彻底断开
|
|
482
|
+
if (bot._client && bot._client.socket) {
|
|
483
|
+
bot._client.socket.destroy();
|
|
484
|
+
}
|
|
485
|
+
} catch (err) {
|
|
486
|
+
debugPrint(`[${this.host}:${this.port}] 销毁幽灵机器人 ${botName} 时出错: ${err.message}`);
|
|
487
|
+
}
|
|
488
|
+
// ----------------------------------
|
|
489
|
+
|
|
465
490
|
this.activeBots.delete(botName);
|
|
466
491
|
this.currentBots = Math.max(0, this.currentBots - 1);
|
|
467
492
|
this.updateStatus();
|
|
468
493
|
this.pendingReconnect.add(botName);
|
|
494
|
+
|
|
469
495
|
debugPrint(`[${this.host}:${this.port}] 当前活跃机器人数量: ${this.currentBots}, 目标: ${this.minBots}`);
|
|
470
496
|
|
|
471
497
|
setTimeout(() => {
|