CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PrismarineJS

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: PrismarineJS/mineflayer
Path: blob/master/examples/inventory.js
Views: 788
1
/*
2
* Using the inventory is one of the first things you learn in Minecraft,
3
* now it's time to teach your bot the same skill.
4
*
5
* Command your bot with chat messages and make him toss, equip, use items
6
* and even craft new items using the built-in recipe book.
7
*
8
* To learn more about the recipe system and how crafting works
9
* remember to read the API documentation!
10
*/
11
const mineflayer = require('mineflayer')
12
13
if (process.argv.length < 4 || process.argv.length > 6) {
14
console.log('Usage : node inventory.js <host> <port> [<name>] [<password>]')
15
process.exit(1)
16
}
17
18
const bot = mineflayer.createBot({
19
host: process.argv[2],
20
port: parseInt(process.argv[3]),
21
username: process.argv[4] ? process.argv[4] : 'inventory',
22
password: process.argv[5]
23
})
24
25
bot.on('chat', async (username, message) => {
26
if (username === bot.username) return
27
const command = message.split(' ')
28
switch (true) {
29
case message === 'loaded':
30
await bot.waitForChunksToLoad()
31
bot.chat('Ready!')
32
break
33
case /^list$/.test(message):
34
sayItems()
35
break
36
case /^toss \d+ \w+$/.test(message):
37
// toss amount name
38
// ex: toss 64 diamond
39
tossItem(command[2], command[1])
40
break
41
case /^toss \w+$/.test(message):
42
// toss name
43
// ex: toss diamond
44
tossItem(command[1])
45
break
46
case /^equip [\w-]+ \w+$/.test(message):
47
// equip destination name
48
// ex: equip hand diamond
49
equipItem(command[2], command[1])
50
break
51
case /^unequip \w+$/.test(message):
52
// unequip testination
53
// ex: unequip hand
54
unequipItem(command[1])
55
break
56
case /^use$/.test(message):
57
useEquippedItem()
58
break
59
case /^craft \d+ \w+$/.test(message):
60
// craft amount item
61
// ex: craft 64 stick
62
craftItem(command[2], command[1])
63
break
64
}
65
})
66
67
function sayItems (items = null) {
68
if (!items) {
69
items = bot.inventory.items()
70
if (bot.registry.isNewerOrEqualTo('1.9') && bot.inventory.slots[45]) items.push(bot.inventory.slots[45])
71
}
72
const output = items.map(itemToString).join(', ')
73
if (output) {
74
bot.chat(output)
75
} else {
76
bot.chat('empty')
77
}
78
}
79
80
async function tossItem (name, amount) {
81
amount = parseInt(amount, 10)
82
const item = itemByName(name)
83
if (!item) {
84
bot.chat(`I have no ${name}`)
85
} else {
86
try {
87
if (amount) {
88
await bot.toss(item.type, null, amount)
89
bot.chat(`tossed ${amount} x ${name}`)
90
} else {
91
await bot.tossStack(item)
92
bot.chat(`tossed ${name}`)
93
}
94
} catch (err) {
95
bot.chat(`unable to toss: ${err.message}`)
96
}
97
}
98
}
99
100
async function equipItem (name, destination) {
101
const item = itemByName(name)
102
if (item) {
103
try {
104
await bot.equip(item, destination)
105
bot.chat(`equipped ${name}`)
106
} catch (err) {
107
bot.chat(`cannot equip ${name}: ${err.message}`)
108
}
109
} else {
110
bot.chat(`I have no ${name}`)
111
}
112
}
113
114
async function unequipItem (destination) {
115
try {
116
await bot.unequip(destination)
117
bot.chat('unequipped')
118
} catch (err) {
119
bot.chat(`cannot unequip: ${err.message}`)
120
}
121
}
122
123
function useEquippedItem () {
124
bot.chat('activating item')
125
bot.activateItem()
126
}
127
128
async function craftItem (name, amount) {
129
amount = parseInt(amount, 10)
130
const item = bot.registry.itemsByName[name]
131
const craftingTableID = bot.registry.blocksByName.crafting_table.id
132
133
const craftingTable = bot.findBlock({
134
matching: craftingTableID
135
})
136
137
if (item) {
138
const recipe = bot.recipesFor(item.id, null, 1, craftingTable)[0]
139
if (recipe) {
140
bot.chat(`I can make ${name}`)
141
try {
142
await bot.craft(recipe, amount, craftingTable)
143
bot.chat(`did the recipe for ${name} ${amount} times`)
144
} catch (err) {
145
bot.chat(`error making ${name}`)
146
}
147
} else {
148
bot.chat(`I cannot make ${name}`)
149
}
150
} else {
151
bot.chat(`unknown item: ${name}`)
152
}
153
}
154
155
function itemToString (item) {
156
if (item) {
157
return `${item.name} x ${item.count}`
158
} else {
159
return '(nothing)'
160
}
161
}
162
163
function itemByName (name) {
164
const items = bot.inventory.items()
165
if (bot.registry.isNewerOrEqualTo('1.9') && bot.inventory.slots[45]) items.push(bot.inventory.slots[45])
166
return items.filter(item => item.name === name)[0]
167
}
168
169