mirror of
https://github.com/FoundationAgents/MetaGPT.git
synced 2026-06-17 15:35:21 +02:00
Create craftItem.js
This commit is contained in:
parent
d70a7f7631
commit
ea7499c2f2
1 changed files with 43 additions and 0 deletions
43
metagpt/actions/minecraft/control_primitives/craftItem.js
Normal file
43
metagpt/actions/minecraft/control_primitives/craftItem.js
Normal file
|
|
@ -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"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue