Enhance crafting error handling and logging in craftItem function

This commit is contained in:
MayaTheShy
2026-03-16 00:40:39 -04:00
parent 473570f398
commit 59ce10b986

View File

@@ -1471,15 +1471,23 @@ end
-- since remote turtles may not expose the inventory API (list/pushItems). -- since remote turtles may not expose the inventory API (list/pushItems).
local function craftItem(recipeIdx) local function craftItem(recipeIdx)
local recipe = CRAFTABLE[recipeIdx] local recipe = CRAFTABLE[recipeIdx]
if not recipe then return false, "Invalid recipe" end if not recipe then
if not craftTurtleName then return false, "No turtle" end print("[CRAFT] Invalid recipe index: " .. tostring(recipeIdx))
if not networkModem then return false, "No modem" end return false, "Invalid recipe"
end
if not craftTurtleName then
print("[CRAFT] No turtle detected on network")
return false, "No turtle"
end
-- Verify the turtle is still on the network -- Verify the turtle is still on the network
if not peripheral.isPresent(craftTurtleName) then if not peripheral.isPresent(craftTurtleName) then
print("[CRAFT] Turtle offline: " .. craftTurtleName)
return false, "Turtle offline" return false, "Turtle offline"
end end
print(string.format("[CRAFT] Starting craft: %s (turtle: %s)", recipe.output, craftTurtleName))
activity.crafting = true activity.crafting = true
needsRedraw = true needsRedraw = true
smelterNeedsRedraw = true smelterNeedsRedraw = true
@@ -1520,18 +1528,26 @@ local function craftItem(recipeIdx)
if chest then if chest then
for slot, slotItem in pairs(chest.list()) do for slot, slotItem in pairs(chest.list()) do
if slotItem.name == itemName then if slotItem.name == itemName then
local n = chest.pushItems(craftTurtleName, slot, 1, turtleSlot) print(string.format("[CRAFT] Pushing %s from %s slot %d -> turtle slot %d", itemName, source.chest, slot, turtleSlot))
if n and n > 0 then local ok, n = pcall(chest.pushItems, craftTurtleName, slot, 1, turtleSlot)
if ok and n and n > 0 then
adjustCache(itemName, source.chest, -n) adjustCache(itemName, source.chest, -n)
placedItems[turtleSlot] = itemName placedItems[turtleSlot] = itemName
placed = true placed = true
print(string.format("[CRAFT] Placed %s in turtle slot %d", itemName, turtleSlot))
break break
elseif not ok then
print(string.format("[CRAFT] pushItems error: %s", tostring(n)))
else
print(string.format("[CRAFT] pushItems returned %s", tostring(n)))
end end
end end
end end
end end
if placed then break end if placed then break end
end end
else
print(string.format("[CRAFT] Item %s not in catalogue!", itemName))
end end
if not placed then if not placed then
-- Cleanup: pull placed items back using tracked names -- Cleanup: pull placed items back using tracked names