From 8b8279878a749e52c01231a7f07e2cdaca25ec17 Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Sun, 22 Mar 2026 22:26:10 -0400 Subject: [PATCH] Fix craft dispatch: add turtle attach/detach handlers, re-scan on craft, log failures - Add peripheral_attach handler to auto-detect crafting turtle connecting after boot - Update peripheral_detach handler to clear craftTurtleName when turtle disconnects - Re-scan peripherals for turtle in display.lua craft handler if none known - Add log.warn when craft turtle check fails (was silent notification only) - Sync ctx.craftTurtleOk from broadcastState so display.lua has current value --- inventoryManager.lua | 19 +++++++++++++++++++ manager/display.lua | 12 ++++++++++++ 2 files changed, 31 insertions(+) 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