paramus

CatChat

Jul 30th, 2026
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | Gaming | 0 0
  1. -- DESCRIPTION: This program was made to be used as an alternative chat in servers that disabled the in-game chat.
  2.  
  3. local modem = peripheral.find("modem") or error("No modem attached", 0)
  4. modem.open(43)
  5.  
  6. local name = nil
  7. local messages = {}
  8. local text = ""
  9. local windowWidth, windowHeight = term.getSize()
  10.  
  11. function NameSelection()
  12.     local error = false
  13.     while (name == nil or #name < 1) do
  14.         term.clear()
  15.        
  16.         term.setTextColor(colors.white)
  17.         term.setCursorPos(1, 1)
  18.         term.write("Welcome to CatChat! Please write your name.")
  19.  
  20.         if (error) then
  21.             term.setTextColor(colors.red)
  22.             term.setCursorPos(1, 6)
  23.             term.write("The name is obligatory.")
  24.         end
  25.  
  26.         term.setTextColor(colors.white)
  27.         term.setCursorPos(1, 3)
  28.         term.write("> ")
  29.         term.setCursorPos(1, 3)
  30.         name = read()
  31.  
  32.         error = true
  33.     end
  34. end
  35.  
  36. function PrintScreen()
  37.     while (true) do
  38.         term.setBackgroundColor(colors.white)
  39.         term.clear()
  40.  
  41.         -- Received messages
  42.         term.setTextColor(colors.red)
  43.         local msgY = windowHeight-2
  44.         for i=#messages, 1 do
  45.             if (msgY <= 0) then
  46.                 break
  47.             end
  48.  
  49.             local msg = messages[i]
  50.             term.setCursorPos(1, msgY)
  51.             term.write(msg)
  52.  
  53.             msgY = msgY -1
  54.         end
  55.  
  56.         -- Current message
  57.         paintutils.drawLine(1,windowHeight-1, 1,windowHeight-1, colors.black)
  58.         term.setTextColor(colors.green)
  59.         term.setCursorPos(1, windowHeight-1)
  60.         term.write(text)
  61.  
  62.         sleep(0.5)
  63.     end
  64. end
  65.  
  66. function GetMessage()
  67.     while (true) do
  68.         local event, side, channel, replyChannel, message, distance = os.pullEvent("modem_message")
  69.         table.insert(messages, message)
  70.     end
  71. end
  72.  
  73. function SendMessage(msg)
  74.     if (msg == nil or msg == "") then
  75.         return
  76.     end
  77.    
  78.     modem.transmit(43, 43, "<" .. name .. "> " .. msg)
  79. end
  80.  
  81. function WriteMessage()
  82.     while true do
  83.         local event, key, is_held = os.pullEvent("key")
  84.        
  85.         key = keys.getName(key)
  86.         if (#key == 1) then
  87.             text = text .. key
  88.         elseif (key == "space") then
  89.             text = text .. " "
  90.         elseif (key == "backspace" and #key > 0) then
  91.             text = string.sub(text, 1, #key-1)
  92.         elseif (key == "enter") then
  93.             SendMessage(text)
  94.             text = text
  95.         end
  96.     end
  97. end
  98.  
  99. -- Main
  100. NameSelection()
  101.  
  102. parallel.waitForAny(
  103. function()
  104.     PrintScreen()
  105. end,
  106. function()
  107.     GetMessage()
  108. end,
  109. function()
  110.     WriteMessage()
  111. end
  112. )
Advertisement
Add Comment
Please, Sign In to add comment