From eff33dfe09218e68b63ded5b03217dd33ecf0d8f Mon Sep 17 00:00:00 2001 From: MayaTheShy Date: Fri, 20 Feb 2026 02:15:54 -0500 Subject: [PATCH] feat: Add real-time inventory and peripheral event handling for turtles --- webbridge.lua | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/webbridge.lua b/webbridge.lua index 81b020e..328b549 100644 --- a/webbridge.lua +++ b/webbridge.lua @@ -573,6 +573,81 @@ while true do addLog(" -> Sent " .. #blocks .. " blocks to server", colors.lime) end + + elseif message.type == "inventory_update" then + -- Turtle inventory changed in real-time + local turtleID = message.turtleID + addLog("Turtle #" .. turtleID .. " inventory update", colors.cyan) + + local success = pcall(function() + local response = http.post( + SERVER_URL .. "/api/turtle/event", + textutils.serializeJSON({ + turtleID = turtleID, + type = "INVENTORY_UPDATE", + message = message.inventory + }), + {["Content-Type"] = "application/json"} + ) + if response then + response.close() + end + end) + + if not success then + stats.errors = stats.errors + 1 + addLog(" -> Failed to forward inventory update", colors.red) + end + + elseif message.type == "peripheral_attached" then + -- Turtle peripheral attached in real-time + local turtleID = message.turtleID + addLog("Turtle #" .. turtleID .. " peripheral attached", colors.cyan) + + local success = pcall(function() + local response = http.post( + SERVER_URL .. "/api/turtle/event", + textutils.serializeJSON({ + turtleID = turtleID, + type = "PERIPHERAL_ATTACHED", + message = message.peripherals + }), + {["Content-Type"] = "application/json"} + ) + if response then + response.close() + end + end) + + if not success then + stats.errors = stats.errors + 1 + addLog(" -> Failed to forward peripheral attach", colors.red) + end + + elseif message.type == "peripheral_detached" then + -- Turtle peripheral detached in real-time + local turtleID = message.turtleID + addLog("Turtle #" .. turtleID .. " peripheral detached: " .. (message.side or "?"), colors.cyan) + + local success = pcall(function() + local response = http.post( + SERVER_URL .. "/api/turtle/event", + textutils.serializeJSON({ + turtleID = turtleID, + type = "PERIPHERAL_DETACHED", + message = message.side + }), + {["Content-Type"] = "application/json"} + ) + if response then + response.close() + end + end) + + if not success then + stats.errors = stats.errors + 1 + addLog(" -> Failed to forward peripheral detach", colors.red) + end end end elseif channel == POCKET_CHANNEL then