From 39b95b366379960bd120b97371ff1651a736c9b7 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Sun, 22 Mar 2026 01:56:40 -0400 Subject: [PATCH] Refactor crafting helpers to delegate functionality to shared UI module --- inventoryManager.lua | 71 ++++++++++---------------------------------- 1 file changed, 16 insertions(+), 55 deletions(-) diff --git a/inventoryManager.lua b/inventoryManager.lua index f3f6067..6924c0a 100644 --- a/inventoryManager.lua +++ b/inventoryManager.lua @@ -1007,73 +1007,34 @@ local function drawDashboard() end ------------------------------------------------- --- Crafting helpers +-- Crafting helpers (delegated to shared ui module) ------------------------------------------------- ---- Get ingredient counts from a recipe's grid +--- Get stock total for an item from catalogue +local function getItemTotal(itemName) + local have = 0 + if cache.catalogue[itemName] then + for _, src in ipairs(cache.catalogue[itemName]) do + have = have + src.total + end + end + return have +end + local function getRecipeIngredients(recipe) - local ingredients = {} - for _, item in ipairs(recipe.grid) do - if item then - ingredients[item] = (ingredients[item] or 0) + 1 - end - end - return ingredients + return ui.getRecipeIngredients(recipe) end ---- Check if a recipe can be crafted with current stock local function canCraftRecipe(recipe) - local ingredients = getRecipeIngredients(recipe) - for itemName, needed in pairs(ingredients) do - local have = 0 - if cache.catalogue[itemName] then - for _, src in ipairs(cache.catalogue[itemName]) do - have = have + src.total - end - end - if have < needed then return false end - end - return true + return ui.canCraftRecipe(recipe, getItemTotal) end ---- How many times a recipe can be crafted local function maxCraftBatches(recipe) - local ingredients = getRecipeIngredients(recipe) - local minBatches = math.huge - for itemName, needed in pairs(ingredients) do - local have = 0 - if cache.catalogue[itemName] then - for _, src in ipairs(cache.catalogue[itemName]) do - have = have + src.total - end - end - local batches = math.floor(have / needed) - if batches < minBatches then minBatches = batches end - end - if minBatches == math.huge then return 0 end - return minBatches + return ui.maxCraftBatches(recipe, getItemTotal) end ---- Get list of missing ingredients for a recipe local function getMissingIngredients(recipe) - local ingredients = getRecipeIngredients(recipe) - local missing = {} - for itemName, needed in pairs(ingredients) do - local have = 0 - if cache.catalogue[itemName] then - for _, src in ipairs(cache.catalogue[itemName]) do - have = have + src.total - end - end - if have < needed then - table.insert(missing, { - name = itemName, - have = have, - need = needed, - }) - end - end - return missing + return ui.getMissingIngredients(recipe, getItemTotal) end --- Execute a craft via the networked turtle.