minecraft-server/Server/mods/OCSLua/lua/apis/sensor

45 lines
935 B
Plaintext
Raw Normal View History

2020-03-30 18:33:48 +02:00
local function waitForResponse( _id )
while true do
local event = {os.pullEvent()}
if event[2] == _id then
if event[1] == "ocs_success" then
return event[3]
elseif event[1] == "ocs_error" then
error(event[3],2)
end
end
end
end
function wrap(side)
local wrappedTable = {}
if peripheral.getType(side) == "sensor" then
local periph = peripheral.wrap(side)
for k,v in pairs(periph) do
if type(k) == "string" and type(v) == "function" then
wrappedTable[k] = function(...)
local id = periph[k](...)
if id == -1 then
return false
end
return waitForResponse(id)
end
end
end
return wrappedTable
else
return nil, "not a sensor"
end
end
function call(side, ...)
if peripheral.getType(side) == "sensor" then
local id = peripheral.call(side, ...)
if id == -1 then
return false
end
return waitForResponse(id)
else
return nil, "not a sensor"
end
end