diff --git a/inventoryManager.lua b/inventoryManager.lua index c09a25b..bfc7fdc 100644 --- a/inventoryManager.lua +++ b/inventoryManager.lua @@ -148,6 +148,9 @@ local function broadcastState() craftTurtleOk = ctx.craftTurtleName and peripheral.isPresent(ctx.craftTurtleName), } + -- Keep ctx in sync so display.lua can check ctx.craftTurtleOk directly + ctx.craftTurtleOk = payload.craftTurtleOk + if state.configDirty then payload.smeltable = cfg.SMELTABLE payload.craftable = cfg.CRAFTABLE @@ -505,6 +508,22 @@ local function main() ops.invalidateWrapCache(name) ops.invalidatePeripheralCaches() log.info("DETACH", "%s", name) + if name == ctx.craftTurtleName then + ctx.craftTurtleName = nil + log.warn("DETACH", "Crafting turtle disconnected") + end + end + end + end, + + -- Task 11b: Peripheral attach handler (auto-detect crafting turtle) + function() + while true do + local event, name = os.pullEvent("peripheral_attach") + if name and name:match("^turtle_") and not ctx.craftTurtleName then + ctx.craftTurtleName = name + log.info("ATTACH", "Crafting turtle detected: %s", name) + pcall(broadcastState) end end end, diff --git a/manager/display.lua b/manager/display.lua index e2b7eec..97b8aed 100644 --- a/manager/display.lua +++ b/manager/display.lua @@ -1141,10 +1141,22 @@ local function buildSmelterPage() local recipeIdx = event.selected.idx local recipe = cfg.CRAFTABLE[recipeIdx] if recipe then + -- Re-scan for turtle if none known + if not ctx.craftTurtleName then + for _, pName in ipairs(peripheral.getNames()) do + if pName:match("^turtle_") then + ctx.craftTurtleName = pName + log.info("CRAFT", "Turtle found on re-scan: %s", pName) + break + end + end + end local turtleOk = ctx.craftTurtleOk or (ctx.craftTurtleName and peripheral.isPresent(ctx.craftTurtleName)) if not turtleOk then + log.warn("CRAFT", "No crafting turtle! (name=%s)", + tostring(ctx.craftTurtleName)) self.notification:error("No crafting turtle!") return true end