paramus

ChunkLoad Receiver

Jul 2nd, 2026 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | Gaming | 0 0
  1. -- Description: This will receive all the messages sent over from the cunkloader detectors
  2. local modem = peripheral.find("modem", rednet.open)
  3. local monitor = peripheral.find("monitor")
  4.  
  5. local nodes = {}
  6.  
  7. function Draw()
  8.     while (true) do
  9.        
  10.         term.clear()
  11.  
  12.         for i, node in ipairs(nodes) do
  13.             term.setCursorPos(1, i)
  14.             print(node.name .. "(" .. node.x .. "/" .. node.y .. "/" .. node.z .. ") " .. node.lastPing .. "s")
  15.         end
  16.        
  17.         sleep(1)
  18.     end
  19. end
  20.  
  21. function checkNodes()
  22.     while (true) do
  23.    
  24.         for i, node in ipairs(nodes) do
  25.             if (node.lastPing > 10) then
  26.                 table.remove(nodes, i)
  27.                 break
  28.             else
  29.                 node.lastPing = node.lastPing + 1
  30.             end
  31.         end
  32.    
  33.         sleep(1)
  34.     end
  35. end
  36.  
  37. function receivePing()
  38.     while (true) do
  39.        
  40.         local event, sender, message, protocol = os.pullEvent("rednet_message")
  41.         local Data = textutils.unserialiseJSON(tostring(message))
  42.        
  43.         -- Check if the node already exists and update last ping time
  44.         local exists = false
  45.         for i, node in ipairs(nodes) do
  46.             if (node.name == Data.name) then
  47.                 node.lastPing = 0
  48.                 exists = true
  49.                 break
  50.             end
  51.         end
  52.        
  53.         -- Create node if non existent
  54.         if (exists == false) then
  55.             Data.lastPing = 0
  56.             table.insert(nodes, Data)
  57.         end
  58.        
  59.     end
  60. end
  61.  
  62. -- It there is a monitor use it
  63. if (monitor ~= nil) then
  64.     term.redirect(monitor)
  65. end
  66.  
  67. -- Main loops
  68. parallel.waitForAny(
  69.     function()
  70.         Draw()
  71.     end,
  72.     function()
  73.         checkNodes()
  74.     end,
  75.     function()
  76.         receivePing()
  77.     end
  78. )
Advertisement
Add Comment
Please, Sign In to add comment