Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Name: Advanced Chat Box
- Description: Monitor chat on your server or in predefined of your chatbox.
- Usage: chat <chatbox side> <lines of history to keep>
- ]]
- local chat = {}
- local maxX, maxY = term.getSize()
- local position = 1
- function cls()
- term.clear()
- term.setCursorPos(1,1)
- end
- function updateScrn()
- cls()
- for i=position,position+maxY-1 do -- -1 to prevent writing on last line
- if chat[i] then
- print(chat[i])
- end
- end
- term.setCursorPos(maxX-7,maxY)
- term.write("down up")
- end
- args = {...}
- if #args < 2 then
- print ("Usage:")
- print ("chat <side> <history>")
- print()
- print("Where <side> is the position of the chatbox in relation to the computer and <history> is the amount of chat lines to buffer.")
- return --exit the program because the user failed
- end
- local side,lines = args[1],tonumber(args[2])
- _ = peripheral.wrap(side)
- cls()
- while true do
- -- update screen
- updateScrn()
- local event, p1, p2, p3 = os.pullEvent()
- if event == "chat" then
- table.insert(chat,"<".. p1 .."> " .. p2) -- add it to the chat table
- if #chat > lines then
- -- limit size of history
- table.remove(chat,1) -- removes the first entry
- end
- if #chat > maxY - 1 then -- only update position if the screen is not full
- position = position + 1
- end
- elseif event == "monitor_touch" then
- x,y=tonumber(p2),tonumber(p3)
- if y == maxY then -- first test for y (row)
- if x <= maxX and x >= maxX - 1 then -- up was clicked
- position = position -1
- elseif x <= maxX - 3 and x >= maxX - 7 then -- down was clicked
- position = position + 1
- end
- end
- end
- if position < 0 then
- position = 0
- elseif position > lines then
- position = lines
- elseif position > #chat then
- position = #chat
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement