feat: ✨ update inventory
This commit is contained in:
		| @@ -10,22 +10,18 @@ | ||||
|  */ | ||||
| const mineflayer = require('mineflayer') | ||||
|  | ||||
| // if (process.argv.length < 4 || process.argv.length > 6) { | ||||
| //   console.log('Usage : node inventory.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] : 'inventory', | ||||
| //   password: process.argv[5] | ||||
| // }) | ||||
|  | ||||
| let cfg = {} | ||||
| let bot = {} | ||||
| // let mcd | ||||
|  | ||||
| const alt_destinations = { | ||||
|     offhand: "off-hand", | ||||
|     lefthand: "off-hand", | ||||
|     shield: "off-hand", | ||||
|     chest: "torso", | ||||
| } | ||||
|  | ||||
| function inventory(username, message) { | ||||
|     if (username === bot.username) return | ||||
|     const command = message.split(' ') | ||||
| @@ -72,8 +68,8 @@ function inventory(username, message) { | ||||
|  | ||||
| function sayItems(items = bot.inventory.items()) { | ||||
|     const output = items.map(itemToString).join(', ') | ||||
|     console.info("inventory:", output) | ||||
|     if (output) { | ||||
|         console.info("inventory:", output) | ||||
|         !cfg.quiet && bot.chat(output) | ||||
|     } else { | ||||
|         !cfg.quiet && bot.chat('empty') | ||||
| @@ -104,8 +100,20 @@ function tossItem(name, amount) { | ||||
|  | ||||
| function equipItem(name, destination, quiet = false) { | ||||
|     const item = itemByName(name) | ||||
|  | ||||
|     if (item) { | ||||
|         bot.equip(item, destination, checkIfEquipped) | ||||
|         if (Object.keys(alt_destinations).includes(destination)) { | ||||
|             destination = alt_destinations[destination] | ||||
|         } | ||||
|         try { | ||||
|             bot.equip(item, destination, checkIfEquipped) | ||||
|         } catch (error) { | ||||
|             if (error.code == 'ERR_ASSERTION' && error.message.startsWith("invalid destination:")) { | ||||
|                 bot.chat(error.message) | ||||
|             } else { | ||||
|                 console.error(error) | ||||
|             } | ||||
|         } | ||||
|     } else { | ||||
|         !quiet && bot.chat(`I have no ${name}`) | ||||
|     } | ||||
| @@ -120,6 +128,9 @@ function equipItem(name, destination, quiet = false) { | ||||
| } | ||||
|  | ||||
| function unequipItem(destination) { | ||||
|     if (Object.keys(alt_destinations).includes(destination)) { | ||||
|         destination = alt_destinations[destination] | ||||
|     } | ||||
|     bot.unequip(destination, (err) => { | ||||
|         if (err) { | ||||
|             bot.chat(`cannot unequip: ${err.message}`) | ||||
| @@ -134,27 +145,54 @@ function useEquippedItem() { | ||||
|     bot.activateItem() | ||||
| } | ||||
|  | ||||
| function craftItem(name, amount) { | ||||
| function craftItem(name, amount = 1) { | ||||
|     amount = parseInt(amount, 10) | ||||
|     const item = require('minecraft-data')(bot.version).findItemOrBlockByName(name) | ||||
|     const mcData = require('minecraft-data')(bot.version) | ||||
|     const item = mcData.findItemOrBlockByName(name) | ||||
|     const craftingTable = bot.findBlock({ | ||||
|         matching: 58 | ||||
|         matching: mcData.blocksByName["crafting_table"].id | ||||
|     }) | ||||
|  | ||||
|     const recipesNoTable = bot.recipesFor(item.id) | ||||
|     let recipes | ||||
|  | ||||
|     if (recipesNoTable.length > 0) { | ||||
|         bot.chat("can make without crafting table!") | ||||
|         recipes = recipesNoTable | ||||
|     } else if (craftingTable) { | ||||
|         recipes = bot.recipesFor(item.id, null, null, craftingTable) | ||||
|     } else { | ||||
|         bot.chat("Couldn't find a crafting table. Maybe craft one?") | ||||
|     } | ||||
|  | ||||
|     if (item) { | ||||
|         const recipe = bot.recipesFor(item.id, null, 1, craftingTable)[0] | ||||
|         if (recipe) { | ||||
|             bot.chat(`I can make ${name}`) | ||||
|             bot.craft(recipe, amount, craftingTable, (err) => { | ||||
|                 if (err) { | ||||
|                     bot.chat(`error making ${name}`) | ||||
|                 } else { | ||||
|                     bot.chat(`did the recipe for ${name} ${amount} times`) | ||||
|                 } | ||||
|             }) | ||||
|         } else { | ||||
|             bot.chat(`I cannot make ${name}`) | ||||
|         let craftSuccess | ||||
|         recipes && cfg.quiet || bot.chat(`${recipes.length} recipes`) | ||||
|         for (let recipe in recipes) { | ||||
|             if (craftSuccess) { | ||||
|                 break | ||||
|             } | ||||
|             setTimeout(craftIt, Math.random() * 5000, recipes[recipe], amount, craftingTable) | ||||
|         } | ||||
|         function craftIt(recipe, amount, craftingTable) { | ||||
|             cfg.quiet || bot.chat(`I can make ${name}`) | ||||
|             console.log("craft:", recipe) | ||||
|             console.time("craft recipe") | ||||
|             bot.craft(recipe, amount, craftingTable, craftError) | ||||
|             console.timeEnd("craft recipe") | ||||
|         } | ||||
|         function craftError(err) { | ||||
|             if (err) { | ||||
|                 console.error(err) | ||||
|                 cfg.quiet || bot.chat(`error making ${name}: ${err.message}`) | ||||
|                 // continue | ||||
|             } else { | ||||
|                 craftSuccess = true | ||||
|                 bot.chat(`did the recipe for ${name} ${amount} times`) | ||||
|                 // break | ||||
|             } | ||||
|         } | ||||
|         craftSuccess || cfg.quiet || bot.chat(`I couldn't make ${name}`) | ||||
|     } else { | ||||
|         bot.chat(`unknown item: ${name}`) | ||||
|     } | ||||
| @@ -187,4 +225,4 @@ const unload = () => { | ||||
|     bot.off('chat', inventory) | ||||
| } | ||||
|  | ||||
| module.exports = { load, unload, equipItem } | ||||
| module.exports = { load, unload, equipItem, craftItem } | ||||
		Reference in New Issue
	
	Block a user