style: Enhance eval response handling and inventory update for improved data management
This commit is contained in:
32
turtle.lua
32
turtle.lua
@@ -281,13 +281,25 @@ local function executeEval(uuid, code)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local ok, result = pcall(fn)
|
local results = table.pack(pcall(fn))
|
||||||
|
local ok = results[1]
|
||||||
|
|
||||||
if ok then
|
if ok then
|
||||||
|
local returnVal
|
||||||
|
if results.n <= 2 then
|
||||||
|
-- Single return value
|
||||||
|
returnVal = results[2]
|
||||||
|
else
|
||||||
|
-- Multiple return values - pack into array
|
||||||
|
returnVal = {}
|
||||||
|
for i = 2, results.n do
|
||||||
|
returnVal[i - 1] = results[i]
|
||||||
|
end
|
||||||
|
end
|
||||||
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||||
type = "eval_response",
|
type = "eval_response",
|
||||||
uuid = uuid,
|
uuid = uuid,
|
||||||
result = result,
|
result = returnVal,
|
||||||
error = nil,
|
error = nil,
|
||||||
turtleID = os.getComputerID()
|
turtleID = os.getComputerID()
|
||||||
})
|
})
|
||||||
@@ -296,7 +308,7 @@ local function executeEval(uuid, code)
|
|||||||
type = "eval_response",
|
type = "eval_response",
|
||||||
uuid = uuid,
|
uuid = uuid,
|
||||||
result = nil,
|
result = nil,
|
||||||
error = "Runtime error: " .. tostring(result),
|
error = "Runtime error: " .. tostring(results[2]),
|
||||||
turtleID = os.getComputerID()
|
turtleID = os.getComputerID()
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
@@ -650,16 +662,16 @@ parallel.waitForAny(
|
|||||||
while true do
|
while true do
|
||||||
os.pullEvent("turtle_inventory")
|
os.pullEvent("turtle_inventory")
|
||||||
local inventory = {}
|
local inventory = {}
|
||||||
local fns = {}
|
|
||||||
for i = 1, 16 do
|
for i = 1, 16 do
|
||||||
fns[i] = function()
|
local item = turtle.getItemDetail(i)
|
||||||
local item = turtle.getItemDetail(i, true)
|
if item then
|
||||||
if item then
|
table.insert(inventory, {
|
||||||
inventory[tostring(i)] = item
|
slot = i,
|
||||||
end
|
name = item.name,
|
||||||
|
count = item.count
|
||||||
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
parallel.waitForAll(table.unpack(fns))
|
|
||||||
|
|
||||||
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||||
type = "inventory_update",
|
type = "inventory_update",
|
||||||
|
|||||||
Reference in New Issue
Block a user