Advertisement
PaymentOption

Lights

Sep 25th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.51 KB | None | 0 0
  1. -- Turns and keeps the lights on the dock on from 12:00 PM to 6:00 AM
  2.  
  3. nLightID = 1764
  4. bOn = false
  5. tLightsTimer = os.startTimer(5)
  6.  
  7. function sendCommand(sCommand)
  8.     rednet.open("top")
  9.    
  10.     if sCommand == "on" then
  11.         rednet.send(nLightID, "lights_on")
  12.     elseif sCommand == "off" then
  13.         rednet.send(nLightID, "lights_off")
  14.     end
  15.    
  16.     rednet.close("top")
  17. end
  18.  
  19. function runLights()
  20.     while true do
  21.         local sEvent, param1 = os.pullEvent()
  22.        
  23.         if sEvent == "timer" and param1 == tLightsTimer then
  24.             local sTime = textutils.formatTime(os.time(), false) -- The current time in the Minecraft world.
  25.             local sTime_Period = sTime:sub(sTime:len() - 1, sTime:len()) -- AM or PM of the world time.
  26.             local nTime_Hour = tonumber(sTime:sub(1, sTime:find(":", 1) - 1)) -- The current hour of the day.
  27.  
  28.             if bOn and nTimeHour == 12 and sTime_Period == "PM" then
  29.                 sendCommand("off")
  30.                 bOn = false
  31.             -- If the time is between 8:00 PM to 6:AM then the lights should be on.
  32.             elseif ((nTime_Hour < 6 and sTime_Period == "AM") or (nTime_Hour >= 6 and sTime_Period == "PM")) and (nTime_Hour ~= 12 and sTime_Period == "PM") and not bOn then
  33.                 sendCommand("on")
  34.                 bOn = true
  35.             elseif ((nTime_Hour >= 6 and sTime_Period == "AM") or (nTime_Hour < 6 and sTime_Period == "PM")) and bOn then
  36.                 sendCommand("off")
  37.                 bOn = false
  38.             end
  39.  
  40.             tLightsTimer = os.startTimer(5)
  41.         end
  42.     end
  43. end
  44.  
  45. function runShell()
  46.     while true do
  47.         shell.run("shell")
  48.     end
  49. end
  50.  
  51. while true do
  52.     parallel.waitForAny(runShell, runLights)
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement