diff --git a/turtle.lua b/turtle.lua index 78a99ca..f695ae6 100644 --- a/turtle.lua +++ b/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