1ng0

Modem-API

Dec 9th, 2015
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.07 KB | None | 0 0
  1. function getModem()
  2.     local side = nil
  3.     local sides = peripheral.getNames()
  4.    
  5.     local index = 1
  6.     while sides[index]~= nil do
  7.         if peripheral.getType(sides[index]) == "modem" then
  8.             side = sides[index]
  9.             do break end
  10.         end
  11.         index = index + 1
  12.     end
  13.    
  14.     return side
  15. end
  16.  
  17. function getRednetHosts(protocol)
  18.     local hosts = nil
  19.     if getModem() ~= nil then
  20.         rednet.open(getModem())
  21.         local hostsID = rednet.lookup(protocol)
  22.         if hostsID ~= nil then
  23.             for index = 1, hostsID[index] ~= nil, 1 do
  24.                 if hostsID[index] ~= os.getComputerID() then
  25.                     rednet.send(hostsID[index], "name", protocol)
  26.                     local result = {rednet.receive(protocol)}
  27.                     hosts[index] = {result[1], result[2]}
  28.                 end
  29.             end
  30.         end
  31.     end
  32.     return hosts
  33. end
  34.  
  35. function regeisterComputer(protocol, newName)
  36.     os.setComputerLabel(newName)
  37.     local hosts = getRednetHosts()
  38.     if hosts ~= nil then
  39.         for index = 1, hosts[index] ~= nil, 1 do
  40.             if string.find(hosts[index][2], newName) then
  41.                 local number = string.sub(hosts[index][2], string.len(newName))
  42.                 print(number)
  43.             end
  44.         end
  45.     end
  46. end
Add Comment
Please, Sign In to add comment