You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
228 lines
6.2 KiB
228 lines
6.2 KiB
/* |
|
* Using the inventory is one of the first things you learn in Minecraft, |
|
* now it's time to teach your bot the same skill. |
|
* |
|
* Command your bot with chat messages and make him toss, equip, use items |
|
* and even craft new items using the built-in recipe book. |
|
* |
|
* To learn more about the recipe system and how crafting works |
|
* remember to read the API documentation! |
|
*/ |
|
const mineflayer = require('mineflayer') |
|
|
|
|
|
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(' ') |
|
switch (true) { |
|
// case message === 'loaded': |
|
// bot.waitForChunksToLoad(() => { |
|
// bot.chat('Ready!') |
|
// }) |
|
// break |
|
case /^list$/.test(message): |
|
sayItems() |
|
break |
|
case /^toss \d+ \w+$/.test(message): |
|
// toss amount name |
|
// ex: toss 64 diamond |
|
tossItem(command[2], command[1]) |
|
break |
|
case /^toss \w+$/.test(message): |
|
// toss name |
|
// ex: toss diamond |
|
tossItem(command[1]) |
|
break |
|
case /^equip \w+ \w+$/.test(message): |
|
// equip destination name |
|
// ex: equip hand diamond |
|
equipItem(command[2], command[1], quiet = cfg.quiet) |
|
break |
|
case /^unequip \w+$/.test(message): |
|
// unequip testination |
|
// ex: unequip hand |
|
unequipItem(command[1]) |
|
break |
|
case /^use$/.test(message): |
|
useEquippedItem() |
|
break |
|
case /^craft \d+ \w+$/.test(message): |
|
// craft amount item |
|
// ex: craft 64 stick |
|
craftItem(command[2], command[1]) |
|
break |
|
} |
|
} |
|
|
|
|
|
function sayItems(items = bot.inventory.items()) { |
|
const output = items.map(itemToString).join(', ') |
|
console.info("inventory:", output) |
|
if (output) { |
|
!cfg.quiet && bot.chat(output) |
|
} else { |
|
!cfg.quiet && bot.chat('empty') |
|
} |
|
} |
|
|
|
function tossItem(name, amount) { |
|
amount = parseInt(amount, 10) |
|
const item = itemByName(name) |
|
if (!item) { |
|
bot.chat(`I have no ${name}`) |
|
} else if (amount) { |
|
bot.toss(item.type, null, amount, checkIfTossed) |
|
} else { |
|
bot.tossStack(item, checkIfTossed) |
|
} |
|
|
|
function checkIfTossed(err) { |
|
if (err) { |
|
bot.chat(`unable to toss: ${err.message}`) |
|
} else if (amount) { |
|
bot.chat(`tossed ${amount} x ${name}`) |
|
} else { |
|
bot.chat(`tossed ${name}`) |
|
} |
|
} |
|
} |
|
|
|
function equipItem(name, destination, quiet = false) { |
|
const item = itemByName(name) |
|
|
|
if (item) { |
|
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}`) |
|
} |
|
|
|
function checkIfEquipped(err) { |
|
if (err) { |
|
bot.chat(`cannot equip ${name}: ${err.message}`) |
|
} else { |
|
!quiet && bot.chat(`equipped ${name}`) |
|
} |
|
} |
|
} |
|
|
|
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}`) |
|
} else { |
|
bot.chat('unequipped') |
|
} |
|
}) |
|
} |
|
|
|
function useEquippedItem() { |
|
bot.chat('activating item') |
|
bot.activateItem() |
|
} |
|
|
|
function craftItem(name, amount = 1) { |
|
amount = parseInt(amount, 10) |
|
const mcData = require('minecraft-data')(bot.version) |
|
const item = mcData.findItemOrBlockByName(name) |
|
const craftingTable = bot.findBlock({ |
|
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) { |
|
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}`) |
|
} |
|
} |
|
|
|
function itemToString(item) { |
|
if (item) { |
|
return `${item.name} x ${item.count}` |
|
} else { |
|
return '(nothing)' |
|
} |
|
} |
|
|
|
function itemByName(name) { |
|
return bot.inventory.items().filter(item => item.name === name)[0] |
|
} |
|
|
|
|
|
const load = (config) => { |
|
cfg = config |
|
bot = cfg.bot |
|
// cfg.inventory = { |
|
// auto: true, |
|
// quiet: false |
|
// } |
|
bot.on('chat', inventory) |
|
} |
|
|
|
const unload = () => { |
|
bot.off('chat', inventory) |
|
} |
|
|
|
module.exports = { load, unload, equipItem, craftItem, itemByName } |