Advertisement
HappySunChild

sender

Apr 14th, 2025 (edited)
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.05 KB | None | 0 0
  1. ---@class command
  2. ---@field key string
  3. ---@field arguments rednet.transmittable[]
  4.  
  5. local COMMAND_PROTOCOL = "controller.command"
  6. local REQUEST_PROTOCOL = "controller.request"
  7. local RESPONSE_PROTOCOL = "controller.response"
  8.  
  9. local connectedTurtles = {}
  10.  
  11. local function split(str, sep)
  12.     sep = sep or "%s"
  13.  
  14.     local t = {}
  15.  
  16.     for s in string.gmatch(str, "[^" .. sep .. "]+") do
  17.         table.insert(t, s)
  18.     end
  19.  
  20.     return t
  21. end
  22.  
  23. local function receiver()
  24.     while true do
  25.         local id, message = rednet.receive(REQUEST_PROTOCOL)
  26.  
  27.         if message == "connect" then
  28.             rednet.send(id, true, RESPONSE_PROTOCOL)
  29.             table.insert(connectedTurtles, id)
  30.         end
  31.     end
  32. end
  33.  
  34. local function main()
  35.     term.clear()
  36.     term.setCursorPos(1, 1)
  37.  
  38.     peripheral.find("modem", rednet.open)
  39.  
  40.     while true do
  41.         term.setTextColor(colors.yellow)
  42.         write("$ ")
  43.  
  44.         term.setTextColor(colors.white)
  45.         local input = read()
  46.         local args = split(input)
  47.  
  48.         rednet.broadcast({ key = table.remove(args, 1), arguments = args }, COMMAND_PROTOCOL)
  49.     end
  50. end
  51.  
  52. parallel.waitForAll(main, receiver)
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement