gur111

remotectl

Jun 13th, 2025 (edited)
1,140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.18 KB | None | 0 0
  1. -- pastebin ID: kfHc79KV
  2. -- URL: https://pastebin.com/kfHc79KV
  3.  
  4.  
  5. -- Mainframe discovery
  6. local mainframeId = nil
  7.  
  8. -- Function to open a modem on any side
  9. local function openModem()
  10.   local sides = {"left", "right", "top", "bottom", "front", "back"}
  11.   for _, side in ipairs(sides) do
  12.     if peripheral.getType(side) == "modem" then
  13.       rednet.open(side)
  14.       return true
  15.     end
  16.   end
  17.   return false
  18. end
  19.  
  20. -- Function to discover mainframe ID via broadcast
  21. local function discoverMainframe()
  22.   if mainframeId then return mainframeId end
  23.  
  24.   if not openModem() then
  25.     print("No modem found. Please attach a modem to the computer.")
  26.     return nil
  27.   end
  28.  
  29.   print("Broadcasting to discover mainframe...")
  30.   rednet.broadcast("discover_mainframe")
  31.  
  32.   -- Wait for responses for up to 3 seconds
  33.   local timeout = os.clock() + 3
  34.   while os.clock() < timeout do
  35.     local senderId, message = rednet.receive(0.1)
  36.     if senderId and message == "mainframe_here" then
  37.       mainframeId = senderId
  38.       print("Discovered mainframe at ID: " .. mainframeId)
  39.       rednet.close()
  40.       return mainframeId
  41.     end
  42.   end
  43.  
  44.   print("No mainframe found via broadcast")
  45.   rednet.close()
  46.   return nil
  47. end
  48.  
  49. -- CLI function to send commands to the turtle
  50. local function turtleCLI(turtleId)
  51.   if not openModem() then
  52.     print("No modem found. Please attach a modem to the computer.")
  53.     return
  54.   end
  55.  
  56.   print("Connected to turtle " .. turtleId .. ". Type 'exit' to quit.")
  57.  
  58.   while true do
  59.     io.write("> ")
  60.     local command = read()
  61.  
  62.     if command == "exit" then
  63.       break
  64.     end
  65.  
  66.     rednet.send(turtleId, command)
  67.     local senderId, response = rednet.receive(5)  -- Wait for up to 5 seconds for a response
  68.  
  69.     if senderId == turtleId then
  70.       print("Response: " .. response)
  71.     else
  72.       print("No response from turtle " .. turtleId)
  73.     end
  74.   end
  75.  
  76.   rednet.close()
  77. end
  78.  
  79. -- Function to get turtle locations from mainframe
  80. local function getLocations()
  81.   local mainframeId = discoverMainframe()
  82.   if not mainframeId then return end
  83.  
  84.   if not openModem() then
  85.     print("No modem found. Please attach a modem to the computer.")
  86.     return
  87.   end
  88.  
  89.   print("Requesting turtle locations from mainframe " .. mainframeId .. "...")
  90.  
  91.   rednet.send(mainframeId, "get_locations")
  92.   local senderId, response = rednet.receive(5)  -- Wait for up to 5 seconds for a response
  93.  
  94.   if senderId == mainframeId then
  95.     if type(response) == "table" and next(response) then
  96.       print("Turtle Locations:")
  97.       print("================")
  98.       for turtleId, location in pairs(response) do
  99.         print("Turtle " .. turtleId .. ": (" .. location.x .. ", " .. location.y .. ", " .. location.z .. ")")
  100.       end
  101.     else
  102.       print("No turtle location data available")
  103.     end
  104.   else
  105.     print("No response from mainframe " .. mainframeId)
  106.   end
  107.  
  108.   rednet.close()
  109. end
  110.  
  111. -- Function to get specific turtle location from mainframe
  112. local function getLocation(targetTurtleId)
  113.   local mainframeId = discoverMainframe()
  114.   if not mainframeId then return end
  115.  
  116.   if not openModem() then
  117.     print("No modem found. Please attach a modem to the computer.")
  118.     return
  119.   end
  120.  
  121.   print("Requesting location for turtle " .. targetTurtleId .. " from mainframe " .. mainframeId .. "...")
  122.  
  123.   rednet.send(mainframeId, "get_location " .. targetTurtleId)
  124.   local senderId, response = rednet.receive(5)  -- Wait for up to 5 seconds for a response
  125.  
  126.   if senderId == mainframeId then
  127.     if type(response) == "table" and response.x then
  128.       print("Turtle " .. targetTurtleId .. " location: (" .. response.x .. ", " .. response.y .. ", " .. response.z .. ")")
  129.     else
  130.       print("Response: " .. tostring(response))
  131.     end
  132.   else
  133.     print("No response from mainframe " .. mainframeId)
  134.   end
  135.  
  136.   rednet.close()
  137. end
  138.  
  139. -- Function to get all precious block reports from mainframe
  140. local function getPreciousReports()
  141.   local mainframeId = discoverMainframe()
  142.   if not mainframeId then return end
  143.  
  144.   if not openModem() then
  145.     print("No modem found. Please attach a modem to the computer.")
  146.     return
  147.   end
  148.  
  149.   print("Requesting precious block reports from mainframe " .. mainframeId .. "...")
  150.  
  151.   rednet.send(mainframeId, "get_precious")
  152.   local senderId, response = rednet.receive(5)  -- Wait for up to 5 seconds for a response
  153.  
  154.   if senderId == mainframeId then
  155.     if type(response) == "table" and next(response) then
  156.       print("Precious Block Reports:")
  157.       print("======================")
  158.       for turtleId, reports in pairs(response) do
  159.         print("Turtle " .. turtleId:gsub("turtle_", "") .. " (" .. #reports .. " reports):")
  160.         for i, report in ipairs(reports) do
  161.           print("  " .. i .. ". " .. report)
  162.         end
  163.         print()
  164.       end
  165.     else
  166.       print("No precious block reports available")
  167.     end
  168.   else
  169.     print("No response from mainframe " .. mainframeId)
  170.   end
  171.  
  172.   rednet.close()
  173. end
  174.  
  175. -- Function to get precious block reports for specific turtle
  176. local function getPreciousReportsForTurtle(targetTurtleId)
  177.   local mainframeId = discoverMainframe()
  178.   if not mainframeId then return end
  179.  
  180.   if not openModem() then
  181.     print("No modem found. Please attach a modem to the computer.")
  182.     return
  183.   end
  184.  
  185.   print("Requesting precious block reports for turtle " .. targetTurtleId .. " from mainframe " .. mainframeId .. "...")
  186.  
  187.   rednet.send(mainframeId, "get_precious " .. targetTurtleId)
  188.   local senderId, response = rednet.receive(5)  -- Wait for up to 5 seconds for a response
  189.  
  190.   if senderId == mainframeId then
  191.     if type(response) == "table" and #response > 0 then
  192.       print("Precious Block Reports for Turtle " .. targetTurtleId .. ":")
  193.       print("================================================")
  194.       for i, report in ipairs(response) do
  195.         print(i .. ". " .. report)
  196.       end
  197.     else
  198.       print("Response: " .. tostring(response))
  199.     end
  200.   else
  201.     print("No response from mainframe " .. mainframeId)
  202.   end
  203.  
  204.   rednet.close()
  205. end
  206.  
  207. -- Function to scrub precious block reports for specific turtle
  208. local function scrubPreciousReports(targetTurtleId)
  209.   local mainframeId = discoverMainframe()
  210.   if not mainframeId then return end
  211.  
  212.   if not openModem() then
  213.     print("No modem found. Please attach a modem to the computer.")
  214.     return
  215.   end
  216.  
  217.   print("Scrubbing precious block reports for turtle " .. targetTurtleId .. " from mainframe " .. mainframeId .. "...")
  218.  
  219.   rednet.send(mainframeId, "scrub_precious " .. targetTurtleId)
  220.   local senderId, response = rednet.receive(5)  -- Wait for up to 5 seconds for a response
  221.  
  222.   if senderId == mainframeId then
  223.     print("Response: " .. tostring(response))
  224.   else
  225.     print("No response from mainframe " .. mainframeId)
  226.   end
  227.  
  228.   rednet.close()
  229. end
  230.  
  231. -- Main function to start the CLI
  232. local args = { ... }
  233. if #args < 1 then
  234.   print("Usage:")
  235.   print("  remote <turtle-id>              - Connect to turtle")
  236.   print("  remote locations                - Get all turtle locations")
  237.   print("  remote location <turtle-id>     - Get specific turtle location")
  238.   print("  remote precious                 - Get all precious block reports")
  239.   print("  remote precious <turtle-id>     - Get precious reports for specific turtle")
  240.   print("  remote scrub <turtle-id>        - Clear precious reports for specific turtle")
  241. elseif args[1] == "locations" then
  242.   getLocations()
  243. elseif args[1] == "location" and #args >= 2 then
  244.   local targetTurtleId = tonumber(args[2])
  245.   if targetTurtleId then
  246.     getLocation(targetTurtleId)
  247.   else
  248.     print("Invalid turtle ID.")
  249.   end
  250. elseif args[1] == "precious" then
  251.   if #args >= 2 then
  252.     local targetTurtleId = tonumber(args[2])
  253.     if targetTurtleId then
  254.       getPreciousReportsForTurtle(targetTurtleId)
  255.     else
  256.       print("Invalid turtle ID.")
  257.     end
  258.   else
  259.     getPreciousReports()
  260.   end
  261. elseif args[1] == "scrub" and #args >= 2 then
  262.   local targetTurtleId = tonumber(args[2])
  263.   if targetTurtleId then
  264.     scrubPreciousReports(targetTurtleId)
  265.   else
  266.     print("Invalid turtle ID.")
  267.   end
  268. else
  269.   local turtleId = tonumber(args[1])
  270.   if turtleId then
  271.     turtleCLI(turtleId)
  272.   else
  273.     print("Invalid turtle ID.")
  274.   end
  275. end
  276.  
Advertisement