Advertisement
qwertyMAN_rus

Chatbox

May 29th, 2016 (edited)
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1. -- Автор: qwertyMAN
  2. -- Версия: 0.1
  3. -- Специально для computercraft.ru
  4. local event = require("event")
  5. local term = require("term")
  6. local com = require("component")
  7. local gpu = com.gpu
  8. local chat = com.chatbox
  9. local old_Foreg = gpu.getForeground()
  10. local old_Back = gpu.getBackground()
  11. local exit = true
  12. local white_list = {}
  13. local color = {
  14.     Foreg = 0xffffff
  15. }
  16. local command = {
  17.     ["clear"] = term.clear,
  18.     ["add"] = function(msg)
  19.             gpu.setForeground(0xffff00)
  20.             for i=2, #msg do
  21.                 print("User " .. msg[i] .. " added")
  22.                 white_list[#white_list+1] = msg[i]
  23.             end
  24.             gpu.setForeground(color.Foreg)
  25.         end,
  26.     ["del"] = function(msg)
  27.             gpu.setForeground(0xff0000)
  28.             if msg[2] == "all" then
  29.                 for i=#white_list, 1, -1 do
  30.                     print("User " .. white_list[i] .. " removed")
  31.                     table.remove(white_list,i)
  32.                 end
  33.                 gpu.setForeground(0xffff00)
  34.                 print("White list clear")
  35.             else
  36.                 for i=2, #msg do
  37.                     for j=1, #white_list do
  38.                         if msg[i]==white_list[j] then
  39.                             print("User " .. msg[i] .. " removed")
  40.                             table.remove(white_list,j)
  41.                             break
  42.                         end
  43.                     end
  44.                 end
  45.             end
  46.            
  47.             gpu.setForeground(color.Foreg)
  48.         end,
  49.     ["list"] = function(msg)
  50.             gpu.setForeground(0x00ff00)
  51.             print("White list:")
  52.             if #white_list==0 then
  53.                 gpu.setForeground(0xffff00)
  54.                 print("none")
  55.             else
  56.                 for i=1, #white_list do
  57.                     print(white_list[i])
  58.                 end
  59.             end
  60.             gpu.setForeground(color.Foreg)
  61.         end,
  62.     ["exit"] = function(msg)
  63.             gpu.setForeground(old_Foreg)
  64.             gpu.setBackground(old_Back)
  65.             term.clear()
  66.             print("Good buy!")
  67.             os.sleep(1)
  68.             term.clear()
  69.             exit = false
  70.         end
  71. }
  72.     local function args_word(words)
  73.     local tb = {}
  74.     for word in string.gmatch(words, "%a+") do
  75.         tb[#tb+1] = word
  76.     end
  77.     return tb
  78. end
  79. local function cmd(_,_,name,msg)
  80.     msg = args_word(msg)
  81.     if command[msg[1]] then
  82.         command[msg[1]](msg)
  83.     end
  84. end
  85. local function msg(_,_,name,msg)
  86.     if #white_list == 0 then
  87.         print(name .. " : " .. msg)
  88.     else
  89.         for i=1, #white_list do
  90.             if name == white_list[i] then
  91.                 print(name .. " : " .. msg)
  92.             end
  93.         end
  94.     end
  95. end
  96. function start()
  97.     term.clear()
  98.     event.listen("chat_command",cmd)
  99.     event.listen("chat_message",msg)
  100.     while exit do
  101.         os.sleep(3)
  102.     end
  103.     event.ignore("chat_command",cmd)
  104.     event.ignore("chat_message",msg)
  105. end
  106. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement