Advertisement
MisterMeister32

[ComputerCraft] RedNetLog

Mar 24th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.83 KB | None | 0 0
  1. --RedNetLog by MisterMeister32
  2.  
  3. local function open()
  4.  
  5.   p = peripheral.getNames()
  6.   local modem = nil
  7.   local for i = 1, #p do --seaching for modems
  8.     if peripheral.getType(p[i]) == "modem" then
  9.       modem = p[i]
  10.     end
  11.   end
  12.  
  13.   if modem == nil then --opening, when found
  14.     return false
  15.   else
  16.     rednet.open(modem)
  17.     return true
  18.   end
  19.  
  20. end
  21.  
  22.  
  23. local function log() --actual logger
  24.  
  25.   while true do
  26.     local id, msg, prot = rednet.receive()
  27.     msg = textutils.serialise(msg)
  28.     print(id.."("..prot.."):"..msg)
  29.   end
  30.  
  31. end
  32.  
  33.  
  34. local function stop() --stop on keypress
  35.  
  36.   while true do
  37.     a = os.pullEvent("key")
  38.     if a == "key" then
  39.       break
  40.     end
  41.   end
  42.  
  43. end
  44.  
  45.  
  46. if open() then
  47.   print("Press any key to exit.")
  48.   parallel.waitForAny(log, stop)
  49. else
  50.   print("No modem found.")
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement