Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local modes = {
- ["regular"] = setmetatable({}, { __call = function( self, side ) return redstone.getInput(side) end, default_value = false }),
- ["bundled"] = setmetatable({}, { __call = function( self, side ) return redstone.getBundledInput(side) end, default_value = 0 })
- }
- local function actualUpdate( _table )
- for side, sideTable in pairs( _table ) do
- local input = _table(side)
- if sideTable.state ~= input then
- sideTable.state = input
- for _, func in pairs( sideTable.callbacks ) do
- pcall(func, side, sideTable.state)
- end
- end
- end
- end
- function update()
- for _, mode in pairs( modes ) do
- actualUpdate( mode )
- end
- end
- function waitRedstoneUpdate()
- os.pullEvent("redstone")
- update()
- end
- function addCallback(mode, func, side)
- -- Compat check
- if type(mode) == "function" then
- return addCallback("regular", func, side)
- end
- if type(func) ~= "function" then
- error("Function expected, got " .. type(func), 3)
- end
- if side then
- if type(side) ~= "string" then
- error("String expected, got " .. type(side), 4 )
- elseif modes[mode][side] == nil then
- error("Expected valid side", 4)
- end
- modes[mode][side].callbacks[#modes[mode][side].callbacks + 1] = func
- return
- end
- for _, sideTable in pairs( modes[mode] ) do
- sideTable.callbacks[#sideTable.callbacks + 1] = func
- end
- end
- function addRegularCallback(func, side)
- addCallback("regular", func, side)
- end
- function addBundledCallback(func, side)
- addCallback("bundled", func, side)
- end
- -- This is code to initialize the tables (always ran)
- do
- local function initialize( _table )
- local mt = getmetatable( _table )
- for _, side in pairs( redstone.getSides() ) do
- _table[side] = {
- ["state"] = mt.default_value,
- ["callbacks"] = {}
- }
- end
- end
- if redstone.getAnalogInput then
- modes["analog"] = setmetatable({}, { __call = function( self, side ) return redstone.getAnalogInput(side) end, default_value = 0 })
- function addAnalogCallback(func, side)
- addCallback("analog", func, side)
- end
- end
- for _, mode in pairs( modes ) do
- initialize( mode )
- actualUpdate( mode )
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment