Create craftItem.js

This commit is contained in:
向劲宇 2023-09-27 02:01:23 +08:00 committed by GitHub
parent d70a7f7631
commit ea7499c2f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View 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"
);
}
}
}