-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (92 loc) · 3.39 KB
/
index.js
File metadata and controls
105 lines (92 loc) · 3.39 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
const mineflayer = require('mineflayer');
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
const { exec } = require('child_process');
const { GoalNear, GoalBlock, GoalXZ, GoalY, GoalInvert, GoalFollow } = goals;
const fs = require('fs');
const { createSwarm, maxPort } = require('./swarm');
const chat = require('./chat');
const { addChat, start } = require('./chat');
const { handleChat: jobSelector } = require('./individual');
const { protectFriendly } = require('./utils');
let botNames = [] //Sin Cos Tan Csc Sec Cot hSin hCos hTan hCsc hSec hCot
for (i = 5; i < process.argv.length; i++) {
botNames.push(process.argv[i]);
}
server = process.argv[2].split(':')
const host = server[0];
const port = parseInt(server[1]);
let password = process.argv[3];
const master = process.argv[4];
const viewDistance = "normal";
// Function to stop the server on a given port
function stopServerOnPort(port) {
console.log(`Stopping server on port ${port}...`);
exec(`lsof -i :${port} | grep LISTEN | awk '{print $2}' | xargs kill -9`);
}
// Register a cleanup handler to stop the servers
process.on('beforeExit', () => {
for (let port = 9000; port <= maxPort; port++) {
stopServerOnPort(port);
}
});
const autoLogin = (bot) => {
addChat(bot, `/register ${password} ${password}`);
addChat(bot, `/login ${password}`);
password = null;
};
const botInit = (bot) => {
bot.loadPlugins([require('mineflayer-pathfinder').pathfinder, require('mineflayer-armor-manager'), require('mineflayer-blockfinder')(mineflayer)]);
console.log(bot.username, 'initialized');
// Once we've spawn, it is safe to access mcData because we know the version
const mcData = require('minecraft-data')(bot.version);
// prepFriendlyProtection(mcData);
const defaultMove = new Movements(bot, mcData);
defaultMove.allowFreeMotion = true
bot.on('chat', (username, message) => {
jobSelector(username, message, bot, master, chat)
});
bot.on('whisper', (username, message) => {
jobSelector(username, message, bot, master, chat, true)
});
const startTime = Date.now();
bot.on('kicked', (reason) => console.log("kicked", reason));
autoLogin(bot);
addChat(bot, `I'm online`, master);
};;
let haveSetupProtection = true;
const prepFriendlyProtection = (mcData) => {
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', () => protectFriendly(bot, other, defaultMove));
}
});
master.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', () => protectFriendly(bot, player, defaultMove));
}
});
});
});
haveSetupProtection = true;
}
const config = {
host,
port,
viewDistance,
initCallback: botInit
};
function stop() {
process.exit(0);
}
start();
const swarm = createSwarm(botNames, master, config, mineflayer);
module.exports = { stop };