mineflayer-crafting-util
Version:
A plugin to simplify crafting recipes.
62 lines (61 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.craftPlan = craftPlan;
exports.craftItem = craftItem;
exports.setupActualCrafting = setupActualCrafting;
function getInventoryItems(bot) {
return bot.inventory.slots
.filter(item => item != null)
.map(item => ({ id: item.type, count: item.count }));
}
function splitApplications(applications, stackSize) {
if (stackSize <= 0 || applications <= stackSize)
return [applications];
const chunks = [];
let remaining = applications;
while (remaining > 0) {
const count = Math.min(remaining, stackSize);
chunks.push(count);
remaining -= count;
}
return chunks;
}
async function craftPlan(bot, plan, craftingTable, options = {}) {
var _a, _b;
if (craftingTable == null) {
throw new Error('craftingTable is required');
}
if (plan.status !== 'complete') {
throw new Error(`Cannot craft a plan with status "${plan.status}"`);
}
// it is so dumb that I have to support this but whatever.
for (const info of plan.recipesToDo) {
const stackSize = (_b = (_a = bot.registry.items[info.recipe.result.id]) === null || _a === void 0 ? void 0 : _a.stackSize) !== null && _b !== void 0 ? _b : info.recipeApplications;
const applicationCounts = options.strict === true
? splitApplications(info.recipeApplications, stackSize)
: [info.recipeApplications];
for (const applicationCount of applicationCounts) {
// console.log(`crafting ${bot.registry.items[info.recipe.result.id].name} x ${applicationCount}`)
await bot.craft(info.recipe, applicationCount, craftingTable);
// console.log(bot.inventory.slots.filter(i => !!i).map(i => [i.name, i.count, i.stackSize]))
}
}
return plan;
}
async function craftItem(bot, itemId, count, craftingTable, options = {}, craftOptions = {}) {
var _a, _b, _c, _d;
const availableItems = ((_a = options.availableItems) !== null && _a !== void 0 ? _a : getInventoryItems(bot))
.map(item => (Object.assign({}, item)));
const plan = bot.planCraft({ id: itemId, count }, Object.assign(Object.assign({}, options), { availableItems, careAboutExisting: (_b = options.careAboutExisting) !== null && _b !== void 0 ? _b : false, includeRecursion: (_c = options.includeRecursion) !== null && _c !== void 0 ? _c : true, multipleRecipes: (_d = options.multipleRecipes) !== null && _d !== void 0 ? _d : true }));
await craftPlan(bot, plan, craftingTable, craftOptions);
return plan;
}
function setupActualCrafting(bot) {
const craftingBot = bot;
craftingBot.craftPlan = async (plan, craftingTable, options) => {
return await craftPlan(bot, plan, craftingTable, options);
};
craftingBot.craftItem = async (itemId, count, craftingTable, options, craftOptions) => {
return await craftItem(craftingBot, itemId, count, craftingTable, options, craftOptions);
};
}