forked from eternum/minecraftBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
127 lines (109 loc) · 2.56 KB
/
bot.js
File metadata and controls
127 lines (109 loc) · 2.56 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
const mineflayer = require("mineflayer");
const ipc = require("node-ipc");
const v = require("vec3");
const dataManager = require("./modules/dataManager");
var config = dataManager.loadConfig();
const botId = process.argv[2];
const server = process.argv[3];
const port = process.argv[4];
const username = process.argv[5] ? process.argv[5] : "bot_" + botId;
const password = process.argv[6] ? process.argv[6] : null;
var socketPath = config.ipc.socketPath;
ipc.config.id = "parent";
ipc.config.appspace = "";
ipc.config.silent = true;
ipc.connectTo("parent", socketPath, function () {
parent = ipc.of.parent;
ipc.log("## connected to parent ##".rainbow, ipc.config.delay);
parent.on("data", function (data) {
switch (data.action) {
case "stop":
stop();
break;
case "status":
sendStatus();
break;
case "move":
move(data);
break;
}
});
parent.on("error", function () {
stop();
});
parent.on("disconnect", function () {
stop();
});
parent.on("destroy", function () {
stop();
});
});
const bot = mineflayer.createBot({
host: server,
port: port,
username: username,
password: password,
});
let mcData;
bot.once("inject_allowed", () => {
mcData = require("minecraft-data")(bot.version);
});
function intitiateConnection(botId) {
var message = { action: "started", botId: botId };
send("started", message);
}
function sendCoordinates() {
if (bot != null) {
var position = bot.entity.position;
var json = {
action: "coords",
data: { x: position.x, y: position.y, z: position.z },
botId: botId,
};
send("data", json);
}
}
function sendHealth() {
if (bot != null) {
var health = bot.health;
var json = { action: "health", data: health, botId: botId };
send("data", json);
}
}
function sendHunger() {
if (bot != null) {
var hunger = bot.food;
var json = { action: "hunger", data: hunger, botId: botId };
send("data", json);
}
}
function sendStatus() {
sendCoordinates();
healthHandler();
}
function send(type, data) {
parent.emit(type, data);
}
bot.on("health", healthHandler);
bot.on("login", () => {
intitiateConnection(botId);
setTimeout(() => {
sendStatus();
bot.chat("/a");
}, 1000);
});
bot.on("move", () => {
sendCoordinates();
});
function healthHandler() {
sendHealth();
sendHunger();
}
function move(data) {
if (data == null) bot.clearControlStates();
bot.setControlState(data.data.operation, data.data.state);
}
function stop() {
bot.quit();
bot.on("end", () => process.exit());
}