feat: Add real-time inventory and peripheral event handling for Turtle
This commit is contained in:
60
turtle.lua
60
turtle.lua
@@ -639,6 +639,66 @@ parallel.waitForAny(
|
||||
end
|
||||
end,
|
||||
|
||||
function()
|
||||
-- Inventory change events (real-time)
|
||||
while true do
|
||||
os.pullEvent("turtle_inventory")
|
||||
local inventory = {}
|
||||
local fns = {}
|
||||
for i = 1, 16 do
|
||||
fns[i] = function()
|
||||
local item = turtle.getItemDetail(i, true)
|
||||
if item then
|
||||
inventory[tostring(i)] = item
|
||||
end
|
||||
end
|
||||
end
|
||||
parallel.waitForAll(table.unpack(fns))
|
||||
|
||||
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||
type = "inventory_update",
|
||||
turtleID = os.getComputerID(),
|
||||
inventory = inventory,
|
||||
timestamp = os.epoch("utc")
|
||||
})
|
||||
end
|
||||
end,
|
||||
|
||||
function()
|
||||
-- Peripheral attached events (real-time)
|
||||
while true do
|
||||
os.pullEvent("peripheral")
|
||||
sleep(0.1) -- Small delay to let CC register
|
||||
local peripherals = {}
|
||||
local names = peripheral.getNames()
|
||||
for _, name in ipairs(names) do
|
||||
peripherals[name] = {types = {peripheral.getType(name)}}
|
||||
end
|
||||
|
||||
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||
type = "peripheral_attached",
|
||||
turtleID = os.getComputerID(),
|
||||
peripherals = peripherals,
|
||||
timestamp = os.epoch("utc")
|
||||
})
|
||||
end
|
||||
end,
|
||||
|
||||
function()
|
||||
-- Peripheral detached events (real-time)
|
||||
while true do
|
||||
local _, side = os.pullEvent("peripheral_detach")
|
||||
if not peripheral.isPresent(side) then
|
||||
modem.transmit(CHANNEL_SEND, CHANNEL_RECEIVE, {
|
||||
type = "peripheral_detached",
|
||||
turtleID = os.getComputerID(),
|
||||
side = side,
|
||||
timestamp = os.epoch("utc")
|
||||
})
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
function()
|
||||
-- Local autonomous fallback
|
||||
while true do
|
||||
|
||||
Reference in New Issue
Block a user