@baipiaodajun/mcbots 1.0.3 → 1.0.5

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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/server.js +35 -47
package/README.md CHANGED
@@ -30,7 +30,7 @@ process.on('SIGTERM', shutdown);
30
30
  "start": "node index.js"
31
31
  },
32
32
  "dependencies": {
33
- "@baipiaodajun/mcbots": "^1.0.1"
33
+ "@baipiaodajun/mcbots": "latest"
34
34
  }
35
35
  }
36
36
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baipiaodajun/mcbots",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "Minecraft bot and status dashboard for multi-server management",
5
5
  "main": "server.js",
6
6
  "scripts": {
package/server.js CHANGED
@@ -1,7 +1,8 @@
1
1
  const mineflayer = require('mineflayer');
2
2
  const express = require('express');
3
3
  const PORT = process.env.SERVER_PORT || process.env.PORT || 3000 ;
4
- const CHAT = process.env.CHAT || false
4
+ const CHAT = process.env.CHAT || false ;
5
+ const MOVE = process.env.MOVE || false ;
5
6
  // Minecraft服务器配置
6
7
  const SERVERS = [
7
8
  {
@@ -25,7 +26,7 @@ if (process && process.env && process.env.SERVERS_JSON) {
25
26
  console.error('原因:', error.message);
26
27
  console.error('原始内容:\n', process.env.SERVERS_JSON);
27
28
  console.error('请检查 JSON 格式是否正确,例如引号、逗号是否缺失');
28
- console.error('容器即将退出...');
29
+ console.error('即将退出...');
29
30
  process.exit(1);
30
31
  }
31
32
  }
@@ -36,13 +37,14 @@ const BOT_CONFIG = {
36
37
  maxReconnectAttempts: 5,
37
38
  healthCheckInterval: 60000,
38
39
  viewDistance: 4,
39
- chat: CHAT
40
+ chat: CHAT,
41
+ move: MOVE
40
42
  };
41
43
 
42
44
  const SERVER_CONFIG = {
43
45
  statusCheckInterval: 30000,
44
46
  maxFailedAttempts: 5,
45
- resetTimeout: 180000
47
+ resetTimeout: 300000
46
48
  };
47
49
 
48
50
  // 全局状态存储
@@ -56,15 +58,13 @@ function generateUsername() {
56
58
  ];
57
59
 
58
60
  const animals = [
59
- 'Fox', 'Wolf', 'Bear', 'Panda', 'Tiger', 'Eagle', 'Shark',
60
- 'Mole', 'Badger', 'Otter', 'Raccoon', 'Frog', 'Hedgehog'
61
+ 'Fox', 'Wolf', 'Bear', 'Panda', 'Tiger', 'Eagle', 'Shark',
62
+ 'Mole', 'Badger', 'Otter', 'Cat', 'Frog', 'Dog'
61
63
  ];
62
64
 
63
65
  const adjective = adjectives[Math.floor(Math.random() * adjectives.length)];
64
66
  const animal = animals[Math.floor(Math.random() * animals.length)];
65
- const number = Math.random() > 0.5 ? Math.floor(Math.random() * 99) : ''; // 50% 加数字
66
-
67
- return `${adjective}${animal}${number}`;
67
+ return `${adjective}${animal}`;
68
68
  }
69
69
  // Minecraft机器人管理器
70
70
  class MinecraftBotManager {
@@ -82,11 +82,7 @@ class MinecraftBotManager {
82
82
  this.monitoringInterval = null;
83
83
 
84
84
  // 机器人名称池
85
- this.botNames = [
86
- generateUsername(),
87
- generateUsername(),
88
- generateUsername()
89
- ];
85
+ this.botNames = Array.from({ length: 20 }, () => generateUsername());
90
86
 
91
87
  // 注册到全局状态
92
88
  globalServerStatus.servers.set(`${host}:${port}`, {
@@ -109,14 +105,14 @@ class MinecraftBotManager {
109
105
  }
110
106
 
111
107
  // 创建Minecraft机器人
112
- async createBot() {
108
+ async createBot(botName) {
113
109
  if (this.currentBots >= this.maxBots) {
114
110
  console.log(`[${this.host}:${this.port}] 已达到最大机器人限制: ${this.maxBots}`);
115
111
  return null;
116
112
  }
117
-
118
- const botName = this.generateBotName();
119
-
113
+ if(!botName){
114
+ botName = this.generateBotName();
115
+ }
120
116
  try {
121
117
  console.log(`[${this.host}:${this.port}] 创建机器人: ${botName}`);
122
118
 
@@ -167,17 +163,8 @@ class MinecraftBotManager {
167
163
 
168
164
  bot.on('error', (error) => {
169
165
  console.log(`[${this.host}:${this.port}] 机器人 ${botName} 错误:`, error.message);
170
-
171
- // 重要修复:在错误事件中也处理断开连接
172
- if (error.message.includes('timed out') || error.message.includes('keepAlive')) {
173
- console.log(`[${this.host}:${this.port}] 检测到超时错误,强制断开机器人: ${botName}`);
166
+ if (this.activeBots.has(botName)) {
174
167
  this.handleBotDisconnect(botName);
175
- // 强制结束连接
176
- try {
177
- bot.end();
178
- } catch (e) {
179
- // 忽略结束时的错误
180
- }
181
168
  } else {
182
169
  this.handleBotFailure();
183
170
  }
@@ -197,21 +184,22 @@ class MinecraftBotManager {
197
184
  // 设置机器人行为
198
185
  setupBotBehavior(bot, botName) {
199
186
  // 随机移动
200
- setInterval(() => {
201
- if (bot.entity && Math.random() < 0.3) {
202
- const yaw = Math.random() * Math.PI * 2;
203
- const pitch = Math.random() * Math.PI - Math.PI / 2;
204
- bot.look(yaw, pitch, false);
205
-
206
- if (Math.random() < 0.2) {
207
- bot.setControlState('forward', true);
208
- setTimeout(() => {
209
- bot.setControlState('forward', false);
210
- }, 1000);
187
+ if (BOT_CONFIG.move){
188
+ setInterval(() => {
189
+ if (bot.entity && Math.random() < 0.3) {
190
+ const yaw = Math.random() * Math.PI * 2;
191
+ const pitch = Math.random() * Math.PI - Math.PI / 2;
192
+ bot.look(yaw, pitch, false);
193
+
194
+ if (Math.random() < 0.2) {
195
+ bot.setControlState('forward', true);
196
+ setTimeout(() => {
197
+ bot.setControlState('forward', false);
198
+ }, 1000);
199
+ }
211
200
  }
212
- }
213
- }, 5000);
214
-
201
+ }, 5000);
202
+ }
215
203
  // 随机聊天(如果启用)
216
204
  if (BOT_CONFIG.chat && Math.random() < 0.1) {
217
205
  setInterval(() => {
@@ -235,13 +223,13 @@ class MinecraftBotManager {
235
223
 
236
224
  // 延迟重连,避免频繁重连
237
225
  setTimeout(() => {
238
- this.maintainBots();
226
+ this.maintainBots(botName);
239
227
  }, BOT_CONFIG.reconnectDelay);
240
228
  } else {
241
229
  console.log(`[${this.host}:${this.port}] 机器人 ${botName} 不在活跃列表中,无需处理`);
242
230
  }
243
231
  }
244
-
232
+
245
233
  // 处理机器人失败
246
234
  handleBotFailure() {
247
235
  this.failedAttempts++;
@@ -262,7 +250,7 @@ class MinecraftBotManager {
262
250
  }
263
251
 
264
252
  // 维护机器人数目 - 增强版本
265
- maintainBots() {
253
+ maintainBots(botName) {
266
254
  const neededBots = this.minBots - this.currentBots;
267
255
 
268
256
  // console.log(`[${this.host}:${this.port}] 当前机器人: ${this.currentBots}, 需要: ${neededBots}, 失败次数: ${this.failedAttempts}`);
@@ -272,8 +260,8 @@ class MinecraftBotManager {
272
260
 
273
261
  for (let i = 0; i < neededBots; i++) {
274
262
  setTimeout(() => {
275
- this.createBot();
276
- }, i * 2000); // 每隔2秒启动一个
263
+ this.createBot(botName);
264
+ }, i * 10000); // 每隔10秒启动一个
277
265
  }
278
266
  } else if (neededBots > 0) {
279
267
  console.log(`[${this.host}:${this.port}] 由于失败次数过多,暂停创建新机器人`);