feat: Add real-time inventory and peripheral event handling for Turtle

This commit is contained in:
MayaTheShy
2026-02-20 02:15:45 -05:00
parent 0bf5590343
commit f38a219ad0

View File

@@ -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