-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbodyguards.js
More file actions
161 lines (141 loc) · 4.68 KB
/
bodyguards.js
File metadata and controls
161 lines (141 loc) · 4.68 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
const { parentPort, workerData } = require('worker_threads')
const mineflayer = require('mineflayer')
const toolPlugin = require('mineflayer-tool').plugin
const armorManager = require('mineflayer-armor-manager')
const autoeat = require('mineflayer-auto-eat')
const pvp = require('mineflayer-pvp').plugin
const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
const bossName = process.argv[4] || workerData.bossName;
const prefix = process.argv[5] || workerData.prefix;
const server = {
address: process.argv[2] || workerData.address,
port: process.argv[3] || workerData.port,
};
const personalSpace = 5;
const botCount = process.argv[6] || workerData.botCount;
var offset = 0;
var bots = [];
var target;
var defaultMove;
function createBot() {
//Log in as a new bot to the server
let bot = mineflayer.createBot({
host: server.address,
port: server.port,
username: `${prefix}#${bots.length}`,
viewDistance: "tiny",
});
bot.id = bots.length;
bot.direction = Math.PI * 2 / botCount * bots.length;
// Loading plugins
bot.loadPlugin(pvp)
bot.loadPlugin(pathfinder)
bot.loadPlugin(armorManager)
bot.loadPlugin(toolPlugin)
//Log errors and kick messages
bot.on('kicked', (reason, loggedIn) => console.log(reason, loggedIn));
bot.on('error', err => console.log(err));
bot.once("spawn", () => {
bot.chat('I am the bodyguard of ' + bossName)
})
bot.on('playerCollect', (collector, itemDrop) => {
if (collector !== bot.entity) return
setTimeout(() => {
const sword = bot.inventory.items().find(item => item.name.includes('sword'))
if (sword) bot.equip(sword, 'hand')
}, 150)
})
bot.on('playerCollect', (collector, itemDrop) => {
if (collector !== bot.entity) return
setTimeout(() => {
const shield = bot.inventory.items().find(item => item.name.includes('shield'))
if (shield) bot.equip(shield, 'off-hand')
}, 250)
})
bot.on('spawn', () => {
defaultMove = new Movements(bot);
})
//Do this every time the bot moves
bot.on('move', () => {
let boss = bot.players[bossName];
//Abort if the boss is not on the server
if (!boss) return;
boss = boss.entity;
//Abort if the boss is not close
if (!boss) return;
offset = boss.yaw;
//Location is where the bot is supposed to be headed
let location;
if (target) {
//If there is a target (enemy) to attack, make them the target location
location = target.position;
} else {
//If there is no enemy (no combat), return to or keep staying with boss
let x = Math.sin(bot.direction + offset) * personalSpace;
let z = Math.cos(bot.direction + offset) * personalSpace;
//Set the headed location to your position next to boss
location = boss.position.offset(x, 0, z);
}
//Face the location it is heading
bot.lookAt(location);
//If in combat, hit the enemy
if (target) bot.attack(target);
//If it is not yet the amount of blocks "personalSpace" away from the location, walk
if (bot.entity.position.xzDistanceTo(location) > personalSpace) {
bot.pathfinder.setMovements(defaultMove);
bot.pathfinder.setGoal(new goals.GoalBlock(location.x, location.y, location.z));
// Face the location it is heading
bot.lookAt(location);
}
});
return (bot);
}
function mainChat(username, message) {
//Ignore messages that are not from the boss
if (username != bossName) return;
//Boss's command parts as an array, e.g. ["kill, "jeb_"]
let tokens = message.split(' ');
switch (tokens[0]) {
case 'kill':
bots[0].chat("Received.");
//Set the target to the nearest entity that has the username by what the boss said
target = bots[0].nearestEntity((entity) => {
return (entity.displayName == tokens[1] || entity.username == tokens[1]);
});
console.log(target);
break;
case 'halt':
bots.chat("Received.")
target = null;
case 'cease':
process.exit(0);
}
}
function populate() {
//Spawn 1 Guard
bots.push(createBot());
if (bots.length < botCount) {
setTimeout(populate, process.argv[7] * 1000);
} else {
//Do this when all bots are spawned
console.log("Ready!");
bots[0].on('chat', mainChat);
//When an entity disappears (quits, dies etc.), and it's the target, remove the target
bots[0].on('entityGone', (entity) => {
if (entity != target) return;
target = 0;
});
//When an entity gets harmed
bots[0].on('entityHurt', (entity) => {
//Ignore if the harmed entity is not the boss
if (entity.username != bossName) return;
//Select the entity that hurt the boss, by selecting the nearest entity that is not the boss or another Guard
//This needs work :/
target = bots[0].nearestEntity((entity) => {
return (entity.username != bossName && !(entity.username || '').startsWith(prefix));
});
});
}
}
//Begin the Guard populating!
populate();