paramus

Messenger

Aug 18th, 2025 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.81 KB | Gaming | 0 0
  1. --TODO: Add a read() that specifies the broadcast channel (for privacy), if left empty, use the standard
  2.  
  3. local MaxX,MaxY = term.getSize()
  4. local msg = ""
  5. local shift = false
  6.  
  7. term.clear()
  8. term.setCursorPos(1, 1)
  9. print("Choose a name:")
  10. local user = read()
  11.  
  12. peripheral.find("modem", rednet.open)
  13. term.clear()
  14. while(true) do
  15.     parallel.waitForAny(function()
  16.             local id, message = rednet.receive()
  17.             term.setTextColor(colors.white)
  18.             local cY,cY = term.getCursorPos()
  19.             term.setCursorPos(1, cY)
  20.             print(message)
  21.             term.setCursorPos(1, MaxY)
  22.             write(msg)
  23.             sleep(1)
  24.         end,
  25.         function()
  26.             local event, key, is_held = os.pullEvent("key")
  27.             sKey = keys.getName(key)
  28.             if (sKey == "enter") then
  29.                 term.setTextColor(colors.green)
  30.                 local cY,cY = term.getCursorPos()
  31.                 term.setCursorPos(1, cY)
  32.                 print(msg)
  33.                 rednet.broadcast(user .. "> " .. msg)
  34.                 msg = ""
  35.             elseif (sKey == "backspace") then
  36.                 msg = string.sub(msg, 1, string.len(msg)-1)
  37.             elseif (sKey == "space") then
  38.                 msg = msg .. " "
  39.             elseif (sKey == "leftShift" or sKey == "rightShift") then
  40.                 shift = true
  41.             elseif (key >=48 and key <= 57) then
  42.                 sKey = key-48
  43.                 msg = msg .. sKey
  44.             elseif (sKey ~= nil) then
  45.                 if (shift) then
  46.                     sKey = string.upper(sKey)
  47.                     shift = false
  48.                 end
  49.                 msg = msg .. sKey
  50.             end
  51.             term.setCursorPos(1, MaxY)
  52.             term.clearLine()
  53.             term.setTextColor(colors.white)
  54.             write(msg)
  55.         end)
  56. end
Advertisement
Add Comment
Please, Sign In to add comment