Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/examples/inventory.js
Views: 788
/*1* Using the inventory is one of the first things you learn in Minecraft,2* now it's time to teach your bot the same skill.3*4* Command your bot with chat messages and make him toss, equip, use items5* and even craft new items using the built-in recipe book.6*7* To learn more about the recipe system and how crafting works8* remember to read the API documentation!9*/10const mineflayer = require('mineflayer')1112if (process.argv.length < 4 || process.argv.length > 6) {13console.log('Usage : node inventory.js <host> <port> [<name>] [<password>]')14process.exit(1)15}1617const bot = mineflayer.createBot({18host: process.argv[2],19port: parseInt(process.argv[3]),20username: process.argv[4] ? process.argv[4] : 'inventory',21password: process.argv[5]22})2324bot.on('chat', async (username, message) => {25if (username === bot.username) return26const command = message.split(' ')27switch (true) {28case message === 'loaded':29await bot.waitForChunksToLoad()30bot.chat('Ready!')31break32case /^list$/.test(message):33sayItems()34break35case /^toss \d+ \w+$/.test(message):36// toss amount name37// ex: toss 64 diamond38tossItem(command[2], command[1])39break40case /^toss \w+$/.test(message):41// toss name42// ex: toss diamond43tossItem(command[1])44break45case /^equip [\w-]+ \w+$/.test(message):46// equip destination name47// ex: equip hand diamond48equipItem(command[2], command[1])49break50case /^unequip \w+$/.test(message):51// unequip testination52// ex: unequip hand53unequipItem(command[1])54break55case /^use$/.test(message):56useEquippedItem()57break58case /^craft \d+ \w+$/.test(message):59// craft amount item60// ex: craft 64 stick61craftItem(command[2], command[1])62break63}64})6566function sayItems (items = null) {67if (!items) {68items = bot.inventory.items()69if (bot.registry.isNewerOrEqualTo('1.9') && bot.inventory.slots[45]) items.push(bot.inventory.slots[45])70}71const output = items.map(itemToString).join(', ')72if (output) {73bot.chat(output)74} else {75bot.chat('empty')76}77}7879async function tossItem (name, amount) {80amount = parseInt(amount, 10)81const item = itemByName(name)82if (!item) {83bot.chat(`I have no ${name}`)84} else {85try {86if (amount) {87await bot.toss(item.type, null, amount)88bot.chat(`tossed ${amount} x ${name}`)89} else {90await bot.tossStack(item)91bot.chat(`tossed ${name}`)92}93} catch (err) {94bot.chat(`unable to toss: ${err.message}`)95}96}97}9899async function equipItem (name, destination) {100const item = itemByName(name)101if (item) {102try {103await bot.equip(item, destination)104bot.chat(`equipped ${name}`)105} catch (err) {106bot.chat(`cannot equip ${name}: ${err.message}`)107}108} else {109bot.chat(`I have no ${name}`)110}111}112113async function unequipItem (destination) {114try {115await bot.unequip(destination)116bot.chat('unequipped')117} catch (err) {118bot.chat(`cannot unequip: ${err.message}`)119}120}121122function useEquippedItem () {123bot.chat('activating item')124bot.activateItem()125}126127async function craftItem (name, amount) {128amount = parseInt(amount, 10)129const item = bot.registry.itemsByName[name]130const craftingTableID = bot.registry.blocksByName.crafting_table.id131132const craftingTable = bot.findBlock({133matching: craftingTableID134})135136if (item) {137const recipe = bot.recipesFor(item.id, null, 1, craftingTable)[0]138if (recipe) {139bot.chat(`I can make ${name}`)140try {141await bot.craft(recipe, amount, craftingTable)142bot.chat(`did the recipe for ${name} ${amount} times`)143} catch (err) {144bot.chat(`error making ${name}`)145}146} else {147bot.chat(`I cannot make ${name}`)148}149} else {150bot.chat(`unknown item: ${name}`)151}152}153154function itemToString (item) {155if (item) {156return `${item.name} x ${item.count}`157} else {158return '(nothing)'159}160}161162function itemByName (name) {163const items = bot.inventory.items()164if (bot.registry.isNewerOrEqualTo('1.9') && bot.inventory.slots[45]) items.push(bot.inventory.slots[45])165return items.filter(item => item.name === name)[0]166}167168169