Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- DESCRIPTION: This program was made to be used as an alternative chat in servers that disabled the in-game chat.
- local modem = peripheral.find("modem") or error("No modem attached", 0)
- modem.open(43)
- local name = nil
- local messages = {}
- local text = ""
- local windowWidth, windowHeight = term.getSize()
- function NameSelection()
- local error = false
- while (name == nil or #name < 1) do
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- term.write("Welcome to CatChat! Please write your name.")
- if (error) then
- term.setTextColor(colors.red)
- term.setCursorPos(1, 6)
- term.write("The name is obligatory.")
- end
- term.setTextColor(colors.white)
- term.setCursorPos(1, 3)
- term.write("> ")
- term.setCursorPos(1, 3)
- name = read()
- error = true
- end
- end
- function PrintScreen()
- while (true) do
- term.setBackgroundColor(colors.white)
- term.clear()
- -- Received messages
- term.setTextColor(colors.red)
- local msgY = windowHeight-2
- for i=#messages, 1 do
- if (msgY <= 0) then
- break
- end
- local msg = messages[i]
- term.setCursorPos(1, msgY)
- term.write(msg)
- msgY = msgY -1
- end
- -- Current message
- paintutils.drawLine(1,windowHeight-1, 1,windowHeight-1, colors.black)
- term.setTextColor(colors.green)
- term.setCursorPos(1, windowHeight-1)
- term.write(text)
- sleep(0.5)
- end
- end
- function GetMessage()
- while (true) do
- local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
- table.insert(messages, message)
- end
- end
- function SendMessage(msg)
- if (msg == nil or msg == "") then
- return
- end
- modem.transmit(43, 43, "<" .. name .. "> " .. msg)
- end
- function WriteMessage()
- while true do
- local event, key, is_held = os.pullEvent("key")
- key = keys.getName(key)
- if (#key == 1) then
- text = text .. key
- elseif (key == "space") then
- text = text .. " "
- elseif (key == "backspace" and #key > 0) then
- text = string.sub(text, 1, #key-1)
- elseif (key == "enter") then
- SendMessage(text)
- text = text
- end
- end
- end
- -- Main
- NameSelection()
- parallel.waitForAny(
- function()
- PrintScreen()
- end,
- function()
- GetMessage()
- end,
- function()
- WriteMessage()
- end
- )
Advertisement
Add Comment
Please, Sign In to add comment