-
-
Notifications
You must be signed in to change notification settings - Fork 921
/
chest.js
401 lines (370 loc) · 10.8 KB
/
chest.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Watch out, this is a big one!
*
* This is a demonstration to show you how you can interact with:
* - Chests
* - Furnaces
* - Dispensers
* - Enchantment Tables
*
* and of course with your own inventory.
*
* Each of the main commands makes the bot interact with the block and open
* its window. From there you can send another set of commands to actually
* interact with the window and make awesome stuff.
*
* There's also a bonus example which shows you how to use the /invsee command
* to see what items another user has in his inventory and what items he has
* equipped.
* This last one is usually reserved to Server Ops so make sure you have the
* appropriate permission to do it or it won't work.
*/
const mineflayer = require('mineflayer')
if (process.argv.length < 4 || process.argv.length > 6) {
console.log('Usage : node chest.js <host> <port> [<name>] [<password>]')
process.exit(1)
}
const bot = mineflayer.createBot({
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : 'chest',
password: process.argv[5]
})
bot.on('experience', () => {
bot.chat(`I am level ${bot.experience.level}`)
})
bot.on('chat', (username, message) => {
if (username === bot.username) return
switch (true) {
case /^list$/.test(message):
sayItems()
break
case /^chest$/.test(message):
watchChest(false, ['chest', 'ender_chest', 'trapped_chest'])
break
case /^furnace$/.test(message):
watchFurnace()
break
case /^dispenser$/.test(message):
watchChest(false, ['dispenser'])
break
case /^enchant$/.test(message):
watchEnchantmentTable()
break
case /^chestminecart$/.test(message):
watchChest(true)
break
case /^invsee \w+( \d)?$/.test(message): {
// invsee Herobrine [or]
// invsee Herobrine 1
const command = message.split(' ')
useInvsee(command[0], command[1])
break
}
}
})
function sayItems (items = bot.inventory.items()) {
const output = items.map(itemToString).join(', ')
if (output) {
bot.chat(output)
} else {
bot.chat('empty')
}
}
async function watchChest (minecart, blocks = []) {
let chestToOpen
if (minecart) {
chestToOpen = Object.keys(bot.entities)
.map(id => bot.entities[id]).find(e => e.entityType === bot.registry.entitiesByName.chest_minecart &&
e.objectData.intField === 1 &&
bot.entity.position.distanceTo(e.position) < 3)
if (!chestToOpen) {
bot.chat('no chest minecart found')
return
}
} else {
chestToOpen = bot.findBlock({
matching: blocks.map(name => bot.registry.blocksByName[name].id),
maxDistance: 6
})
if (!chestToOpen) {
bot.chat('no chest found')
return
}
}
const chest = await bot.openContainer(chestToOpen)
sayItems(chest.containerItems())
chest.on('updateSlot', (slot, oldItem, newItem) => {
bot.chat(`chest update: ${itemToString(oldItem)} -> ${itemToString(newItem)} (slot: ${slot})`)
})
chest.on('close', () => {
bot.chat('chest closed')
})
bot.on('chat', onChat)
function onChat (username, message) {
if (username === bot.username) return
const command = message.split(' ')
switch (true) {
case /^close$/.test(message):
closeChest()
break
case /^withdraw \d+ \w+$/.test(message):
// withdraw amount name
// ex: withdraw 16 stick
withdrawItem(command[2], command[1])
break
case /^deposit \d+ \w+$/.test(message):
// deposit amount name
// ex: deposit 16 stick
depositItem(command[2], command[1])
break
}
}
function closeChest () {
chest.close()
bot.removeListener('chat', onChat)
}
async function withdrawItem (name, amount) {
const item = itemByName(chest.containerItems(), name)
if (item) {
try {
await chest.withdraw(item.type, null, amount)
bot.chat(`withdrew ${amount} ${item.name}`)
} catch (err) {
bot.chat(`unable to withdraw ${amount} ${item.name}`)
}
} else {
bot.chat(`unknown item ${name}`)
}
}
async function depositItem (name, amount) {
const item = itemByName(chest.items(), name)
if (item) {
try {
await chest.deposit(item.type, null, amount)
bot.chat(`deposited ${amount} ${item.name}`)
} catch (err) {
bot.chat(`unable to deposit ${amount} ${item.name}`)
}
} else {
bot.chat(`unknown item ${name}`)
}
}
}
async function watchFurnace () {
const furnaceBlock = bot.findBlock({
matching: ['furnace', 'lit_furnace'].filter(name => bot.registry.blocksByName[name] !== undefined).map(name => bot.registry.blocksByName[name].id),
maxDistance: 6
})
if (!furnaceBlock) {
bot.chat('no furnace found')
return
}
const furnace = await bot.openFurnace(furnaceBlock)
let output = ''
output += `input: ${itemToString(furnace.inputItem())}, `
output += `fuel: ${itemToString(furnace.fuelItem())}, `
output += `output: ${itemToString(furnace.outputItem())}`
bot.chat(output)
furnace.on('updateSlot', (slot, oldItem, newItem) => {
bot.chat(`furnace update: ${itemToString(oldItem)} -> ${itemToString(newItem)} (slot: ${slot})`)
})
furnace.on('close', () => {
bot.chat('furnace closed')
})
furnace.on('update', () => {
console.log(`fuel: ${Math.round(furnace.fuel * 100)}% progress: ${Math.round(furnace.progress * 100)}%`)
})
bot.on('chat', onChat)
function onChat (username, message) {
if (username === bot.username) return
const command = message.split(' ')
switch (true) {
case /^close$/.test(message):
closeFurnace()
break
case /^(input|fuel) \d+ \w+$/.test(message):
// input amount name
// ex: input 32 coal
putInFurnace(command[0], command[2], command[1])
break
case /^take (input|fuel|output)$/.test(message):
// take what
// ex: take output
takeFromFurnace(command[0])
break
}
function closeFurnace () {
furnace.close()
bot.removeListener('chat', onChat)
}
async function putInFurnace (where, name, amount) {
const item = itemByName(furnace.items(), name)
if (item) {
const fn = {
input: furnace.putInput,
fuel: furnace.putFuel
}[where]
try {
await fn.call(furnace, item.type, null, amount)
bot.chat(`put ${amount} ${item.name}`)
} catch (err) {
bot.chat(`unable to put ${amount} ${item.name}`)
}
} else {
bot.chat(`unknown item ${name}`)
}
}
async function takeFromFurnace (what) {
const fn = {
input: furnace.takeInput,
fuel: furnace.takeFuel,
output: furnace.takeOutput
}[what]
try {
const item = await fn.call(furnace)
bot.chat(`took ${item.name}`)
} catch (err) {
bot.chat('unable to take')
}
}
}
}
async function watchEnchantmentTable () {
const enchantTableBlock = bot.findBlock({
matching: ['enchanting_table'].map(name => bot.registry.blocksByName[name].id),
maxDistance: 6
})
if (!enchantTableBlock) {
bot.chat('no enchantment table found')
return
}
const table = await bot.openEnchantmentTable(enchantTableBlock)
bot.chat(itemToString(table.targetItem()))
table.on('updateSlot', (slot, oldItem, newItem) => {
bot.chat(`enchantment table update: ${itemToString(oldItem)} -> ${itemToString(newItem)} (slot: ${slot})`)
})
table.on('close', () => {
bot.chat('enchantment table closed')
})
table.on('ready', () => {
bot.chat(`ready to enchant. choices are ${table.enchantments.map(o => o.level).join(', ')}`)
})
bot.on('chat', onChat)
function onChat (username, message) {
if (username === bot.username) return
const command = message.split(' ')
switch (true) {
case /^close$/.test(message):
closeEnchantmentTable()
break
case /^put \w+$/.test(message):
// put name
// ex: put diamondsword
putItem(command[1])
break
case /^add lapis$/.test(message):
addLapis()
break
case /^enchant \d+$/.test(message):
// enchant choice
// ex: enchant 2
enchantItem(command[1])
break
case /^take$/.test(message):
takeEnchantedItem()
break
}
function closeEnchantmentTable () {
table.close()
}
async function putItem (name) {
const item = itemByName(table.window.items(), name)
if (item) {
try {
await table.putTargetItem(item)
bot.chat(`I put ${itemToString(item)}`)
} catch (err) {
bot.chat(`error putting ${itemToString(item)}`)
}
} else {
bot.chat(`unknown item ${name}`)
}
}
async function addLapis () {
const item = itemByType(table.window.items(), ['dye', 'purple_dye', 'lapis_lazuli'].filter(name => bot.registry.itemByName[name] !== undefined)
.map(name => bot.registry.itemByName[name].id))
if (item) {
try {
await table.putLapis(item)
bot.chat(`I put ${itemToString(item)}`)
} catch (err) {
bot.chat(`error putting ${itemToString(item)}`)
}
} else {
bot.chat("I don't have any lapis")
}
}
async function enchantItem (choice) {
choice = parseInt(choice, 10)
try {
const item = await table.enchant(choice)
bot.chat(`enchanted ${itemToString(item)}`)
} catch (err) {
bot.chat('error enchanting')
}
}
async function takeEnchantedItem () {
try {
const item = await table.takeTargetItem()
bot.chat(`got ${itemToString(item)}`)
} catch (err) {
bot.chat('error getting item')
}
}
}
}
function useInvsee (username, showEquipment) {
bot.once('windowOpen', (window) => {
const count = window.containerItems().length
const what = showEquipment ? 'equipment' : 'inventory items'
if (count) {
bot.chat(`${username}'s ${what}:`)
sayItems(window.containerItems())
} else {
bot.chat(`${username} has no ${what}`)
}
})
if (showEquipment) {
// any extra parameter triggers the easter egg
// and shows the other player's equipment
bot.chat(`/invsee ${username} 1`)
} else {
bot.chat(`/invsee ${username}`)
}
}
function itemToString (item) {
if (item) {
return `${item.name} x ${item.count}`
} else {
return '(nothing)'
}
}
function itemByType (items, type) {
let item
let i
for (i = 0; i < items.length; ++i) {
item = items[i]
if (item && item.type === type) return item
}
return null
}
function itemByName (items, name) {
let item
let i
for (i = 0; i < items.length; ++i) {
item = items[i]
if (item && item.name === name) return item
}
return null
}