From d70a7f76310c28c0ca8b75ea8780a233bb4650dd 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:00:41 +0800 Subject: [PATCH] Create craftHelper.js --- .../control_primitives/craftHelper.js | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 metagpt/actions/minecraft/control_primitives/craftHelper.js diff --git a/metagpt/actions/minecraft/control_primitives/craftHelper.js b/metagpt/actions/minecraft/control_primitives/craftHelper.js new file mode 100644 index 000000000..41ae1f091 --- /dev/null +++ b/metagpt/actions/minecraft/control_primitives/craftHelper.js @@ -0,0 +1,61 @@ +function failedCraftFeedback(bot, name, item, craftingTable) { + const recipes = bot.recipesAll(item.id, null, craftingTable); + if (!recipes.length) { + throw new Error(`No crafting table nearby`); + } else { + const recipes = bot.recipesAll( + item.id, + null, + mcData.blocksByName.crafting_table.id + ); + // find the recipe with the fewest missing ingredients + var min = 999; + var min_recipe = null; + for (const recipe of recipes) { + const delta = recipe.delta; + var missing = 0; + for (const delta_item of delta) { + if (delta_item.count < 0) { + const inventory_item = bot.inventory.findInventoryItem( + mcData.items[delta_item.id].name, + null + ); + if (!inventory_item) { + missing += -delta_item.count; + } else { + missing += Math.max( + -delta_item.count - inventory_item.count, + 0 + ); + } + } + } + if (missing < min) { + min = missing; + min_recipe = recipe; + } + } + const delta = min_recipe.delta; + let message = ""; + for (const delta_item of delta) { + if (delta_item.count < 0) { + const inventory_item = bot.inventory.findInventoryItem( + mcData.items[delta_item.id].name, + null + ); + if (!inventory_item) { + message += ` ${-delta_item.count} more ${ + mcData.items[delta_item.id].name + }, `; + } else { + if (inventory_item.count < -delta_item.count) { + message += `${ + -delta_item.count - inventory_item.count + } more ${mcData.items[delta_item.id].name}`; + } + } + } + } + bot.chat(`I cannot make ${name} because I need: ${message}`); + } +}