Advertisement
ElijahCrafter

Untitled

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