KedrikFeeD

messager.lua

Feb 12th, 2025 (edited)
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. local basalt = require("modules/basalt")
  2. basalt.setTheme("LabelText", colors.red)
  3.  
  4. local player = require("player")
  5.  
  6. local main = basalt.createFrame()
  7.     :setBackground(colors.lightBlue)
  8.  
  9. local tx, ty = term.current().getSize()
  10.  
  11. local title = main
  12.     :addLabel()
  13.     :setText("Message receiver")
  14.  
  15. local titleX, titleY = title.getSize()
  16. title:setPosition(
  17.     math.floor(tx / 2 - titleX / 2) + 1,
  18.     math.floor(2)
  19. ):setForeground(colors.black)
  20.  
  21. local chatFrame = main:addScrollableFrame()
  22.     :setPosition(2, 3)
  23.     :setSize("parent.w - 2", "parent.h - 4")
  24.     :setBackground(colors.black)
  25.  
  26. local chatFrameX, chatFrameY = chatFrame:getSize()
  27.  
  28. local noMessagesText = chatFrame
  29.     :addLabel()
  30.     :setText("No messages found")
  31.     :setBackground(colors.black)
  32.     :setForeground(colors.red)
  33.  
  34. local noMessageX, noMessageY = noMessagesText
  35.     :getSize()
  36.    
  37. noMessagesText:setPosition(
  38.     math.floor(chatFrameX / 2 - noMessageX / 2) + 1,
  39.     math.floor(chatFrameY / 2 + 1)
  40. )
  41.  
  42. local current = 1
  43. local CFWidth, CFHeight = chatFrame.getSize()
  44.  
  45. function showMessage(id, message)
  46.     noMessagesText:hide()
  47.     local from = ""
  48.     local textColor = colors.red
  49.     if not id then
  50.         from = "You: "
  51.         textColor = colors.blue  
  52.     else
  53.         from = "ID_" .. id .. ": "
  54.         textColor = colors.green
  55.         player.playNotice()
  56.     end
  57.     chatFrame
  58.         :addLabel()
  59.         :setPosition(1, current)
  60.         :setBackground(colors.black)
  61.         :setForeground(textColor)
  62.         :setText(from .. message)
  63.     if current > CFHeight then
  64.         chatFrame:setOffset(0, current - CFHeight)
  65.     end
  66.     current = current + 1
  67. end
  68. basalt.onEvent(function(event, client, message)
  69.     if event == "rednet_message" then
  70.         showMessage(client, message)
  71.     end
  72. end)
  73.  
  74. local input = main
  75.     :addInput()
  76.     :setBackground(colors.gray)
  77.     :setForeground(colors.lightBlue)
  78.     :setDefaultText("Send message")
  79.     :setPosition(2, ty - 1)
  80.     :setSize("parent.w - 2", 1)
  81.     :onLoseFocus(function(self)
  82.         local message = self:getValue()
  83.         if message and message ~= "" then
  84.             showMessage(nil, message)
  85.             rednet.broadcast(message)
  86.         end
  87.         self:setValue("")
  88.     end)
  89.      
  90. basalt.autoUpdate()
Add Comment
Please, Sign In to add comment