Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft EnderChat

Sep 14th, 2024 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. -- Chat Client with MiniChat support
  2. local args = { ... }
  3. if #args < 1 then
  4.     print("Usage: chat_client <username>")
  5.     return
  6. end
  7.  
  8. local username = args[1]
  9. local modem = peripheral.find("modem")
  10. if not modem then
  11.     print("Ender Modem not found!")
  12.     return
  13. end
  14.  
  15. local defaultChannel = 1
  16. modem.open(defaultChannel)
  17.  
  18. -- Message history
  19. local messages = {}
  20. local maxMessages = 18
  21.  
  22. -- Current channel
  23. local currentChannel = "global"
  24.  
  25. -- Function to display chat history
  26. function displayMessages()
  27.     term.clear()
  28.     term.setCursorPos(1, 1)
  29.     for _, msg in ipairs(messages) do
  30.         print(msg)
  31.     end
  32. end
  33.  
  34. -- Function to send a chat message
  35. function sendMessage(message)
  36.     modem.transmit(defaultChannel, defaultChannel, {type="message", username=username, channel=currentChannel, message=message})
  37. end
  38.  
  39. -- Function to join a MiniChat channel
  40. function joinChannel(channel)
  41.     currentChannel = channel
  42.     modem.transmit(defaultChannel, defaultChannel, {type="join", username=username, channel=channel})
  43. end
  44.  
  45. -- Function to receive incoming messages
  46. function handleIncomingMessage(event, side, freq, replyFreq, message, dist)
  47.     if freq == defaultChannel and type(message) == "table" then
  48.         if message.type == "message" then
  49.             table.insert(messages, message.data)
  50.             if #messages > maxMessages then
  51.                 table.remove(messages, 1)
  52.             end
  53.             displayMessages()
  54.         elseif message.type == "status" then
  55.             print(message.data)
  56.         end
  57.     end
  58. end
  59.  
  60. -- Main function for sending and receiving messages
  61. function chatLoop()
  62.     parallel.waitForAny(
  63.         function()
  64.             while true do
  65.                 local input = read()
  66.  
  67.                 if input:sub(1, 8) == "/minichat" then
  68.                     local channel = input:sub(10)
  69.                     joinChannel(channel)
  70.                 else
  71.                     sendMessage(input)
  72.                 end
  73.             end
  74.         end,
  75.         function()
  76.             while true do
  77.                 local event, side, freq, replyFreq, message, dist = os.pullEvent("modem_message")
  78.                 handleIncomingMessage(event, side, freq, replyFreq, message, dist)
  79.             end
  80.         end
  81.     )
  82. end
  83.  
  84. -- Start the chat loop
  85. chatLoop()
  86.  
  87. modem.close(defaultChannel)
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement