-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswarmManager.ts
65 lines (54 loc) · 1.56 KB
/
swarmManager.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
import { createBot, Bot, Plugin } from "mineflayer"
import EventEmitter from "events";
export class BotProfile {
username: string;
password: string;
constructor(username: string, password: string) {
this.username = username;
this.password = password;
}
}
export class ServerProfile {
name: string;
host: string;
port: number;
constructor(name: string, host: string, port: number) {
this.name = name;
this.host = host;
this.port = port;
}
}
export const Bots: Bot[] = []
export const emitter = new EventEmitter();
declare module "mineflayer" {
interface Bot {
profile: BotProfile;
}
}
export function AddBot(profile: BotProfile, server: ServerProfile, plugins: Plugin[] = []): Promise<Bot> {
return new Promise((resolve) => {
const options = {
username: profile.username,
password: profile.password,
host: server.host,
port: server.port,
skipValidation: true
}
const bot = createBot(options)
bot.profile = profile;
bot.loadPlugins(plugins);
bot.once("spawn", async () => {
Bots.push(bot);
emitter.emit("swarm_bot_added", bot);
bot.waitForChunksToLoad().finally(() => resolve(bot))
})
bot.once("end", (reason) => {
RemoveBot(profile)
})
});
}
export function RemoveBot(profile: BotProfile) {
const index = Bots.findIndex(e => e.profile == profile)
if (index != -1)
Bots.splice(index, 1);
}