Advertisement
Alakazard12

AltProto_Spawner

Mar 12th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.69 KB | None | 0 0
  1. local monitor = peripheral.find("monitor")
  2. local rside = "back"
  3.  
  4. local w, h
  5.  
  6. if monitor then
  7.     monitor.setTextScale(0.5)
  8.     w, h = monitor.getSize()
  9. end
  10.  
  11. local function CenterPrint(text, y)
  12.     monitor.setCursorPos((w - #text) / 2 + 1, y)
  13.     monitor.write(text)
  14. end
  15.  
  16. local atPos = math.floor(h / 2)
  17. local itPos = math.floor(h / 2) + 1
  18.  
  19. function SetActive(active)
  20.     local file = fs.open("altproto-spawnerstate", "w")
  21.     file.write(tostring(not not active))
  22.     file.close()
  23.     redstone.setOutput(rside, not active)
  24.  
  25.     if monitor then
  26.         monitor.setTextColor(colors.white)
  27.         if active then
  28.             monitor.setBackgroundColor(colors.green)
  29.             monitor.clear()
  30.             CenterPrint("Active", atPos)
  31.         else
  32.             monitor.setBackgroundColor(colors.red)
  33.             monitor.clear()
  34.             CenterPrint("Inactive", atPos)
  35.         end
  36.  
  37.         monitor.setTextColor(colors.lightGray)
  38.         CenterPrint("Right click", itPos)
  39.         CenterPrint("to toggle", itPos + 1)
  40.     end
  41. end
  42.  
  43. local active = false
  44.  
  45.  
  46. local file = fs.open("altproto-spawnerstate", "r")
  47. if file then
  48.     active = file.readAll() == "true"
  49.     file.close()
  50. end
  51.  
  52.  
  53. OnMessage(function(id, recipient, sender, data)
  54.     SetActive(data)
  55. end, "SETSPAWNER")
  56.  
  57.  
  58. OnEvent(function(event, side)
  59.     if not monitor and peripheral.getType(side) == "monitor" then
  60.         monitor = peripheral.wrap(side)
  61.  
  62.         monitor.setTextScale(0.5)
  63.         w, h = monitor.getSize()
  64.         Log("Monitor attached")
  65.     end
  66. end, "peripheral")
  67.  
  68.  
  69. OnEvent(function(event, side)
  70.     if monitor and peripheral.getType(side) == "monitor" then
  71.         monitor = nil
  72.         Log("Monitor detached")
  73.     end
  74. end, "peripheral_detach")
  75.  
  76.  
  77.  
  78. SetActive(active)
  79.  
  80.  
  81.  
  82. OnEvent(function(event, side, x, y)
  83.     active = not active
  84.     SetActive(active)
  85. end, "monitor_touch")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement