Advertisement
ElijahCrafter

Untitled

Feb 21st, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. local modemLocations = {}
  3.  
  4. -- Initialize modem and open Rednet
  5. local function initializeModem()
  6.     for _, side in ipairs(rs.getSides()) do
  7.         if peripheral.getType(side) == "modem" then
  8.             rednet.open(side)
  9.             print("Rednet opened on side:", side)
  10.             return true
  11.         end
  12.     end
  13.     print("No modem found.")
  14.     return false
  15. end
  16.  
  17. -- Function to log the location of the current computer
  18. local function logComputerLocation()
  19.     local computerID = os.computerID()
  20.     local computerLabel = os.getComputerLabel() or "Unnamed"
  21.     modemLocations[computerID] = {label = computerLabel, location = os.getComputerLabel()}
  22.     print("Logged computer ID:", computerID, "with label:", computerLabel)
  23. end
  24.  
  25. -- Function to send the computer's ID to the main computer
  26. local function sendComputerID()
  27.     local mainComputerID = 77 -- Replace with the ID of the main computer
  28.     rednet.send(mainComputerID, os.computerID())
  29.     print("Sent computer ID:", os.computerID(), "to main computer ID:", mainComputerID)
  30. end
  31.  
  32. -- Main function to handle receiving and executing commands
  33. local function main()
  34.     -- Initialize modem and open Rednet
  35.     if not initializeModem() then
  36.         return  -- Exit if no modem is found
  37.     end
  38.  
  39.     -- Log the location of the current computer
  40.     logComputerLocation()
  41.  
  42.     -- Send the computer's ID to the main computer
  43.     sendComputerID()
  44.  
  45.     -- Main loop to listen for incoming commands
  46.     while true do
  47.         local senderID, message = rednet.receive()
  48.  
  49.         -- Check if the message is intended for this computer
  50.         if type(message) == "string" then
  51.             print("Received command:", message)
  52.             -- Execute the received command
  53.             shell.run(message)
  54.         elseif message == "screen_request" then
  55.             -- Capture the screen and send it back to the requester
  56.             local screen = {}
  57.             for y = 1, 16 do
  58.                 screen[y] = ""
  59.                 for x = 1, 51 do
  60.                     screen[y] = screen[y] .. (term.getTextColor(x, y) * 16 + term.getBackgroundColor(x, y)) % 16
  61.                 end
  62.             end
  63.             rednet.send(senderID, screen)
  64.         end
  65.     end
  66. end
  67.  
  68. -- Run the main function
  69. main()
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement