forked from MineDojo/Voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run2.js
233 lines (192 loc) · 6.19 KB
/
run2.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const fs = require("fs");
const express = require("express");
const bodyParser = require("body-parser");
const mineflayer = require("mineflayer");
const skills = require("./lib/skillLoader");
const { initCounter, getNextTime } = require("./lib/utils");
const obs = require("./lib/observation/base");
const OnChat = require("./lib/observation/onChat");
const OnError = require("./lib/observation/onError");
const { Voxels, BlockRecords } = require("./lib/observation/voxels");
const Status = require("./lib/observation/status");
const Inventory = require("./lib/observation/inventory");
const OnSave = require("./lib/observation/onSave");
const Chests = require("./lib/observation/chests");
const { plugin: tool } = require("mineflayer-tool");
let bot1 = null;
let bot2 = null;
function clearBotInventory(bot){
// Iterate through the bot's inventory slots
bot.inventory.slots.forEach((slot, index) => {
if (slot) {
bot.tossStack(slot, (err) => {
if (err) {
console.error(`Error tossing item from slot ${index}:`, err);
}
});
}
});
}
function moveHeldItemToInventory() {
const heldItem1 = bot1.heldItem;
if (!heldItem1) {
bot1.chat("Bot isn't holding any item.");
return;
}
// Find first empty slot in the main inventory (excluding hotbar)
let firstEmptySlotIndex = bot1.inventory.slots.slice(9, 36).findIndex(slot => !slot);
if (firstEmptySlotIndex === -1) {
bot1.chat("No empty slot found in the main inventory.");
return;
}
// Move the held item to the first empty slot in the main inventory
bot1.inventory.move(bot1.quickBarSlot, firstEmptySlotIndex + 9);
const heldItem2 = bot2.heldItem;
if (!heldItem2) {
bot2.chat("Bot isn't holding any item.");
return;
}
// Find first empty slot in the main inventory (excluding hotbar)
firstEmptySlotIndex = bot2.inventory.slots.slice(9, 36).findIndex(slot => !slot);
if (firstEmptySlotIndex === -1) {
bot2.chat("No empty slot found in the main inventory.");
return;
}
// Move the held item to the first empty slot in the main inventory
bot2.inventory.move(bot2.quickBarSlot, firstEmptySlotIndex + 9);
}
if (bot1) onDisconnect("Restarting bot");
bot1 = mineflayer.createBot({
host: "localhost", // minecraft server ip
port: req.body.port, // minecraft server port
username: "bot" + PORT, // minecraft username
disableChatSigning: true,
checkTimeoutInterval: 60 * 60 * 1000,
});
bot2 = mineflayer.createBot({
host: "localhost", // minecraft server ip
port: req.body.port, // minecraft server port
username: "bot" + PORT+2, // minecraft username
disableChatSigning: true,
checkTimeoutInterval: 60 * 60 * 1000,
});
bot1.once("error", onConnectionFailed);
bot2.once("error", onConnectionFailed);
// Event subscriptions
bot1.waitTicks = req.body.waitTicks;
bot1.globalTickCounter = 0;
bot1.stuckTickCounter = 0;
bot1.stuckPosList = [];
bot1.iron_pickaxe = false;
bot1.on("kicked", onDisconnect);
bot2.waitTicks = req.body.waitTicks;
bot2.globalTickCounter = 0;
bot2.stuckTickCounter = 0;
bot2.stuckPosList = [];
bot2.iron_pickaxe = false;
bot2.on("kicked", onDisconnect);
// mounting will cause physicsTick to stop
bot1.on("mount", () => {
bot1.dismount();
});
bot2.on("mount", () => {
bot2.dismount();
});
let promises = [];
bot1.once("spawn", async () => {
promises.push(new Promise(async (resolve) => {
bot1.removeListener("error", onConnectionFailed);
let itemTicks = 1;
bot1.chat("/clear @s");
bot1.chat("/kill @s");
const { pathfinder } = require("mineflayer-pathfinder");
const tool = require("mineflayer-tool").plugin;
const collectBlock = require("mineflayer-collectblock").plugin;
const pvp = require("mineflayer-pvp").plugin;
const minecraftHawkEye = require("minecrafthawkeye");
bot1.loadPlugin(pathfinder);
bot1.loadPlugin(tool);
bot1.loadPlugin(collectBlock);
bot1.loadPlugin(pvp);
bot1.loadPlugin(minecraftHawkEye);
obs.inject(bot1, [
OnChat,
OnError,
Voxels,
Status,
Inventory,
OnSave,
Chests,
BlockRecords,
]);
skills.inject(bot1);
if (req.body.spread) {
bot1.chat(`/spreadplayers ~ ~ 0 300 under 80 false @s`);
await bot1.waitForTicks(bot1.waitTicks);
}
await bot1.waitForTicks(bot1.waitTicks * itemTicks);
initCounter(bot1);
// return_data = str([bot1.observe(), bot2.observe()])
moveHeldItemToInventory(bot1);
clearBotInventory(bot1);
await bot1.waitForTicks(bot1.waitTicks * itemTicks);
resolve();
}));
});
bot2.once("spawn", async () => {
promises.push(new Promise(async (resolve) => {
bot2.chat("/clear @s");
bot2.chat("/kill @s");
bot2.loadPlugin(pathfinder);
bot2.loadPlugin(tool);
bot2.loadPlugin(collectBlock);
bot2.loadPlugin(pvp);
bot2.loadPlugin(minecraftHawkEye);
obs.inject(bot2, [
OnChat,
OnError,
Voxels,
Status,
Inventory,
OnSave,
Chests,
BlockRecords,
]);
skills.inject(bot2);
if (req.body.spread) {
bot2.chat(`/spreadplayers ~ ~ 0 300 under 80 false @s`);
await bot2.waitForTicks(bot2.waitTicks);
}
initCounter(bot2);
moveHeldItemToInventory(bot2);
clearBotInventory(bot2);
await bot2.waitForTicks(bot2.waitTicks * itemTicks);
resolve();
}));
});
const thirdCallback = () => {
res.json({bot1: bot1.observe(), bot2: bot2.observe()});
}
Promise.all(promises).then(() => {
res.json({bot1: bot1.observe(), bot2: bot2.observe()});
});
function onConnectionFailed(e) {
console.log(e);
bot1 = null;
bot2 = null;
res.status(400).json({ error: e });
}
function onDisconnect(message) {
if (bot1.viewer) {
bot1.viewer.close();
}
bot1.end();
console.log(message);
bot1 = null;
if (bot2.viewer) {
bot2.viewer.close();
}
bot2.end();
console.log(message);
bot2 = null;
}