Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Load the required components
- local monitor = peripheral.find("monitor", function(name) return name == "monitor_2" end) -- Monitor peripheral named monitor_2
- local redstoneSide = "left" -- Define the side where the redstone is connected
- local redstoneTop = "top" -- Define the side where the redstone is connected
- -- Function to display line 1 from create_target_1 on monitor_2
- function displayLine()
- -- Get the line 1 from create_target_1
- local line = peripheral.call("create_target_1", "getLine", 1)
- -- Clear the monitor
- monitor.clear()
- -- Set cursor position at (1,1)
- monitor.setCursorPos(1, 1)
- -- Display the line on monitor
- monitor.write(line)
- -- Display clickable text below the line
- local onText = "ON"
- local offText = " OFF"
- -- Calculate the width of ON text
- local onTextWidth = #onText
- -- Calculate the width of OFF text
- local offTextWidth = #offText
- -- Calculate the starting x-coordinate for ON text
- local onTextX = 1
- -- Calculate the starting x-coordinate for OFF text
- local offTextX = 33
- -- Display ON text
- monitor.setCursorPos(onTextX, 3)
- monitor.setTextColor(colors.green) -- Set text color to green
- monitor.write(onText)
- -- Display OFF text
- monitor.setCursorPos(offTextX, 3)
- monitor.setTextColor(colors.red) -- Set text color to red
- monitor.write(offText)
- -- Reset text color
- monitor.setTextColor(colors.white)
- -- Return the hitbox coordinates for ON and OFF texts
- return onTextX, onTextX + onTextWidth - 1, offTextX, offTextX + offTextWidth - 1
- end
- -- Function to handle monitor touch events
- function handleTouch(x, y)
- -- Get the hitbox coordinates for ON and OFF texts
- local onTextX1, onTextX2, offTextX1, offTextX2 = displayLine()
- -- Check if touch is within the clickable area for ON
- if y == 3 and x >= onTextX1 and x <= onTextX2 then
- -- Send a 5-second redstone pulse on the left side
- redstone.setOutput(redstoneSide, true)
- sleep(5)
- redstone.setOutput(redstoneSide, false)
- end
- -- Check if touch is within the clickable area for OFF
- if y == 3 and x >= offTextX1 and x <= offTextX2 then
- -- Send a 5-second redstone pulse on the top side
- redstone.setOutput(redstoneTop, true)
- sleep(5)
- redstone.setOutput(redstoneTop, false)
- end
- end
- -- Main function
- function main()
- while true do
- -- Wait for touch event
- local event, side, x, y = os.pullEvent("monitor_touch")
- handleTouch(x, y)
- end
- end
- -- Run the main function
- main()
Advertisement
Add Comment
Please, Sign In to add comment