Guest User

Untitled

a guest
Oct 29th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 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.  while not quit do
  30.   GetUsers()
  31.   local id, msg = rednet.receive()
  32.   if msg == "PING" then
  33.     rednet.send(id, CurrentUsername)
  34.   elseif msg == "LOGIN" then
  35.     GetUsers()
  36.   elseif msg == "QUIT" then
  37.     EraseLine()
  38.     UID[id+1] = nil
  39.   else
  40.     EraseLine()
  41.     print(msg)
  42.   end
  43.   end
  44. end
  45.  
  46. function Send()
  47.  while true do
  48.   write(CurrentUsername.." > ")
  49.   local iscommand = false
  50.   local input = read()
  51.   for i=1,#command do
  52.     if input == command[i] then
  53.       if i == 1 then
  54.         iscommand = true
  55.         rednet.broadcast("QUIT")
  56.         rednet.broadcast("* * "..CurrentUsername.." has left RedNet.")
  57.         term.clear()
  58.         term.setCursorPos(1,1)
  59.         rednet.close("top")
  60.         quit = true
  61.       elseif i == 2 then
  62.         iscommand = true
  63.         write("Send message to: ")
  64.         local whisper = read()
  65.         write("Message: ")
  66.         local msg = read()
  67.         rednet.send(tonumber(whisper), CurrentUsername.." whispers: "..msg, true)
  68.       elseif i == 3 then
  69.         iscommand = true
  70.         for n=1, #UID do
  71.           write(UID[n].." ")
  72.         end
  73.         write("\n")
  74.       end
  75.     end
  76.   end
  77.   if iscommand == false then
  78.     rednet.broadcast("<"..CurrentUsername.."> "..input)
  79.   end
  80.   end
  81. end
  82.  
  83. print("-------------------------")
  84. print("Welcome to RedNet, "..CurrentUsername.."!")
  85. shell.run("realtime")
  86. print("Current Realtime is "..Time)
  87. print("Current Minecraft time is "..os.time())
  88. GetUsers()
  89. print("Current users online: ")
  90. for key,value in pairs(UID) do
  91.   write(value.." ")
  92. end
  93. write("\n")
  94. print("-------------------------")
  95. rednet.broadcast("LOGIN")
  96. rednet.broadcast("* * "..CurrentUsername.." has joined RedNet.")
  97.  
  98.  
  99. parallel.waitForAny(Receive, Send)
Advertisement
Add Comment
Please, Sign In to add comment