-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguard.js
109 lines (93 loc) · 2.56 KB
/
guard.js
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
const mineflayer = require("mineflayer");
const { pathfinder, Movements, goals } = require("mineflayer-pathfinder");
const pvp = require("mineflayer-pvp").plugin;
if (process.argv.length < 4 || process.argv.length > 6) {
console.log("Usage : node guard.js <host> <port> [<name>]");
process.exit(1);
}
const bot = mineflayer.createBot({
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] || "Guard",
});
bot.loadPlugin(pathfinder);
bot.loadPlugin(pvp);
let guardPos = null;
function equipBot() {
const diamondSword = bot.inventory
.items()
.find((i) => i.name.includes("diamond_sword"));
if (diamondSword) {
bot.equip(diamondSword, "hand");
} else {
bot.chat(`/give ${bot.username} diamond_sword`);
}
}
bot.on("spawn", () => {
const player = bot.players["Mysteriza"];
if (player && player.entity) {
bot.chat(`/teleport ${bot.username} Mysteriza`);
setTimeout(() => {
bot.chat("guard");
}, 5000);
}
equipBot();
});
function guardArea(pos) {
guardPos = pos;
if (!bot.pvp.target) {
moveToGuardPos();
}
}
function stopGuarding() {
guardPos = null;
bot.pvp.stop();
bot.pathfinder.setGoal(null);
}
function moveToGuardPos() {
bot.pathfinder.setMovements(new Movements(bot));
bot.pathfinder.setGoal(
new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z)
);
}
function noHostileEntitiesAround() {
const filter = (entity) =>
entity.type === "hostile" &&
entity.position.distanceTo(bot.entity.position) < 15 &&
entity.displayName !== "Armor Stand";
return !bot.nearestEntity(filter);
}
bot.on("physicsTick", () => {
if (!guardPos) return;
const filter = (e) =>
e.type === "hostile" &&
e.position.distanceTo(bot.entity.position) < 15 &&
e.displayName !== "Armor Stand";
const entity = bot.nearestEntity(filter);
if (entity) {
bot.pvp.attack(entity);
} else if (noHostileEntitiesAround()) {
const player = bot.players["Mysteriza"];
if (player && player.entity) {
bot.pathfinder.setMovements(new Movements(bot));
bot.pathfinder.setGoal(new goals.GoalFollow(player.entity, 3), true);
}
}
});
bot.on("chat", (username, message) => {
if (message === "guard") {
const player = bot.players[username];
if (!player) {
bot.chat("I can't see you.");
return;
}
bot.chat("I will protect you, baby!");
guardArea(player.entity.position);
} else if (message === "stop") {
bot.chat("OK then... See you!");
stopGuarding();
}
});
bot.on("error", (err) => {
console.error(`Bot error: ${err}`);
});