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

This commit is contained in:
MayaTheShy
2026-02-20 02:15:54 -05:00
parent f38a219ad0
commit eff33dfe09

View File

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