This repository was archived by the owner on May 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.js
More file actions
146 lines (129 loc) · 4.05 KB
/
bot.js
File metadata and controls
146 lines (129 loc) · 4.05 KB
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
// Moving the Bot
const mineflayer = require('mineflayer');
const moveBot = mineflayer.createBot({
host: 'localhost', // Replace with your server's IP
port: 25565, // Replace with your server's port
username: 'MoveBot', // Replace with desired bot username
});
moveBot.on('spawn', () => {
moveBot.chat('I am moving forward!');
moveBot.setControlState('forward', true);
setTimeout(() => {
moveBot.setControlState('forward', false);
moveBot.chat('I stopped moving!');
}, 5000);
});
// Breaking Blocks
const breakerBot = mineflayer.createBot({
host: 'localhost',
port: 25565,
username: 'BreakerBot',
});
breakerBot.on('spawn', () => {
breakerBot.chat('I am ready to break a block!');
const block = breakerBot.blockAt(breakerBot.entity.position.offset(0, -1, 0));
if (block) {
breakerBot.chat(`Breaking block: ${block.name}`);
breakerBot.dig(block, (err) => {
if (err) {
breakerBot.chat(`Failed to break block: ${err.message}`);
} else {
breakerBot.chat('Block broken!');
}
});
} else {
breakerBot.chat('No block to break!');
}
});
// Placing Blocks
const placerBot = mineflayer.createBot({
host: 'localhost',
port: 25565,
username: 'PlacerBot',
});
placerBot.on('spawn', () => {
placerBot.chat('I am ready to place a block!');
const blockToPlace = placerBot.inventory.items().find(item => item.name === 'dirt');
if (!blockToPlace) {
placerBot.chat('No dirt block found in inventory!');
return;
}
placerBot.equip(blockToPlace, 'hand', (err) => {
if (err) {
placerBot.chat(`Failed to equip block: ${err.message}`);
return;
}
const position = placerBot.entity.position.offset(1, 0, 0); // Place block to the right
const referenceBlock = placerBot.blockAt(position);
if (referenceBlock && referenceBlock.name === 'air') {
placerBot.placeBlock(referenceBlock, vec3(0, 1, 0), (err) => {
if (err) {
placerBot.chat(`Failed to place block: ${err.message}`);
} else {
placerBot.chat('Block placed successfully!');
}
});
} else {
placerBot.chat('Cannot place block here!');
}
});
});
// Combined Actions with Commands
const commandBot = mineflayer.createBot({
host: 'localhost',
port: 25565,
username: 'CommandBot',
});
commandBot.on('chat', (username, message) => {
if (username === commandBot.username) return;
if (message === 'move') {
commandBot.chat('Moving forward for 5 seconds...');
commandBot.setControlState('forward', true);
setTimeout(() => {
commandBot.setControlState('forward', false);
commandBot.chat('Stopped moving!');
}, 5000);
}
if (message === 'break') {
commandBot.chat('Breaking block beneath...');
const block = commandBot.blockAt(commandBot.entity.position.offset(0, -1, 0));
if (block) {
commandBot.dig(block, (err) => {
if (err) {
commandBot.chat(`Failed to break block: ${err.message}`);
} else {
commandBot.chat('Block broken!');
}
});
} else {
commandBot.chat('No block to break!');
}
}
if (message === 'place') {
commandBot.chat('Placing a dirt block next to me...');
const blockToPlace = commandBot.inventory.items().find(item => item.name === 'dirt');
if (!blockToPlace) {
commandBot.chat('No dirt block in inventory!');
return;
}
commandBot.equip(blockToPlace, 'hand', (err) => {
if (err) {
commandBot.chat(`Failed to equip block: ${err.message}`);
return;
}
const position = commandBot.entity.position.offset(1, 0, 0);
const referenceBlock = commandBot.blockAt(position);
if (referenceBlock && referenceBlock.name === 'air') {
commandBot.placeBlock(referenceBlock, vec3(0, 1, 0), (err) => {
if (err) {
commandBot.chat(`Failed to place block: ${err.message}`);
} else {
commandBot.chat('Block placed successfully!');
}
});
} else {
commandBot.chat('Cannot place block here!');
}
});
}
});