-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIPC.js
More file actions
71 lines (56 loc) · 2.07 KB
/
Copy pathIPC.js
File metadata and controls
71 lines (56 loc) · 2.07 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
const WebSocket = require('ws');
const { NearGoal } = require('mineflayer-pathfinder').goals;
const { Movements } = require('mineflayer-pathfinder');
function IPC(endpoint, bot) {
this.endpoint = endpoint
this.ws = new WebSocket('ws://' + this.endpoint);
this.bot = bot
this.username = bot.username
this.following = null;
this.ping = async function() {
this.ws.send(JSON.stringify({ "username": this.username }))
}
this.bot.on('entityMoved', (entity) => {
if (entity === this.following) {
const mcData = require('minecraft-data')(this.bot.version);
this.bot.pathfinder.setMovements(new Movements(this.bot, mcData));
this.bot.pathfinder.setGoal(new GoalNear(entity.x, entity.y, entity.z, 1));
}
});
this.followEntity = function(entity) {
this.following = entity
};
this.unfollowEntity= function() {
this.following = null
};
this.handleResponse = function(response) {
if (response.action === 'attack') {
const entity = this.bot.entities[response.extra_data.entity];
if (entity){
if (entity === this.bot.entity) return;
this.bot.pvp.attack(entity);
}
} else if (response.action === 'stopattack') {
this.bot.pvp.stop();
} else if (response.action === 'chat') {
this.bot.chat(response.extra_data.content);
} else if (response.action === 'follow') {
if (response.extra_data.entity.type === 'player') {
this.followingEntity(this.bot.players[username].entity)
}
} else if (response.action === 'unfollow') {
thius.unfollowEntity()
}
console.log(response)
}
this.communicate = async function(payload){
const response = await axios ({
url: this.endpoint,
headers: { "username": this.username },
data: payload,
method: "POST"
})
return this.handleResponse(response.data)
}
}
module.exports = IPC;