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).
local function craftItem(recipeIdx)
local recipe = CRAFTABLE[recipeIdx]
if not recipe then return false, "Invalid recipe" end
if not craftTurtleName then return false, "No turtle" end
if not networkModem then return false, "No modem" end
if not recipe then
print("[CRAFT] Invalid recipe index: " .. tostring(recipeIdx))
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
if not peripheral.isPresent(craftTurtleName) then
print("[CRAFT] Turtle offline: " .. craftTurtleName)
return false, "Turtle offline"
end
print(string.format("[CRAFT] Starting craft: %s (turtle: %s)", recipe.output, craftTurtleName))
activity.crafting = true
needsRedraw = true
smelterNeedsRedraw = true
@@ -1520,18 +1528,26 @@ local function craftItem(recipeIdx)
if chest then
for slot, slotItem in pairs(chest.list()) do
if slotItem.name == itemName then
local n = chest.pushItems(craftTurtleName, slot, 1, turtleSlot)
if n and n > 0 then
print(string.format("[CRAFT] Pushing %s from %s slot %d -> turtle slot %d", itemName, source.chest, slot, turtleSlot))
local ok, n = pcall(chest.pushItems, craftTurtleName, slot, 1, turtleSlot)
if ok and n and n > 0 then
adjustCache(itemName, source.chest, -n)
placedItems[turtleSlot] = itemName
placed = true
print(string.format("[CRAFT] Placed %s in turtle slot %d", itemName, turtleSlot))
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
if placed then break end
end
else
print(string.format("[CRAFT] Item %s not in catalogue!", itemName))
end
if not placed then
-- Cleanup: pull placed items back using tracked names