Advertisement
kreezxil

Advanced Chat Monitor

Sep 16th, 2013
509
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. --[[
  2. Name: Advanced Chat Box
  3. Description: Monitor chat on your server or in predefined of your chatbox.
  4. Usage: chat <chatbox side> <lines of history to keep>
  5. ]]
  6.  
  7. local chat = {}
  8. local maxX, maxY = term.getSize()
  9. local position = 1
  10.  
  11. function cls()
  12.     term.clear()
  13.     term.setCursorPos(1,1)
  14. end
  15.  
  16. function updateScrn()
  17.   cls()
  18.   for i=position,position+maxY-1 do -- -1 to prevent writing on last line
  19.       if chat[i] then
  20.           print(chat[i])
  21.       end
  22.   end
  23.   term.setCursorPos(maxX-7,maxY)
  24.   term.write("down up")
  25. end
  26.  
  27. args = {...}
  28.  
  29. if #args < 2 then
  30.     print ("Usage:")
  31.     print ("chat <side> <history>")
  32.     print()
  33.     print("Where <side> is the position of the chatbox in relation to the computer and <history> is the amount of chat lines to buffer.")
  34.     return --exit the program because the user failed
  35. end
  36.  
  37. local side,lines = args[1],tonumber(args[2])
  38.  
  39. _ = peripheral.wrap(side)
  40.  
  41. cls()
  42. while true do
  43.     -- update screen
  44.     updateScrn()
  45.    
  46.     local event, p1, p2, p3 = os.pullEvent()
  47.    
  48.     if event == "chat" then
  49.       table.insert(chat,"<".. p1 .."> " .. p2) -- add it to the chat table
  50.  
  51.       if #chat > lines then
  52.           -- limit size of history
  53.           table.remove(chat,1) -- removes the first entry
  54.       end
  55.      
  56.       if #chat > maxY - 1 then -- only update position if the screen is not full
  57.         position = position + 1
  58.       end
  59.      
  60.     elseif event == "monitor_touch" then
  61.        
  62.       x,y=tonumber(p2),tonumber(p3)
  63.      
  64.       if y == maxY then -- first test for y (row)
  65.          
  66.             if x <= maxX and x >= maxX - 1 then -- up was clicked
  67.               position = position -1
  68.             elseif x <= maxX - 3 and x >= maxX - 7 then -- down was clicked
  69.               position = position + 1
  70.             end
  71.            
  72.       end
  73.      
  74.     end
  75.  
  76.     if position < 0 then
  77.       position = 0
  78.     elseif position > lines then
  79.       position = lines
  80.     elseif position > #chat then
  81.       position = #chat
  82.     end
  83.              
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement