Advertisement
BombBloke

Server Up-Time Timer (ComputerCraft)

Jun 28th, 2022
1,202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. -- Needs a monitor four blocks wide.
  2.  
  3. -- Text counts down in white until the next predicted Minecraft server reboot.
  4.  
  5. -- If the server stays up longer than predicted, text turns grey and starts counting
  6. -- upwards to indicate current uptime instead.
  7.  
  8. -- Prediction is based upon the longest uptime encountered.
  9.  
  10. local mon = peripheral.find("monitor")
  11.  
  12. local counter, maxCount = 0, 0
  13.  
  14. if fs.exists("lastTime") then
  15.     local input = io.open("lastTime", "r")
  16.     maxCount = tonumber(input:read())
  17.     input:close()
  18. end
  19.  
  20. print("Last uptime " .. (maxCount / 60) .. " minutes (" .. (maxCount / 3600) .. " hours).")
  21.  
  22. mon.setTextColour(colours.white)
  23. mon.setBackgroundColour(colours.black)
  24. mon.setTextScale(5)
  25.  
  26. while true do
  27.     if counter == maxCount then mon.setTextColour(colours.grey) end
  28.    
  29.     local thisTime
  30.    
  31.     if counter > maxCount then
  32.         if counter % 60 == 0 then
  33.             local output = io.open("lastTime", "w")
  34.             output:write(counter)
  35.             output:close()
  36.         end
  37.        
  38.         thisTime = counter
  39.     else
  40.         thisTime = maxCount - counter
  41.     end
  42.    
  43.     local hours = tostring(math.floor(thisTime / 3600))
  44.     if #hours == 1 then hours = "0" .. hours end
  45.    
  46.     thisTime = thisTime % 3600
  47.    
  48.     local minutes = tostring(math.floor(thisTime / 60))
  49.     if #minutes == 1 then minutes = "0" .. minutes end
  50.    
  51.     local seconds = tostring(thisTime % 60)
  52.     if #seconds == 1 then seconds = "0" .. seconds end
  53.    
  54.     mon.setCursorPos(1, 1)
  55.     mon.write(hours .. ":" .. minutes .. ":" .. seconds)
  56.    
  57.     sleep(1)
  58.     counter = counter + 1
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement