Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft Clock (NOT MINE)

Aug 16th, 2024
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | Source Code | 0 0
  1. -- Clock for display on a monitor
  2. -- Author: CecilKilmer
  3. -- Version: 2.0
  4. -- Date: 2024/08/16
  5.  
  6. -- Min width required (IE: 10:00 PM + Date)
  7. minWidthReq = 10
  8. minHeightReq = 4
  9.  
  10. -- Figure out where our monitor is
  11. monitorSide = nil
  12. monitor = nil
  13.  
  14. if peripheral.getType("left") == "monitor" then
  15. monitorSide = "left"
  16. elseif peripheral.getType("right") == "monitor" then
  17. monitorSide = "right"
  18. elseif peripheral.getType("top") == "monitor" then
  19. monitorSide = "top"
  20. elseif peripheral.getType("bottom") == "monitor" then
  21. monitorSide = "bottom"
  22. elseif peripheral.getType("front") == "monitor" then
  23. monitorSide = "front"
  24. elseif peripheral.getType("back") == "monitor" then
  25. monitorSide = "back"
  26. end
  27.  
  28. screenWidth = 0
  29. screenHeight = 0
  30.  
  31. -- If we have one, redirect term to it
  32. if monitorSide ~= nil then
  33. monitor = peripheral.wrap(monitorSide)
  34. term.redirect(monitor)
  35.  
  36. local scale = 5.5
  37.  
  38. repeat
  39. scale = scale - 0.5
  40. monitor.setTextScale(scale)
  41. screenWidth, screenHeight = term.getSize()
  42. until screenWidth > minWidthReq and screenHeight > minHeightReq
  43. end
  44.  
  45. -- Determine our screen size
  46. screenWidth, screenHeight = term.getSize()
  47. yPos = screenHeight - minHeightReq
  48. yPos = math.floor(yPos / 2)
  49.  
  50. -- Function to convert Minecraft days to a calendar date
  51. function convertToCalendar(day)
  52. local year = math.floor(day / 360) + 1 -- Each year has 360 days (12 months * 30 days)
  53. local month = math.floor((day % 360) / 30) + 1 -- 30 days per month
  54. local dayOfMonth = (day % 30) + 1
  55. return dayOfMonth, month, year
  56. end
  57.  
  58. -- Main loop
  59. while true do
  60. term.clear()
  61.  
  62. -- Get current Minecraft time and day
  63. local time = os.time()
  64. local day = os.day()
  65.  
  66. -- Format the time and date
  67. time = textutils.formatTime(time, false)
  68. local dayOfMonth, month, year = convertToCalendar(day)
  69. local dateString = string.format("%02d/%02d/%04d", dayOfMonth, month, year)
  70.  
  71. -- Calculate positions
  72. local xPosTime = screenWidth - string.len(time)
  73. xPosTime = math.ceil(xPosTime / 2)
  74. local xPosDate = screenWidth - string.len(dateString)
  75. xPosDate = math.ceil(xPosDate / 2)
  76.  
  77. -- Display time and date
  78. term.setCursorPos(xPosTime + 1, yPos + 1)
  79. print(time)
  80. term.setCursorPos(xPosDate + 1, yPos + 3)
  81. print(dateString)
  82.  
  83. sleep(.1)
  84. end
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement