| | async function craftItem(bot, name, count = 1) { |
| | |
| | if (typeof name !== "string") { |
| | throw new Error("name for craftItem must be a string"); |
| | } |
| | |
| | if (typeof count !== "number") { |
| | throw new Error("count for craftItem must be a number"); |
| | } |
| | const itemByName = mcData.itemsByName[name]; |
| | if (!itemByName) { |
| | throw new Error(`No item named ${name}`); |
| | } |
| | const craftingTable = bot.findBlock({ |
| | matching: mcData.blocksByName.crafting_table.id, |
| | maxDistance: 32, |
| | }); |
| | if (!craftingTable) { |
| | bot.chat("Craft without a crafting table"); |
| | } else { |
| | await bot.pathfinder.goto( |
| | new GoalLookAtBlock(craftingTable.position, bot.world) |
| | ); |
| | } |
| | const recipe = bot.recipesFor(itemByName.id, null, 1, craftingTable)[0]; |
| | if (recipe) { |
| | bot.chat(`I can make ${name}`); |
| | try { |
| | await bot.craft(recipe, count, craftingTable); |
| | bot.chat(`I did the recipe for ${name} ${count} times`); |
| | } catch (err) { |
| | bot.chat(`I cannot do the recipe for ${name} ${count} times`); |
| | } |
| | } else { |
| | failedCraftFeedback(bot, name, itemByName, craftingTable); |
| | _craftItemFailCount++; |
| | if (_craftItemFailCount > 10) { |
| | throw new Error( |
| | "craftItem failed too many times, check chat log to see what happened" |
| | ); |
| | } |
| | } |
| | } |
| |
|