-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
274 lines (228 loc) · 7.75 KB
/
index.js
File metadata and controls
274 lines (228 loc) · 7.75 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
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
const mineflayer = require('mineflayer')
const { GoalFollow, GoalBlock, pathfinder } = require('mineflayer-pathfinder')
const config = require('./config.json')
bot.loadPlugin(pathfinder)
const is_a_AFK_Bot = config.AFK_Mod
let MinusPlayer_followSB = { value: false, player: undefined }
const bot = mineflayer.createBot({
host: config.host,
port: config.port,
username: config.username,
version: config.version
})
const checkAFK = () => {
if (is_a_AFK_Bot) {
bot.chat('The bot is in AFK mode');
return true;
}
return false;
};
bot.on('spawn', () => {
console.log("Logged In");
if (is_a_AFK_Bot) {
bot.setControlState('forward', true)
setTimeout(() => {
bot.setControlState('forward', false)
console.log('Stopped moving forward')
}, 5000)
}
});
bot.on('chat', (username, message) => {
if (username === bot.username || checkAFK()) return;
const parts = message.split(' ');
switch (message) {
case 'hi':
case 'hello':
bot.chat(`Hi ${username} !`);
break;
case message.startsWith('whisper ') && message:
const playerName = parts[1];
const msg = parts.slice(2).join(' ');
bot.whisper(playerName, `Someone asked me to tell you: ${msg}`);
break;
case message.startsWith('follow ') && message:
const targetPlayer = parts[1] === 'me' ? username : parts[1];
MinusPlayer_followSB.value = true;
MinusPlayer_followSB.player = targetPlayer;
const goal = new GoalFollow(targetPlayer.entity, 3);
bot.pathfinder.setGoal(goal, true);
break;
case 'stopFollow':
if (!MinusPlayer_followSB.value) {
bot.chat('The bot isn\'t following anyone...');
return;
}
MinusPlayer_followSB.value = false;
MinusPlayer_followSB.player = undefined;
bot.pathfinder.setGoal(null);
bot.chat("I'm not following anyone now.");
break;
case message.startsWith('drop '):
handleDropCommand(parts);
break;
case message.startsWith('found wood'):
handleWoodCollection(parts);
break;
case 'equip armor':
equipArmor();
break;
case message.startsWith('tp '):
handleTeleportCommand(parts, username);
break;
default:
break;
}
});
// Handle drop command
const handleDropCommand = (parts) => {
let quantity = parseInt(parts[2]) || 1;
if (isNaN(quantity) || quantity <= 0) {
bot.chat('Please specify a valid quantity to drop.');
return;
}
const item = bot.inventory.findInventoryItem(parts[1]);
if (item && item.count >= quantity) {
bot.chat(`Dropping ${quantity}x ${parts[1]}`);
bot.tossStack(item, quantity);
} else {
bot.chat(`I don't have enough ${parts[1]} to drop!`);
}
};
// Handle wood collection
const handleWoodCollection = (parts) => {
let quantity = parseInt(parts[2]);
if (isNaN(quantity) || quantity <= 0) {
bot.chat('Please specify a valid quantity of wood.');
return;
}
bot.chat(`I will find and collect ${quantity} pieces of wood for you!`);
let collectedWood = 0;
const targetWood = quantity;
function collectWood() {
const wood = bot.findBlock({
matching: (block) => block.name.includes('log'),
maxDistance: 100
});
if (wood) {
bot.pathfinder.setGoal(new GoalBlock(wood.position.x, wood.position.y, wood.position.z), true);
bot.once('goal_reached', async () => {
bot.chat('I found some wood, now I will break it!');
try {
await bot.dig(wood);
collectedWood++;
if (collectedWood < targetWood) {
collectWood();
} else {
bot.chat(`I collected all ${collectedWood} pieces of wood! Bringing them to you.`);
deliverWood();
}
} catch (err) {
bot.chat('I couldn\'t break the wood: ' + err.message);
}
});
} else {
bot.chat('I couldn\'t find any wood nearby!');
}
}
function deliverWood() {
const player = bot.players[username]?.entity;
if (player) {
bot.pathfinder.setGoal(new GoalFollow(player, 2), true);
bot.once('goal_reached', async () => {
const logs = bot.inventory.items().filter(item => item.name.includes('log'));
if (logs.length > 0) {
for (const log of logs) {
await bot.toss(log.type, null, log.count);
}
bot.chat('Here is the wood I collected!');
} else {
bot.chat('I couldn\'t collect any wood somehow...');
}
});
} else {
bot.chat('I can\'t find you to bring the wood!');
}
}
collectWood();
};
// Equip armor logic
const equipArmor = () => {
const armorItems = ['helmet', 'chestplate', 'leggings', 'boots'];
armorItems.forEach(item => {
const armor = bot.inventory.findInventoryItem(item, null);
if (armor) {
bot.equip(armor, item, (err) => {
if (err) bot.chat(`Error equipping ${item}: ${err.message}`);
else bot.chat(`${item.charAt(0).toUpperCase() + item.slice(1)} equipped!`);
});
}
});
};
// Teleport logic
const handleTeleportCommand = (parts, username) => {
const playerName = parts[1] === "me" ? username : parts[1];
const player = bot.players[playerName];
if (player) {
const playerPosition = player.entity.position;
bot.chat(`Teleporting to ${playerName}...`);
bot.teleport(playerPosition);
} else {
bot.chat(`I can't find player ${playerName}.`);
}
};
bot.on('health', () => {
if (!is_a_AFK_Bot) {
if (bot.health <= 4) {
bot.chat('I\'m in danger! I have less than 2 hearts!');
}
if (bot.food < 6 && !isEating) {
bot.chat('I\'m hungry! I need food.');
eatFood();
}
}
});
let isEating = false;
const eatFood = () => {
if (isEating) return;
const food = bot.inventory.items().find(item => ['cooked_beef', 'bread', 'cooked_porkchop'].includes(item.name));
if (food) {
isEating = true;
bot.equip(food, 'hand', (err) => {
if (err) {
bot.chat('I couldn\'t equip the food: ' + err.message);
isEating = false;
return;
}
bot.consume((err) => {
isEating = false;
if (err) bot.chat('I had trouble eating: ' + err.message);
else bot.chat('Yummy! I\'m no longer hungry.');
});
});
} else {
bot.chat('I have no food! Please give me something to eat.');
}
};
bot.on('playerJoined', (player) => {
console.log(`${player.username} has joined the server`);
bot.chat(`Hello ${player.username} !`);
});
bot.on('drop', (entity) => {
if (!is_a_AFK_Bot && entity && entity.item) {
bot.collectBlock.collect(entity);
bot.chat(`Item ${entity.item.name} picked up`);
}
});
bot.on('death', function () {
bot.emit("respawn");
bot.chat('I am dead');
});
bot.on('error', function (err) {
console.error("Bot error: ", err);
});
bot.on('kicked', function (reason) {
console.log("Kicked for: ", reason);
});
bot.on('end', function () {
console.log("Bot has ended");
});