-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
135 lines (118 loc) · 4.6 KB
/
index.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
import { IndexedData } from "minecraft-data";
import { Bot } from "mineflayer";
import { Behaviours } from "./utils";
import { ChatBuffer } from "./chat";
import { Individual } from "./individual";
import { Swarm, SwarmConfig } from "./swarm";
const mineflayer = require('mineflayer');
const { Movements } = require('mineflayer-pathfinder');
const utils = new Behaviours();
const chat = new ChatBuffer();
const botNames: string[] = [
'QuailBotherer'
];
const host: string = process.argv[2];
const port: number = parseInt(process.argv[3], 10);
const password: string = process.argv[4];
const masters: string[] = [process.argv[5]];
console.log(`Starting: Bots: ${botNames}, ${host}:${port}. Controllers: ${masters}. Pass set = ${!!password}`);
function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const autoLogin = async (bot: Bot) => {
await sleep(3000);
console.log("Auto logging in...");
chat.addChat(bot, `/register ${password} ${password}`);
chat.addChat(bot, `/login ${password}`);
};
const newBots = (names: string[]) => {
const newBots = Swarm.Create(names, botConfig);
console.log(`Created ${names} (${newBots.map((nb: Bot) => nb.username)}). Waiting for spawns..`);
let counter = 0;
newBots.forEach((bot: Bot) => {
bot.once('spawn', () => {
counter++;
console.log("Spawned", bot.username, `(${counter}/${newBots.length})`);
if(counter === newBots.length) {
newBots.forEach((b: Bot) => swarm.push(b));
console.log("All bots now:", swarm.map((b: Bot) => b.username));
}
});
});
}
const botInit = (bot: Bot) => {
console.log(`[${bot.username}] Loading plugins...`);
bot.loadPlugins([require('mineflayer-pathfinder').pathfinder, require('mineflayer-armor-manager'), require('mineflayer-blockfinder')(mineflayer)]);
console.log(bot.username, 'initalised');
// Once we've spawn, it is safe to access mcData because we know the version
const mcData: IndexedData = require('minecraft-data')(bot.version);
prepFriendlyProtection(mcData, swarm);
const defaultMove = new Movements(bot, mcData);
defaultMove.allowFreeMotion = true
const individual: Individual = new Individual(bot, chat);
bot.on('chat', (username: string, message: string) => {
console.log(`[Msg>${bot.username}]`, username, message);
try {
individual.handleChat(username, message, bot, masters, false, newBots)
} catch (err) {
console.log(err);
chat.addChat(bot, `Can't, sorry`, username);
}
});
bot.on('whisper', (username: string, message: string) => {
console.log(`[Whisper>${bot.username}]`, username, message);
try {
individual.handleChat(username, message, bot, masters, true, newBots)
} catch (err) {
console.log(err);
chat.addChat(bot, `Can't, sorry`, username);
}
});
bot.on("end", (reason: string) => {
console.warn(`${bot.player.username} disconnected! (${reason})`);
});
const startTime: number = Date.now();
bot.on('health', () => {
if (Date.now() - startTime < 500) return;
utils.attackNearestMob(bot, defaultMove, () => {});
});
bot.on('kicked', (reason: string) => console.log("kicked", reason));
bot.on('error', console.error);
autoLogin(bot);
masters.forEach(master => {
chat.addChat(bot, `I'm online`, master);
});
};
let haveSetupProtection = false;
const prepFriendlyProtection = (mcData: IndexedData, swarm: Bot[]) => {
if (haveSetupProtection) return;
swarm[swarm.length - 1].once('spawn', () => {
swarm.forEach(bot => {
const defaultMove = new Movements(bot, mcData);
defaultMove.allowFreeMotion = true;
swarm.forEach(other => {
if (other.username != bot.username) {
other.on('health', () => utils.protectFriendly(bot, other, defaultMove));
}
});
masters.forEach(m => {
let player = bot.players[m];
if (!player) {
console.warn("No player found for auto protect");
} else {
while (!player.entity) { }
player.entity.on('health', () => utils.protectFriendly(bot, player, defaultMove));
}
});
});
});
haveSetupProtection = true;
}
const botConfig: SwarmConfig = {
host,
port,
version: '1.20.1',
initCallback: botInit
};
chat.start();
const swarm = Swarm.Create(botNames, botConfig);