minecraft-cc-tools/libs/rs.lua
2024-05-28 01:05:40 +02:00

68 lines
1.4 KiB
Lua

local rs = {}
rs.listenInput = function(side, onChangeCallback)
local state = redstone.getInput(side)
while true do
os.pullEvent('redstone')
local newState = redstone.getInput(side)
if state ~= newState then
state = newState
onChangeCallback(newState)
end
end
end
rs.createBundledOutput = function(side, initialColorState)
local bundledOutput = {}
local _colorState = initialColorState or 0
local function getState()
return _colorState
end
local function setState(newColorState)
if newColorState == _colorState then
return false
end
_colorState = newColorState
redstone.setBundledOutput(side, newColorState)
return true
end
bundledOutput.getStateColor = getState
bundledOutput.setColorState = setState
bundledOutput.test = function(color)
return colors.test(getState(), color)
end
bundledOutput.setOn = function(color)
local colorState = getState()
local newColorState = colors.combine(colorState, color)
return setState(newColorState)
end
bundledOutput.setOff = function(color)
local colorState = getState()
local newColorState = colors.subtract(colorState, color)
return setState(newColorState)
end
bundledOutput.toggle = function(color)
if colors.test(getState(), color) then
return bundledOutput.setOff(color)
end
return bundledOutput.setOn(color)
end
return bundledOutput
end
return rs