LBPHacker

MFFS Controller for devillwithin - Revision #2

Jul 11th, 2013
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.15 KB | None | 0 0
  1. -- * configuration
  2. local usernamesToAccept = {
  3.     "yourUserame",
  4.     "otherUserame",
  5.     "LBPHacker"
  6. }
  7. local innerMonitorName = "monitor_0"
  8. local outerMonitorName = "monitor_1"
  9. local outerDetectorName = "playerDetector_0"
  10. local redstoneOutput = "bottom"
  11.  
  12. -- * calls the same methods of the two monitors
  13. local callMonitorMethod = function(...)
  14.     -- * I used peripheral.call in the end - no need of wrapping the monitors up
  15.     peripheral.call(innerMonitorName, ...)
  16.     peripheral.call(outerMonitorName, ...)
  17. end
  18.  
  19. -- * updates the redstone output and the monitor
  20. local toggleOutput = function()
  21.     local current = not rs.getOutput(redstoneOutput)
  22.     rs.setOutput(redstoneOutput, current) -- * toggles the output
  23.     callMonitorMethod("setBackgroundColor", current and colors.lime or colors.red)
  24.     callMonitorMethod("clear")
  25.     callMonitorMethod("setCursorPos", 2, 3)
  26.     callMonitorMethod("write", current and "MFFS: [ON]| OFF " or "MFFS:  ON |[OFF]")
  27. end
  28.  
  29. -- * checks for access
  30. local hasAccess = function(username)
  31.     local access = false
  32.     for ixUser = 1, #usernamesToAccept do access = usernamesToAccept[ixUser] == username or access end -- * gives access sooner or later if the username is in the table 'usernamesToAccept'
  33.     return access
  34. end
  35.  
  36. callMonitorMethod("setTextColor", colors.white)
  37. rs.setOutput(redstoneOutput, false) -- * change false to true if you want the MFFS to be off by default
  38. toggleOutput()
  39.  
  40. -- * endless loop
  41. while true do
  42.     local eventData = {os.pullEvent()} -- * puts the event data into a table
  43.     -- * every monitor sends a monitor_touch
  44.     -- * (eventData[1] == "player" and hasAccess(eventData[2])):
  45.     --        will return true if the event was player and the username can be found
  46.     --        in the list of users permitted to toggle the MFFS power - again, call for
  47.     --        hasAccess won't execute if the event wasn't player
  48.     if eventData[1] == "monitor_touch" or eventData[1] == "player" and hasAccess(eventData[2]) then
  49.         -- * nothing is on the monitor apart from the button
  50.         --   which toggles the MFFS, so no need of fancy button APIs
  51.         toggleOutput()
  52.     end
  53. end
Add Comment
Please, Sign In to add comment