Nezn

Rednet Chat [CC]

Jul 10th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. --zdarova
  2. connection = false
  3. ID = 0
  4. myMessage = ""
  5.  
  6. if not term.isColor() then
  7.     print("Need advanced computer!")
  8.     return
  9. end
  10.  
  11. sides = {"front","back","left","right","top","bottom"}
  12. for i=1,6 do
  13.     if peripheral.getType(sides[i]) == "modem" then
  14.                 rednet.open(sides[i])
  15.                 break
  16.         end
  17. end
  18. if not rednet.isOpen() then
  19.     print("Need wireless modem!")
  20.     return
  21. end
  22.  
  23. rednet.broadcast("zdarova", "ChatProto")
  24.  
  25. term.clear()
  26. term.setCursorPos(1,1)
  27. print("Searching...")
  28. term.setBackgroundColor(colors.red)
  29. term.setCursorPos(1,6)
  30. term.write("Exit")
  31. term.setBackgroundColor(colors.black)
  32. term.setCursorPos(1,4)
  33. while true do
  34.     event, arg1, arg2, arg3 = os.pullEvent()
  35.     if event == "rednet_message" and arg3 == "ChatProto" then
  36.         if not connection and arg2 == "zdarova" then
  37.             connection = true
  38.             ID = arg1
  39.             term.clearLine()
  40.             print("Connection created with "..ID.."                   ")
  41.             term.setCursorPos(1,5)
  42.             term.clearLine()
  43.             term.setCursorPos(1,1)
  44.             print("You:        ")
  45.             print("> ")
  46.             rednet.send(ID, "connected", "ChatProto")
  47.         elseif not connection and arg2 == "connected" then
  48.             connection = true
  49.             ID = arg1
  50.             term.clearLine()
  51.             print("Connection created with "..ID.."                   ")
  52.             term.setCursorPos(1,1)
  53.             print("You:        ")
  54.             print("> ")
  55.         elseif connection and arg2 == "disconnected" and arg1 == ID then
  56.             term.clear()
  57.             term.setCursorPos(1,1)
  58.             print("Searching...")
  59.             term.setBackgroundColor(colors.red)
  60.             term.setCursorPos(1,6)
  61.             term.write("Exit")
  62.             term.setCursorPos(1,4)
  63.             term.setBackgroundColor(colors.black)
  64.             term.setTextColor(colors.red)
  65.             write("Your chat partner has disconnected      ")
  66.             term.setTextColor(colors.white)
  67.             connection = false
  68.             term.setCursorPos(1,4)
  69.         elseif connection and arg1 == ID then
  70.             term.setCursorPos(1,2)
  71.             term.clearLine()
  72.             write("> "..arg2)
  73.         end
  74.     elseif event == "key" then
  75.         if connection and arg1 == 28 and myMessage ~= "" then
  76.             rednet.send(ID, myMessage, "ChatProto")
  77.             term.setCursorPos(6,1)
  78.             term.setTextColor(colors.gray)
  79.             write(myMessage)
  80.             term.setTextColor(colors.white)
  81.             myMessage = ""
  82.         elseif arg1 == 14 then
  83.             myMessage = string.sub(myMessage,1,string.len(myMessage)-1)
  84.             term.setCursorPos(1,1)
  85.             term.clearLine()
  86.             write("You: "..myMessage)
  87.         end
  88.     elseif event == "char" then
  89.         myMessage = myMessage..arg1
  90.         term.setCursorPos(1,1)
  91.         term.clearLine()
  92.         write("You: "..myMessage)
  93.     elseif event == "mouse_click" and arg2 < 5 and arg3 == 6 then
  94.         rednet.send(ID, "disconnected", "ChatProto")
  95.         term.clear()
  96.         term.setCursorPos(1,1)
  97.         break
  98.     end
  99. end
Advertisement
Add Comment
Please, Sign In to add comment