Guest User

rednet

a guest
Oct 29th, 2012
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. rednet.open("top")
  2. term.clear()
  3. term.setCursorPos(1,1)
  4.  
  5. local command = {"-quit", "-msg", "-users"}
  6. local input = ""
  7. local quit = false
  8. CID = {0,1,2,3,4,5,6,7,8,9}
  9. UID = {}
  10. oldUID = {}
  11. UID[os.computerID()+1] = CurrentUsername
  12.  
  13. function EraseLine()
  14.   oldx, oldy = term.getCursorPos()
  15.   term.clearLine()
  16.   term.setCursorPos(1,oldy)
  17. end
  18.  
  19. function GetUsers()
  20.   for n=1,#UID do
  21.     if n-1 ~= os.computerID() then
  22.       rednet.send(n-1, "PING")
  23.       idn, UID[n], distance = rednet.receive(0.5)
  24.     end
  25.   end
  26. end
  27.  
  28. function Receive()
  29.   GetUsers()
  30.   local id, msg = rednet.receive()
  31.   if msg == "PING" then
  32.     rednet.send(id, CurrentUsername)
  33.   elseif msg == "LOGIN" then
  34.     GetUsers()
  35.   elseif msg == "QUIT" then
  36.     EraseLine()
  37.     UID[id+1] = nil
  38.   else
  39.     EraseLine()
  40.     print(msg)
  41.   end
  42. end
  43.  
  44. function Send()
  45.   write(CurrentUsername.." > ")
  46.   local iscommand = false
  47.   local input = read()
  48.   for i=1,#command do
  49.     if input == command[i] then
  50.       if i == 1 then
  51.         iscommand = true
  52.         rednet.broadcast("QUIT")
  53.         rednet.broadcast("* * "..CurrentUsername.." has left RedNet.")
  54.         term.clear()
  55.         term.setCursorPos(1,1)
  56.         rednet.close("top")
  57.         quit = true
  58.       elseif i == 2 then
  59.         iscommand = true
  60.         write("Send message to: ")
  61.         local whisper = read()
  62.         write("Message: ")
  63.         local msg = read()
  64.         rednet.send(tonumber(whisper), CurrentUsername.." whispers: "..msg, true)
  65.       elseif i == 3 then
  66.         iscommand = true
  67.         for n=1, #UID do
  68.           write(UID[n].." ")
  69.         end
  70.         write("\n")
  71.       end
  72.     end
  73.   end
  74.   if iscommand == false then
  75.     rednet.broadcast("<"..CurrentUsername.."> "..input)
  76.   end
  77. end
  78.  
  79. print("-------------------------")
  80. print("Welcome to RedNet, "..CurrentUsername.."!")
  81. shell.run("realtime")
  82. print("Current Realtime is "..Time)
  83. print("Current Minecraft time is "..os.time())
  84. GetUsers()
  85. print("Current users online: ")
  86. for key,value in pairs(UID) do
  87.   write(value.." ")
  88. end
  89. write("\n")
  90. print("-------------------------")
  91. rednet.broadcast("LOGIN")
  92. rednet.broadcast("* * "..CurrentUsername.." has joined RedNet.")
  93.  
  94. while quit == false do
  95.   parallel.waitForAny(Receive, Send)
  96. end
Advertisement
Add Comment
Please, Sign In to add comment