From ea7499c2f2e117fe62eb3466fc4a8884db80cea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E5=8A=B2=E5=AE=87?= <137690584+isaacJinyu@users.noreply.github.com> Date: Wed, 27 Sep 2023 02:01:23 +0800 Subject: [PATCH] Create craftItem.js --- .../minecraft/control_primitives/craftItem.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 metagpt/actions/minecraft/control_primitives/craftItem.js diff --git a/metagpt/actions/minecraft/control_primitives/craftItem.js b/metagpt/actions/minecraft/control_primitives/craftItem.js new file mode 100644 index 000000000..a26090582 --- /dev/null +++ b/metagpt/actions/minecraft/control_primitives/craftItem.js @@ -0,0 +1,43 @@ +async function craftItem(bot, name, count = 1) { + // return if name is not string + if (typeof name !== "string") { + throw new Error("name for craftItem must be a string"); + } + // return if count is not number + 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" + ); + } + } +}