diff --git a/inventoryClient.lua b/inventoryClient.lua index 63a4fb9..61ee250 100644 --- a/inventoryClient.lua +++ b/inventoryClient.lua @@ -42,7 +42,6 @@ end -- Override dofile to load modules into our _ENV so they inherit -- Opus's require/package (CC:Tweaked dofile uses _G instead). -local _ccDofile = dofile local function dofile(path) -- luacheck: ignore local fn, err = loadfile(path, nil, _ENV) if fn then return fn() @@ -56,6 +55,7 @@ local CLIENT_CONFIG_FILE = _configPath(".client_config") ------------------------------------------------- local log = dofile(_path("lib/log.lua")) +local ui = dofile(_path("lib/ui.lua")) local function loadConfig() if not fs.exists(CLIENT_CONFIG_FILE) then return end @@ -184,46 +184,11 @@ local function getItemTotal(itemName) return 0 end -function ops.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 -end - -function ops.canCraftRecipe(recipe) - local ingredients = ops.getRecipeIngredients(recipe) - for itemName, needed in pairs(ingredients) do - if (getItemTotal(itemName) or 0) < needed then return false end - end - return true -end - -function ops.maxCraftBatches(recipe) - local ingredients = ops.getRecipeIngredients(recipe) - local minBatches = math.huge - for itemName, needed in pairs(ingredients) do - local batches = math.floor((getItemTotal(itemName) or 0) / needed) - if batches < minBatches then minBatches = batches end - end - if minBatches == math.huge then return 0 end - return minBatches -end - -function ops.getMissingIngredients(recipe) - local ingredients = ops.getRecipeIngredients(recipe) - local missing = {} - for itemName, needed in pairs(ingredients) do - local have = getItemTotal(itemName) or 0 - if have < needed then - table.insert(missing, { name = itemName, have = have, need = needed }) - end - end - return missing -end +-- Delegate recipe helpers to shared lib/ui.lua with local stock lookup. +function ops.getRecipeIngredients(recipe) return ui.getRecipeIngredients(recipe) end +function ops.canCraftRecipe(recipe) return ui.canCraftRecipe(recipe, getItemTotal) end +function ops.maxCraftBatches(recipe) return ui.maxCraftBatches(recipe, getItemTotal) end +function ops.getMissingIngredients(recipe) return ui.getMissingIngredients(recipe, getItemTotal) end function ops.orderItem(itemName, amount) sendToMaster({