fix: improve error handling in crafting turtle script
This commit is contained in:
@@ -4,6 +4,15 @@
|
|||||||
-- Pulls ingredients from chests, crafts, and pushes results back.
|
-- Pulls ingredients from chests, crafts, and pushes results back.
|
||||||
-- Requires a wired modem attached to the turtle.
|
-- Requires a wired modem attached to the turtle.
|
||||||
|
|
||||||
|
local shell = _ENV.shell
|
||||||
|
|
||||||
|
local function fatal(msg)
|
||||||
|
printError(msg)
|
||||||
|
print("\nPress any key to exit...")
|
||||||
|
os.pullEvent("key")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
-- Default configuration (overridden by .turtle_config)
|
-- Default configuration (overridden by .turtle_config)
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
@@ -52,9 +61,7 @@ print("")
|
|||||||
|
|
||||||
-- Verify this is a crafting turtle
|
-- Verify this is a crafting turtle
|
||||||
if not turtle or not turtle.craft then
|
if not turtle or not turtle.craft then
|
||||||
print("[ERR] No turtle.craft() available!")
|
return fatal("[ERR] No turtle.craft() available!\n This must be a Crafting Turtle.")
|
||||||
print(" This must be a Crafting Turtle.")
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Find wired modem and get our network name
|
-- Find wired modem and get our network name
|
||||||
@@ -78,10 +85,7 @@ for _, side in ipairs({"top", "bottom", "left", "right", "front", "back"}) do
|
|||||||
end
|
end
|
||||||
|
|
||||||
if not modem or not selfName then
|
if not modem or not selfName then
|
||||||
print("[ERR] No wired modem found!")
|
return fatal("[ERR] No wired modem found!\n Attach a wired modem to the turtle\n and connect it to the network.")
|
||||||
print(" Attach a wired modem to the turtle")
|
|
||||||
print(" and connect it to the network.")
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
print("[OK] Modem: " .. modemSide)
|
print("[OK] Modem: " .. modemSide)
|
||||||
@@ -94,9 +98,7 @@ print("[OK] Listening on channel " .. CRAFT_CHANNEL)
|
|||||||
-- Wrap our own inventory peripheral for pullItems/pushItems
|
-- Wrap our own inventory peripheral for pullItems/pushItems
|
||||||
local selfInv = peripheral.wrap(selfName)
|
local selfInv = peripheral.wrap(selfName)
|
||||||
if not selfInv then
|
if not selfInv then
|
||||||
print("[ERR] Cannot wrap own peripheral: " .. selfName)
|
return fatal("[ERR] Cannot wrap own peripheral: " .. selfName .. "\n Make sure the wired modem is connected.")
|
||||||
print(" Make sure the wired modem is connected.")
|
|
||||||
return
|
|
||||||
end
|
end
|
||||||
|
|
||||||
print("[OK] Self-inventory peripheral ready")
|
print("[OK] Self-inventory peripheral ready")
|
||||||
@@ -266,20 +268,26 @@ end
|
|||||||
-- Main loop: listen for modem commands
|
-- Main loop: listen for modem commands
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
while true do
|
local ok, err = pcall(function()
|
||||||
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
|
while true do
|
||||||
|
local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
|
||||||
|
|
||||||
if channel == CRAFT_CHANNEL and type(message) == "table" then
|
if channel == CRAFT_CHANNEL and type(message) == "table" then
|
||||||
if message.type == "craft_request" then
|
if message.type == "craft_request" then
|
||||||
local result = handleCraftCommand(message)
|
local result = handleCraftCommand(message)
|
||||||
-- Send result back to master
|
-- Send result back to master
|
||||||
modem.transmit(CRAFT_REPLY_CHANNEL, CRAFT_CHANNEL, result)
|
modem.transmit(CRAFT_REPLY_CHANNEL, CRAFT_CHANNEL, result)
|
||||||
elseif message.type == "ping" then
|
elseif message.type == "ping" then
|
||||||
-- Health check from master
|
-- Health check from master
|
||||||
modem.transmit(CRAFT_REPLY_CHANNEL, CRAFT_CHANNEL, {
|
modem.transmit(CRAFT_REPLY_CHANNEL, CRAFT_CHANNEL, {
|
||||||
type = "pong",
|
type = "pong",
|
||||||
name = selfName,
|
name = selfName,
|
||||||
})
|
})
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
if not ok then
|
||||||
|
fatal("[ERR] Crafting turtle crashed:\n" .. tostring(err))
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user