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
This commit is contained in:
MayaTheShy
2026-03-22 22:26:10 -04:00
parent f095e18e95
commit 8b8279878a
2 changed files with 31 additions and 0 deletions

View File

@@ -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,

View File

@@ -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