File size: 2,481 Bytes
36ba3ef |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
async function smeltItem(bot, itemName, fuelName, count = 1) {
// return if itemName or fuelName is not string
if (typeof itemName !== "string" || typeof fuelName !== "string") {
throw new Error("itemName or fuelName for smeltItem must be a string");
}
// return if count is not a number
if (typeof count !== "number") {
throw new Error("count for smeltItem must be a number");
}
const item = mcData.itemsByName[itemName];
const fuel = mcData.itemsByName[fuelName];
if (!item) {
throw new Error(`No item named ${itemName}`);
}
if (!fuel) {
throw new Error(`No item named ${fuelName}`);
}
const furnaceBlock = bot.findBlock({
matching: mcData.blocksByName.furnace.id,
maxDistance: 32,
});
if (!furnaceBlock) {
throw new Error("No furnace nearby");
} else {
await bot.pathfinder.goto(
new GoalLookAtBlock(furnaceBlock.position, bot.world)
);
}
const furnace = await bot.openFurnace(furnaceBlock);
let success_count = 0;
for (let i = 0; i < count; i++) {
if (!bot.inventory.findInventoryItem(item.id, null)) {
bot.chat(`No ${itemName} to smelt in inventory`);
break;
}
if (furnace.fuelSeconds < 15 && furnace.fuelItem()?.name !== fuelName) {
if (!bot.inventory.findInventoryItem(fuel.id, null)) {
bot.chat(`No ${fuelName} as fuel in inventory`);
break;
}
await furnace.putFuel(fuel.id, null, 1);
await bot.waitForTicks(20);
if (!furnace.fuel && furnace.fuelItem()?.name !== fuelName) {
throw new Error(`${fuelName} is not a valid fuel`);
}
}
await furnace.putInput(item.id, null, 1);
await bot.waitForTicks(12 * 20);
if (!furnace.outputItem()) {
throw new Error(`${itemName} is not a valid input`);
}
await furnace.takeOutput();
success_count++;
}
furnace.close();
if (success_count > 0) bot.chat(`Smelted ${success_count} ${itemName}.`);
else {
bot.chat(
`Failed to smelt ${itemName}, please check the fuel and input.`
);
_smeltItemFailCount++;
if (_smeltItemFailCount > 10) {
throw new Error(
`smeltItem failed too many times, please check the fuel and input.`
);
}
}
}
|