fi5hii

Untitled

May 11th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 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 ON text
  21. local onText = "ON"
  22. monitor.setTextColor(colors.green) -- Set text color to green
  23. monitor.write(onText)
  24.  
  25. -- Display OFF text
  26. local offText = " OFF"
  27. monitor.setTextColor(colors.red) -- Set text color to red
  28. monitor.write(offText)
  29.  
  30. -- Reset text color
  31. monitor.setTextColor(colors.white)
  32.  
  33. -- Return the hitbox coordinates for ON and OFF texts
  34. return #onText, #offText
  35. end
  36.  
  37. -- Function to handle monitor touch events
  38. function handleTouch(x, y, onTextWidth, offTextWidth)
  39. -- Check if touch is within the clickable area for ON
  40. if y == 3 and x >= 1 and x <= onTextWidth then
  41. -- Send a 5-second redstone pulse on the left side
  42. redstone.setOutput(redstoneSide, true)
  43. sleep(5)
  44. redstone.setOutput(redstoneSide, false)
  45. end
  46.  
  47. -- Check if touch is within the clickable area for OFF
  48. if y == 3 and x > onTextWidth and x <= onTextWidth + offTextWidth then
  49. -- Send a 5-second redstone pulse on the top side
  50. redstone.setOutput(redstoneTop, true)
  51. sleep(5)
  52. redstone.setOutput(redstoneTop, false)
  53. end
  54. end
  55.  
  56. -- Main function
  57. function main()
  58. local onTextWidth, offTextWidth = displayLine()
  59.  
  60. while true do
  61. -- Wait for touch event
  62. local event, side, x, y = os.pullEvent("monitor_touch")
  63. handleTouch(x, y, onTextWidth, offTextWidth)
  64. end
  65. end
  66.  
  67. -- Run the main function
  68. main()
  69.  
Advertisement
Add Comment
Please, Sign In to add comment