-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindividual.ts
348 lines (333 loc) · 16.5 KB
/
individual.ts
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
import { Bot } from "mineflayer";
import { ChatBuffer } from "./chat";
import { Behaviours } from "./utils";
import { Movements } from "mineflayer-pathfinder";
import { IndexedData } from "minecraft-data";
import { Vec3 } from "vec3";
import { CraftTask } from "./CraftTask/CraftTask";
import { BotTask } from "./CraftTask/BotTask";
export class Individual {
private utils: Behaviours;
private chat: ChatBuffer;
private bot: Bot;
private defaultMove: Movements;
private mcData: IndexedData;
private task: BotTask<any> | null = null;
private taskRunner: NodeJS.Timeout | null = null;
constructor(bot: Bot, chat: ChatBuffer) {
this.utils = new Behaviours();
this.chat = chat;
this.bot = bot;
this.mcData = require('minecraft-data')(this.bot.version);
this.defaultMove = new Movements(this.bot);
}
private movementCallback(returnAddress: string | null, successful: boolean) {
const announcement = successful ? `:)` : `I can't get there`;
this.chat.addChat(this.bot, announcement, returnAddress);
}
private goto(messageParts: string[], returnAddress: string | null): void {
if (messageParts.length > 3) {
let x = parseInt(messageParts[1], 10);
let y = parseInt(messageParts[2], 10);
let z = parseInt(messageParts[3], 10);
this.chat.addChat(this.bot, "Ok, on my way", returnAddress);
this.utils.goToTarget(this.bot, { position: { x, y, z } }, this.defaultMove, 0, (success) => {
this.movementCallback(returnAddress, success);
});
} else {
let player = this.bot.players[messageParts[1]]
let target: { position: Vec3 } = player.entity;
if (player) {
this.chat.addChat(this.bot, "Ok, I'll find 'em", returnAddress);
} else {
if (messageParts[1] == 'home') {
let homePos = this.utils.getHome(this.bot);
if (homePos) {
target = { position: homePos };
} else {
this.chat.addChat(this.bot, "I'm homeless, I've got no home to go to", returnAddress);
return;
}
} else {
this.chat.addChat(this.bot, "No-one is called " + messageParts[1], returnAddress);
return;
}
}
this.utils.goToTarget(this.bot, target, this.defaultMove, 1, (success) => {
this.movementCallback(returnAddress, success);
});
}
};
public handleChat(username: string, message: string, bot: Bot, masters: string[],
isWhisper: boolean = false, createBotCallback: ((names: string[]) => void) | null = null) {
if (username === this.bot.username || !masters.includes(username)) return;
// insert bot name for whispers, if not present, for easier parsing
if (!message.startsWith(this.bot.username)) message = this.bot.username + ' ' + message;
const returnAddress = isWhisper ? username : null; // used for direct response or global chat depending on how we were spoken to
const messageParts = message.split(' ');
let messageFor = messageParts.shift();
if (messageFor != this.bot.username && messageFor != 'swarm') return;
console.log("Command:", username, messageFor, messageParts);
let target = this.bot.players[username].entity;
switch (messageParts[0]) {
case 'come':
this.chat.addChat(this.bot, 'coming', returnAddress);
this.utils.goToTarget(this.bot, target, this.defaultMove, 0, (success: boolean) => {
this.movementCallback(returnAddress, success);
});
break;
case 'follow':
if (messageParts.length > 1) {
let player = this.bot.players[messageParts[1]]
if (player) {
target = player.entity;
} else {
this.chat.addChat(this.bot, "No-one is called " + messageParts[1], returnAddress);
return;
}
}
this.utils.follow(this.bot, target, this.defaultMove);
this.chat.addChat(this.bot, 'ok', returnAddress);
break;
case 'avoid':
if (messageParts.length > 1) {
let player = this.bot.players[messageParts[1]]
if (player) {
target = player.entity;
} else {
this.chat.addChat(this.bot, "No-one is called " + messageParts[1], returnAddress);
return;
}
}
this.utils.avoid(this.bot, target, this.defaultMove);
this.chat.addChat(this.bot, 'ok', returnAddress);
break;
case 'shift':
this.utils.shift(this.bot, this.defaultMove);
break;
case 'stay':
case 'stop':
this.clearActiveTask();
this.utils.stop(bot);
this.chat.addChat(this.bot, 'ok', returnAddress);
break;
case 'info':
this.chat.addChat(this.bot, this.utils.info(this.bot, messageParts), returnAddress);
break;
case 'hole':
this.utils.hole(this.bot, messageParts, this.defaultMove, (msg: string) => {
this.chat.addChat(this.bot, msg, returnAddress);
});
break;
case 'nearby':
this.chat.addChat(this.bot, this.utils.nearbyBlocks(bot).join(', '), returnAddress);
break;
case 'inventory':
this.chat.addChat(this.bot, this.utils.inventoryAsString(this.bot), returnAddress);
break;
case 'equip':
if (messageParts.length == 1) {
this.chat.addChat(this.bot, "equip what?", returnAddress);
return;
}
this.utils.equipByNameDescriptive(this.bot, messageParts[1], this.mcData, (msg: string) => {
this.chat.addChat(this.bot, msg, returnAddress);
});
break;
case 'drop':
case 'gimme':
if (messageParts.length < 3) {
this.chat.addChat(this.bot, "drop how much of what?", returnAddress);
return;
}
this.utils.tossItem(this.bot, messageParts[2], messageParts[1], username, (msg: string) => {
this.chat.addChat(this.bot, msg, returnAddress);
});;
break;
case 'harvest':
if (messageParts.length == 1) {
this.chat.addChat(this.bot, "Harvest how much of what!?", returnAddress);
return;
}
this.utils.harvest(this.bot, messageParts[2], this.defaultMove, parseInt(messageParts[1], 10), this.mcData, (msg: string) => {
this.chat.addChat(this.bot, msg, returnAddress);
});
break;
case 'collect':
this.utils.collectDrops(this.bot, this.defaultMove, 30, () => this.chat.addChat(this.bot, "Everything's collected", returnAddress));
break;
case 'hunt':
this.chat.addChat(this.bot, "I'm off hunting", returnAddress);
this.utils.hunt(this.bot, this.defaultMove, parseInt(messageParts[1], 30), 30, () => {
this.chat.addChat(this.bot, 'finished hunting', returnAddress);
});
break;
case 'protect':
this.chat.addChat(this.bot, "I'm on it", returnAddress);
this.utils.attackNearestMob(this.bot, this.defaultMove, (msg: string) => {
this.chat.addChat(this.bot, msg, returnAddress);
});
break;
case 'goto':
case 'go':
this.goto(messageParts, returnAddress);
break;
case 'move':
let move = (messageParts: string[]) => {
const x = parseInt(messageParts[1], 10);
const y = parseInt(messageParts[2], 10);
const z = parseInt(messageParts[3], 10);
this.utils.goToTarget(this.bot, { position: this.bot.entity.position.add(new Vec3(x, y, z)) }, this.defaultMove, 0, (success) => {
this.movementCallback(returnAddress, success);
});
};
move(messageParts);
break;
case 'craft':
const itemName = messageParts[1];
const friendlyItemName = itemName.split("_").join(" ");
const amount = messageParts.length > 2 ? parseInt(messageParts[2]) : 1;
const craftingTableBlockInfo = this.utils.nameToBlock('crafting_table', this.mcData);
const craftingTablePos = this.bot.findBlocks({
matching: craftingTableBlockInfo.id,
point: this.bot.entity.position
})[0];
this.utils.goToTarget(this.bot, { position: craftingTablePos }, this.defaultMove, 2, (arrivedSuccessfully) => {
if (!arrivedSuccessfully && craftingTablePos != null) return this.chat.addChat(this.bot, `Couldn't get to the crafting table`, returnAddress);
this.utils.craft(this.bot, itemName, this.mcData, amount, bot.blockAt(craftingTablePos), (err) => {
if (err) {
this.chat.addChat(this.bot, `Couldn't make a ${friendlyItemName}`, returnAddress);
console.log(err);
} else {
this.chat.addChat(this.bot, `Made the ${friendlyItemName}${amount > 1 && !friendlyItemName.endsWith("s") ? "s" : ""}`, returnAddress);
}
});
});
break;
case 'sethome':
this.utils.setHome(this.bot, this.bot.entity.position);
this.chat.addChat(this.bot, "Homely!", returnAddress);
break;
case 'where':
this.chat.addChat(this.bot, this.bot.entity.position.toString(), returnAddress);
break;
case 'say':
messageParts.shift();
const msgToSend = messageParts.join(' ');
this.chat.addChat(this.bot, `Ok I'll say "${msgToSend}"`, username);
console.log('repeat', msgToSend);
this.chat.addChat(this.bot, msgToSend, null);
break;
case 'use':
this.bot.activateItem();
break;
case 'disuse':
this.bot.deactivateItem();
break;
case 'empty':
this.utils.emptyNearestChest(this.bot, 7, () => { this.chat.addChat(this.bot, 'Emptied the chest', returnAddress) });
break;
case 'fill':
this.utils.emptyNearestChest(this.bot, 7, () => { this.chat.addChat(this.bot, 'Filled the chest', returnAddress), true });
break;
case 'place':
// place, block, x, y, z
// 0 1 2 3 4
if (messageParts.length !== 5) return this.chat.addChat(this.bot, 'place what block where?!', returnAddress);
const position = new Vec3(Number(messageParts[2]), Number(messageParts[3]), Number(messageParts[4]));
const blockToPlace = messageParts[1];
this.utils.getAdjacentTo(this.bot, { position }, this.defaultMove, () => {
this.utils.placeBlockAt(this.bot, position, blockToPlace, this.mcData, () => {
this.chat.addChat(this.bot, `Placed the ${blockToPlace}`, returnAddress);
});
});
break;
case 'portal':
// portal, (bl)x, y, z, (tr)x, y, z, (opt)blockname
// 0 1 2 3 4 5 6 7
if (messageParts.length < 7) return this.chat.addChat(this.bot, 'portal from where to where?!', returnAddress);
const bottomLeft = new Vec3(Number(messageParts[1]), Number(messageParts[2]), Number(messageParts[3]));
const topRight = new Vec3(Number(messageParts[4]), Number(messageParts[5]), Number(messageParts[6]));
const portalBlockName = messageParts.length === 8 ? messageParts[7] : 'obsidian';
//return console.log(portalBlockName, messageParts, messageParts.length);
const portalBlock = this.utils.nameToBlock(portalBlockName, this.mcData);
this.defaultMove.blocksCantBreak.add(portalBlock.id);
this.utils.goToTarget(this.bot, { position: bottomLeft }, this.defaultMove, 5, () => {
this.utils.buildPortal(this.bot, bottomLeft, topRight, this.mcData, this.defaultMove, portalBlockName, () => {
this.chat.addChat(this.bot, `Built the ${portalBlockName} portal`, returnAddress);
this.defaultMove.blocksCantBreak.delete(portalBlock.id);
});
});
break;
case 'new':
if (!createBotCallback) {
console.warn("Tried to create new bot(s) but no callback provided");
this.chat.addChat(this.bot, `Can't right now`, returnAddress);
break;
}
messageParts.shift();
// Allow for " ", "," and ", " separated names
const names: string[] = [];
messageParts.forEach((curr: string) => {
names.push(...curr.split(",").map(n => n.trim()));
});
createBotCallback(names);
break;
case 'sleep':
this.utils.sleep(this.bot, (msg: string) => {
this.chat.addChat(this.bot, msg, returnAddress);
});
break;
case 'worldspawn':
this.chat.addChat(this.bot, `Heading to spawn`, returnAddress);
this.utils.goToTarget(this.bot, { position: this.bot.spawnPoint }, this.defaultMove, 2, (success) => this.movementCallback(returnAddress, success));
break;
case 'torch':
this.chat.addChat(this.bot, `Torch? ${this.utils.shouldPlaceTorch(bot)}`, returnAddress);
break;
case 'crafttree':
const rootName = messageParts[1];
const totalAmount = messageParts.length > 2 ? parseInt(messageParts[2]) : 1;
const craftTask = new CraftTask(bot, this.utils, rootName, totalAmount, this.mcData, true);
this.chat.addChat(this.bot, `On it`, returnAddress);
this.runTask(craftTask).then(() => {
this.chat.addChat(this.bot, `Managed to craft that ${rootName.replace("_", " ")}`, returnAddress);
}).catch((err) => {
console.error(err);
this.chat.addChat(this.bot, `Can't craft a ${message[0]} at the minute`, returnAddress);
});
break;
default:
this.chat.addChat(this.bot, 'What do you mean?', returnAddress);
return;
}
}
private runTask<T>(task: BotTask<T>): Promise<T | null> {
return new Promise<T | null>((resolve, reject) => {
if (this.taskRunner) {
this.clearActiveTask();
this.utils.stop(this.bot);
}
this.taskRunner = setInterval(() => {
if (task.isComplete()) {
resolve(task.result());
this.clearActiveTask();
}
try {
task.tick();
} catch (err) {
this.clearActiveTask();
reject(err);
}
}, 2000);
// Note: 2000 here seems to work ok. It can be that if we call tick() too often that things start breaking
// eg: block mining is reset too quickly
});
}
private clearActiveTask() {
if (this.taskRunner) {
clearInterval(this.taskRunner);
}
this.task = null;
}
}