fi5hii

Untitled

May 11th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. -- Load the required components
  2. local monitor = peripheral.find("monitor", function(name) return name == "monitor_2" end) -- Monitor peripheral named monitor_2
  3. local redstoneSide = "left" -- Define the side where the redstone is connected
  4. local redstoneTop = "top" -- Define the side where the redstone is connected
  5.  
  6. -- Function to display line 1 from create_target_1 on monitor_2
  7. function displayLine()
  8. -- Get the line 1 from create_target_1
  9. local line = peripheral.call("create_target_1", "getLine", 1)
  10.  
  11. -- Clear the monitor
  12. monitor.clear()
  13.  
  14. -- Set cursor position at (1,1)
  15. monitor.setCursorPos(1, 1)
  16.  
  17. -- Display the line on monitor
  18. monitor.write(line)
  19.  
  20. -- Display clickable text below the line
  21. local onText = "ON"
  22. local offText = " OFF"
  23.  
  24. -- Calculate the width of ON text
  25. local onTextWidth = #onText
  26.  
  27. -- Calculate the width of OFF text
  28. local offTextWidth = #offText
  29.  
  30. -- Calculate the starting x-coordinate for ON text
  31. local onTextX = 1
  32.  
  33. -- Calculate the starting x-coordinate for OFF text
  34. local offTextX = 33
  35.  
  36. -- Display ON text
  37. monitor.setCursorPos(onTextX, 3)
  38. monitor.setTextColor(colors.green) -- Set text color to green
  39. monitor.write(onText)
  40.  
  41. -- Display OFF text
  42. monitor.setCursorPos(offTextX, 3)
  43. monitor.setTextColor(colors.red) -- Set text color to red
  44. monitor.write(offText)
  45.  
  46. -- Reset text color
  47. monitor.setTextColor(colors.white)
  48.  
  49. -- Return the hitbox coordinates for ON and OFF texts
  50. return onTextX, onTextX + onTextWidth - 1, offTextX, offTextX + offTextWidth - 1
  51. end
  52.  
  53. -- Function to handle monitor touch events
  54. function handleTouch(x, y)
  55. -- Get the hitbox coordinates for ON and OFF texts
  56. local onTextX1, onTextX2, offTextX1, offTextX2 = displayLine()
  57.  
  58. -- Check if touch is within the clickable area for ON
  59. if y == 3 and x >= onTextX1 and x <= onTextX2 then
  60. -- Send a 5-second redstone pulse on the left side
  61. redstone.setOutput(redstoneSide, true)
  62. sleep(5)
  63. redstone.setOutput(redstoneSide, false)
  64. end
  65.  
  66. -- Check if touch is within the clickable area for OFF
  67. if y == 3 and x >= offTextX1 and x <= offTextX2 then
  68. -- Send a 5-second redstone pulse on the top side
  69. redstone.setOutput(redstoneTop, true)
  70. sleep(5)
  71. redstone.setOutput(redstoneTop, false)
  72. end
  73. end
  74.  
  75. -- Main function
  76. function main()
  77. while true do
  78. -- Wait for touch event
  79. local event, side, x, y = os.pullEvent("monitor_touch")
  80. handleTouch(x, y)
  81. end
  82. end
  83.  
  84. -- Run the main function
  85. main()
  86.  
Advertisement
Add Comment
Please, Sign In to add comment