-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
622 lines (584 loc) · 23.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
const mineflayer = require("mineflayer");
const blockFinderPlugin = require("mineflayer-blockfinder")(mineflayer);
const minecraftData = require("minecraft-data");
const fs = require("fs");
const { Vec3 } = require("vec3");
const { pathfinder, Movements } = require("mineflayer-pathfinder");
const { GoalNear, GoalGetToBlock } = require("mineflayer-pathfinder").goals;
const blockCheckerInit = require("./utils/blockChecker");
const itemHandlerInit = require("./utils/item");
const produceManagerInit = require("./utils/produceManager");
require("dotenv").config();
const config = process.env;
// Initialize variables
let bot;
let reconnectInterval;
let farmingInterval;
let mcData;
let farmingInProgress = false; // Flag to track whether farming is in progress
let isConnected = false;
// Load and initialize data
try {
if (!fs.existsSync("data.json")) {
fs.writeFileSync("data.json", JSON.stringify({}));
}
} catch (err) {
console.error(err);
}
// Create bot function
function createBot() {
bot = mineflayer.createBot({
host: config.host,
port: Number(config.port),
auth: config.auth,
username: config.username || "",
password: config.password || "",
version: config.version || null,
});
// Initialize utility instances
const itemHandler = new itemHandlerInit(bot);
const blockChecker = new blockCheckerInit(bot);
const produceManager = new produceManagerInit(bot);
// Load plugins
bot.loadPlugin(blockFinderPlugin);
bot.loadPlugin(pathfinder);
// Bot event listeners
bot.on("chat", async (username, message) => {
console.log(`${username} said: ${message}`);
if (username === bot.username) return;
message = message.toLowerCase();
if (message.includes("!help")) {
bot.chat("commands: \n!set , !enable , !disable");
}
if (message.includes("!exit")) {
bot.chat("exiting");
bot.quit();
process.exit(0);
}
if (message.includes("!enable")) {
const data = JSON.parse(fs.readFileSync("data.json"));
if (!data) {
bot.chat(
"set farm by !set startcords endcords dustbincords\neg: !set 1,2,3 4,5,6 7,8,9"
);
} else {
if (!data.start || !data.end || !data.dustbin) {
bot.chat(
"set farm by !set startcords endcords dustbincords\neg: !set 1,2,3 4,5,6 7,8,9 "
);
return;
}
// enable logic
data.enable = true;
fs.writeFileSync("data.json", JSON.stringify(data));
bot.chat("enabled automatic farming");
}
}
if (message.includes("!disable")) {
bot.chat("disabled automatic farming");
const data = JSON.parse(fs.readFileSync("data.json"));
// disable logic
data.enable = false;
fs.writeFileSync("data.json", JSON.stringify(data));
}
if (message.includes("!set")) {
// split the message to get the coordinates
let cords = message.split(" ");
let start = cords[1]?.split(",");
let end = cords[2]?.split(",");
let dustbin = cords[3]?.split(",");
if (
!start ||
!end ||
!dustbin ||
start.length !== 3 ||
end.length !== 3 ||
dustbin.length !== 3
) {
bot.chat(
"Invalid coordinates format. Use '!set startcords endcords dustbincords', e.g., '!set 1,2,3 4,5,6 7,8,9'"
);
return;
}
// start[1] = String(Number(start[1]) - 1);
// end[1] = String(Number(end[1]) - 1);
// y level minecraft cordinates displayed in f3 is 1 more than y coordinate of block
dustbin[1] = String(Number(dustbin[1]) - 1);
const startVec = new Vec3(...start.map(Number));
const endVec = new Vec3(...end.map(Number));
const isValidBlockType = async (pos, mcData) => {
const block = await bot.blockAt(pos);
if (!block) return false;
const blockType = block.type;
const validBlockIds = [
mcData.blocksByName.dirt.id,
mcData.blocksByName.farmland.id,
mcData.blocksByName.grass_block.id,
mcData.blocksByName.water.id,
];
return validBlockIds.includes(blockType);
};
// Check all specified coordinates
const allCoords = [startVec, endVec];
for (const pos of allCoords) {
if (!(await isValidBlockType(pos, mcData))) {
bot.chat(
"Invalid block type detected in specified coordinates. Only dirt, farmland, grass, or water blocks are allowed."
);
return;
}
}
// Save valid data to JSON file
fs.writeFileSync(
"data.json",
JSON.stringify({ start, end, dustbin, enable: true })
);
bot.chat("Farm area set successfully.");
}
});
// Start the farming logic once the bot is logged in
// and clear the reconnect interval if it is set
bot.once("login", async () => {
console.log("Bot logged in ");
isConnected = true;
clearInterval(reconnectInterval);
mcData = minecraftData(bot.version);
bot.chat("farmer bot made by jagrit at your service!");
farmingInterval = setInterval(async () => {
if (!isConnected) {
console.log("Bot is not connected. Stopping farming.");
farmingInProgress = false; // Reset the farming flag
return; // Exit the interval if bot is not connected
}
if (!farmingInProgress) {
farmingInProgress = true; // Set the flag to indicate farming is in progress
console.log("Starting farming...");
await startFarming(mcData);
console.log("Farming complete.");
farmingInProgress = false; // Reset the flag once farming is complete
} else {
console.log("Farming already in progress, skipping...");
}
}, 10000);
});
bot.on("kicked", (reason) => {
console.log("Kicked for", reason);
isConnected = false;
farmingInProgress = false;
clearInterval(farmingInterval);
reconnect();
});
bot.on("end", () => {
console.log("Bot disconnected");
isConnected = false;
farmingInProgress = false;
clearInterval(farmingInterval);
reconnect();
});
bot.on("error", (err) => {
console.log("Error occurred:", err.message);
if (err.code.includes("ECONNRESET")) {
console.log("Connection reset by peer");
isConnected = false;
farmingInProgress = false;
clearInterval(farmingInterval);
reconnect();
}
});
// Main farming logic
async function startFarming() {
try {
bot.once("end", () => {
console.log("Bot disconnected");
isConnected = false;
farmingInProgress = false;
return;
});
const data = JSON.parse(fs.readFileSync("data.json"));
if (!data || !data.enable || !data.start || !data.end || !data.dustbin) {
return;
}
produceManager.setcords(data.start.map(Number), data.dustbin.map(Number));
const chest = await produceManager.findNearestChest(16, mcData);
//console.log("Chest found", chest);
//const wheatItems = bot.inventory.items() .filter((item) => item.name === "wheat");
if (chest) {
const space = await produceManager.calculateSpaceInChest(
chest,
"wheat",
mcData
);
if (typeof space === "string") {
console.log(`Failed to calculate space in chest: ${space}`);
return;
}
if (space > 0) {
data.tempDisabled = false;
fs.writeFileSync("data.json", JSON.stringify(data));
if (data.tempDisabled) {
bot.chat("Farming re-enabled, chest space available.");
}
} else {
bot.chat("Chest found but no space available, farming disabled.");
data.tempDisabled = true;
fs.writeFileSync("data.json", JSON.stringify(data));
}
}
if (data.tempDisabled) {
console.log("Farming is temporarily disabled due to full chest.");
return;
}
const startCoords = data.start.map(Number); // Convert coordinates to numbers
const endCoords = data.end.map(Number); // Convert coordinates to numbers
console.log("Farming function called");
const startX = Math.min(startCoords[0], endCoords[0]);
const endX = Math.max(startCoords[0], endCoords[0]);
const startY = startCoords[1];
const startZ = Math.min(startCoords[2], endCoords[2]);
const endZ = Math.max(startCoords[2], endCoords[2]);
let requiredseedscount = 0;
for (let x = startX; x <= endX; x++) {
for (let z = startZ; z <= endZ; z++) {
if (!isConnected) {
console.log("Bot disconnected. not starting farming.");
return;
}
await produceManager.cleanInventoryIfNeeded(
data.dustbin.map(Number),
data.start.map(Number),
data.end.map(Number),
mcData
);
// console.log("Checking block at", x, startY, z);
try {
if (await blockChecker.isDirt(new Vec3(x, startY, z), mcData)) {
requiredseedscount++;
const block = await bot.blockAt(new Vec3(x, startY, z));
const aboveBlock = await bot.blockAt(new Vec3(x, startY + 1, z));
if (!block) {
console.log(`Block at ${x}, ${startY}, ${z} is not found`);
continue; // Skip to next iteration if block is not found
}
console.log("Equipping hoe to till land at", x, startY, z);
// Equip hoe (assuming this function works as expected)
const equipped = await itemHandler.equipHoe();
// Ensure the block is defined and has a position
if (block && block.position) {
// Calculate target position just above the block
const targetPosition = block.position.offset(0.5, 1, 0.5); // Adjusted position just above the block
// Move towards the target position
if (equipped) {
if (bot.entity.position !== targetPosition) {
const goal = new GoalNear(
targetPosition.x,
targetPosition.y,
targetPosition.z,
0.5
); // Set a tolerance of 0.5 block radius
bot.pathfinder.setMovements(new Movements(bot, mcData));
bot.pathfinder.setGoal(goal);
await new Promise((resolve) => {
bot.once("goal_reached", async () => {
bot.pathfinder.setGoal(null); // Clear the goal once reached
console.log(
"unexpected goal (if not null)",
bot.pathfinder.goal
);
console.log(
`Bot reached position above block at ${x}, ${startY}, ${z}`
);
if (
aboveBlock &&
aboveBlock.type !== mcData.blocksByName.air.id
) {
await bot.dig(aboveBlock);
await itemHandler.equipHoe();
}
// Look at the block position
await bot.lookAt(block.position);
// If equipped with hoe, activate the block (till the land)
console.log("equipped", equipped);
if (equipped) {
console.log("activating block");
await bot.activateBlock(block);
console.log("activated block");
}
resolve(); // Resolve the promise once the action is complete
});
});
} else {
console.log("already at target position");
if (
aboveBlock &&
aboveBlock.type !== mcData.blocksByName.air.id
) {
await bot.dig(aboveBlock);
}
await bot.lookAt(block.position);
// If equipped with hoe, activate the block (till the land)
if (equipped) {
console.log("activating block");
await bot.activateBlock(block);
console.log("activated block");
}
}
}
// Wait for bot to reach the target position
} else {
console.error(
`Block or block position is undefined at ${x}, ${startY}, ${z}`
);
}
} else if (
await blockChecker.isFarmland(new Vec3(x, startY, z), mcData)
) {
requiredseedscount++;
const block = await bot.blockAt(new Vec3(x, startY, z));
if (!block) {
console.log(`Block at ${x}, ${startY + 1}, ${z} is not found`);
continue; // Skip to next iteration if block is not found
}
if (
await blockChecker.isWheat(new Vec3(x, startY + 1, z), mcData)
) {
const wheatBlock = await bot.blockAt(
new Vec3(x, startY + 1, z)
);
const wheatAge = wheatBlock.metadata;
//console.log("Wheat age", wheatAge);
if (wheatAge === 7) {
console.log("Harvesting wheat at", x, startY + 1, z);
if (block && block.position) {
// Calculate target position just above the block
const targetPosition = block.position.offset(0.5, 1, 0.5); // Adjusted position just above the block
const currentPos = bot.entity.position;
// Move towards the target position
if (currentPos !== targetPosition) {
const goal = new GoalGetToBlock(
targetPosition.x,
targetPosition.y,
targetPosition.z
// 0.5
); // Set a tolerance of 0.5 block radius
bot.pathfinder.setMovements(new Movements(bot));
bot.pathfinder.setGoal(goal);
} else {
bot.emit("goal_reached");
}
// Wait for bot to reach the target position
await new Promise((resolve) => {
bot.once("goal_reached", async () => {
bot.pathfinder.setGoal(null); // Clear the goal once reached
console.log(
"unexpected goal (if not null)",
bot.pathfinder.goal
);
console.log(
`Bot reached position above block at ${x}, ${
startY + 1
}, ${z}`
);
// Look at the block position
bot.lookAt(block.position);
// If equipped with seeds, activate the block (plant the seeds)
await bot.dig(wheatBlock);
// await produceManager.collectWheat();
// Delay for a short period to allow items to drop
await delay(1000);
const hasSeeds =
itemHandler.hasItemByName("wheat_seeds");
console.log("hasSeeds", hasSeeds);
if (hasSeeds) {
console.log(
"Equipping seeds to plant at",
x,
startY + 1,
z
);
const seedsEquipped = await itemHandler.equipItem(
"wheat_seeds"
);
if (seedsEquipped) {
console.log("activating block");
await bot.activateBlock(block);
console.log("activated block");
}
} else {
console.log("No seeds found in inventory");
}
resolve(); // Resolve the promise once the action is complete
});
});
}
}
} else {
const hasSeeds = itemHandler.hasItemByName("wheat_seeds");
// console.log("hasSeeds", hasSeeds);
if (hasSeeds) {
console.log("Equipping seeds to plant at", x, startY + 1, z);
const seedsEquipped = await itemHandler.equipItem(
"wheat_seeds"
);
// Ensure the block is defined and has a position
if (block && block.position) {
// Calculate target position just above the block
// const targetPosition = block.position.offset(0.5, 1, 0.5); // Adjusted position just above the block
// Move towards the target position
console.log(
"blockPosition ",
block.position,
"\n",
"blockPosition rounded ",
block.position.rounded()
);
const currentPos = bot.entity.position;
console.log(
"currentPos ",
currentPos,
"\n",
"currentPos rounded",
currentPos.rounded()
);
if (
!currentPos.floored().equals(block.position.rounded())
) {
const goal = new GoalGetToBlock(
block.position.x,
block.position.y + 1,
block.position.z
// 0.8
); // Set a tolerance of 0.5 block radius
const MovementsAlowed = new Movements(bot);
// MovementsAlowed.allowSprinting = false;
bot.pathfinder.setMovements(MovementsAlowed);
bot.pathfinder.goto(goal);
console.log("moving to target position");
await new Promise((resolve) => {
bot.once("goal_reached", async () => {
// bot.pathfinder.setGoal(null); // Clear the goal once reached
console.log(
"unexpected goal (if not null)",
bot.pathfinder.goal
);
console.log("bot is on targetPosition");
// Look at the block position
await bot.lookAt(block.position);
// If equipped with seeds, activate the block (plant the seeds)
console.log("seedsEquipped", seedsEquipped);
if (seedsEquipped) {
console.log("activating block");
await bot.activateBlock(block);
console.log("activated block");
}
resolve(); // Resolve the promise once the action is complete
});
});
} else {
console.log("already at target position");
await bot.lookAt(block.position);
// If equipped with seeds, activate the block (plant the seeds)
if (seedsEquipped) {
await bot.activateBlock(block);
}
}
// Wait for bot to reach the target position
} else {
console.error(
`Block or block position is undefined at ${x}, ${
startY + 1
}, ${z}`
);
}
} else {
//console.log("No seeds found in inventory");
}
}
}
} catch (error) {
console.error(
`Error while processing block at ${x}, ${startY}, ${z}:`,
error
);
// Handle any errors that occur during block processing
}
}
}
if (!isConnected) {
console.log("Bot disconnected. Stopping farming.");
return;
}
console.log("requiredseedscount", requiredseedscount);
await produceManager.accurateCleanExtraseeds(requiredseedscount, mcData);
if (!isConnected) {
console.log("Bot disconnected. Stopping farming.");
return;
}
const wheatDeposit = await produceManager.depositWheatIfNeeded(
16,
mcData
);
if (wheatDeposit.success) {
console.log(
`Deposited ${wheatDeposit.depositedCount} wheat into chest.`
);
} else {
console.log(`Failed to deposit wheat: ${wheatDeposit.reason}`);
if (wheatDeposit.reason === "chestfull") {
console.log("Disabling farming temporarily due to full chest");
data.tempDisabled = true;
fs.writeFileSync("data.json", JSON.stringify(data));
} else if (wheatDeposit.reason === "nochest") {
bot.chat(
"No chest found near startCoords you gave during set command."
);
} else if (
wheatDeposit.reason === "notachest" ||
wheatDeposit.reason === "chestnotavailable"
) {
bot.chat("No chest found nearby to deposit wheat.");
} else if (wheatDeposit.reason === "chestopenfailed") {
bot.chat("Failed to open chest to deposit wheat.");
} else if (wheatDeposit.reason === "nowheat") {
// bot.chat("No wheat in inventory to deposit.");
} else {
bot.chat(`Failed to deposit wheat: ${wheatDeposit.reason}`);
}
}
} catch (error) {
console.error("Error in startFarming", error);
farmingInProgress = false;
}
}
}
// Reconnect bot function
function reconnect() {
// return if bot is already connected
if (bot && bot.connected) {
console.log("Bot already connected.");
return;
}
// set reconnect interval to try to reconnect bot every 20 seconds
// if reconnect interval is not set
if (!reconnectInterval) {
reconnectInterval = setInterval(() => {
console.log("Reconnecting bot...");
createBot();
}, 20000);
}
}
// Delay function
function delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
createBot();
// for nodejs version higher than 15
// handle unhandled promise rejections
process.on("unhandledRejection", (err) => {
console.error("Unhandled promise rejection:", err);
// process.exit(1);
});
// handle uncaught exceptions
process.on("uncaughtException", (err) => {
console.error("Uncaught exception:", err);
// process.exit(1);
});