feat: enhance inventory management with recursive crafting and supply chest functionality

This commit is contained in:
MayaTheShy
2026-03-22 18:15:25 -04:00
parent 467be0493c
commit 37e9b89057

View File

@@ -19,8 +19,10 @@ local function _path(rel) return fs.combine(_baseDir, rel) end
-- Structured logging & shared UI helpers
-------------------------------------------------
local log = dofile(_path("lib/log.lua"))
local ui = dofile(_path("lib/ui.lua"))
local log = dofile(_path("lib/log.lua"))
local ui = dofile(_path("lib/ui.lua"))
local itemDB = dofile(_path("lib/itemDB.lua"))
itemDB.init(_path(".item_names.db"))
-------------------------------------------------
-- Load modules (factory pattern → shared context)
@@ -48,6 +50,12 @@ ctx.ops = ops
local display = dofile(_path("manager/display.lua"))(ctx)
ctx.display = display
-- Recursive crafting engine
local craftEngine = dofile(_path("lib/craft.lua"))
craftEngine.init(cfg.recipeBook, ops.getItemTotal)
ctx.craftEngine = craftEngine
ctx.itemDB = itemDB
-- Convenience aliases
local cache = state.cache
local activity = state.activity
@@ -196,7 +204,11 @@ local function main()
for k in pairs(cfg.SMELTABLE) do
if not state.disabledRecipes[k] then enabledCount = enabledCount + 1 end
end
log.info("INIT", "%d/%d recipes enabled", enabledCount, totalRecipeCount)
log.info("INIT", "%d/%d smelting recipes enabled", enabledCount, totalRecipeCount)
do
local cc, sc = cfg.recipeBook.count()
log.info("INIT", "Recipe book: %d crafting, %d smelting", cc, sc)
end
-- Compost peripherals
if peripheral.isPresent(cfg.COMPOST_DROPPER) then
@@ -304,6 +316,8 @@ local function main()
sleep(cfg.SCAN_INTERVAL)
pcall(ops.refreshCache)
pcall(ops.checkAlerts)
pcall(function() itemDB.flush() end)
pcall(function() cfg.recipeBook.flush() end)
state.needsRedraw = true
state.smelterNeedsRedraw = true
end
@@ -442,7 +456,17 @@ local function main()
end
end,
-- Task 12: Network order/command listener
-- Task 12: Supply chest (builder / manifest-based stocking)
function()
if cfg.SUPPLY_CHEST == "" or #cfg.SUPPLY_MANIFEST == 0 then return end
log.info("SUPPLY", "Stocking %s with %d item types", cfg.SUPPLY_CHEST, #cfg.SUPPLY_MANIFEST)
while true do
pcall(ops.supplyChest)
sleep(cfg.SUPPLY_INTERVAL)
end
end,
-- Task 13: Network order/command listener
function()
if not ctx.networkModem then return end
while true do
@@ -589,6 +613,56 @@ local function main()
state.smelterNeedsRedraw = true
state.needsRedraw = true
pcall(broadcastState)
elseif message.type == "recursive_craft" and message.itemName and message.count then
log.info("NET", "Recursive craft: %s x%d", message.itemName, message.count)
local pok, ok, craftErr = pcall(ops.recursiveCraft, message.itemName, message.count)
if not pok then
log.error("NET", "recursiveCraft crashed: %s", tostring(ok))
craftErr = tostring(ok)
ok = false
end
pcall(function()
ctx.networkModem.transmit(replyChannel, cfg.ORDER_CHANNEL, {
type = "recursive_craft_result",
commandId = message.commandId,
success = ok,
error = craftErr,
})
end)
state.smelterNeedsRedraw = true
state.needsRedraw = true
pcall(broadcastState)
elseif message.type == "learn_crafting_recipe" and message.output and message.count and message.grid then
cfg.recipeBook.learnCraftingRecipe(message.output, message.count, message.grid)
cfg.refreshRecipes()
cfg.recipeBook.flush()
log.info("NET", "Learned crafting recipe: %s", message.output)
state.configDirty = true
state.bumpStateVersion()
pcall(broadcastState)
elseif message.type == "learn_smelting_recipe" and message.input and message.result then
cfg.recipeBook.learnSmeltingRecipe(message.input, message.result, message.furnaces)
cfg.refreshRecipes()
cfg.recipeBook.flush()
log.info("NET", "Learned smelting recipe: %s -> %s", message.input, message.result)
state.configDirty = true
state.bumpStateVersion()
pcall(broadcastState)
elseif message.type == "forget_recipe" and message.recipe then
local forgot = cfg.recipeBook.forgetCraftingRecipe(message.recipe) or
cfg.recipeBook.forgetSmeltingRecipe(message.recipe)
if forgot then
cfg.refreshRecipes()
cfg.recipeBook.flush()
log.info("NET", "Forgot recipe: %s", message.recipe)
state.configDirty = true
state.bumpStateVersion()
end
pcall(broadcastState)
end
end) -- pcall handler