|
| 1 | +--[[-- Control - Chat Popup |
| 2 | +Creates flying text entities when a player sends a message in chat |
| 3 | +]] |
| 4 | + |
| 5 | +local FlyingText = require("modules/exp_util/flying_text") |
| 6 | +local config = require("modules.exp_legacy.config.popup_messages") |
| 7 | + |
| 8 | +local lower = string.lower |
| 9 | +local find = string.find |
| 10 | + |
| 11 | +--- Create a chat bubble when a player types a message |
| 12 | +--- @param event EventData.on_console_chat |
| 13 | +local function on_console_chat(event) |
| 14 | + if not event.player_index then return end |
| 15 | + local player = assert(game.get_player(event.player_index)) |
| 16 | + local name = player.name |
| 17 | + |
| 18 | + -- Sends the message as text above them |
| 19 | + if config.show_player_messages then |
| 20 | + FlyingText.create_as_player{ |
| 21 | + target_player = player, |
| 22 | + text = { "exp_chat-popup.flying-text-message", name, event.message }, |
| 23 | + } |
| 24 | + end |
| 25 | + |
| 26 | + if not config.show_player_mentions then return end |
| 27 | + |
| 28 | + -- Loops over online players to see if they name is included |
| 29 | + local search_string = lower(event.message) |
| 30 | + for _, mentioned_player in ipairs(game.connected_players) do |
| 31 | + if mentioned_player.index ~= player.index then |
| 32 | + if find(search_string, lower(mentioned_player.name), 1, true) then |
| 33 | + FlyingText.create_as_player{ |
| 34 | + target_player = mentioned_player, |
| 35 | + text = { "exp_chat-popup.flying-text-ping", name }, |
| 36 | + } |
| 37 | + end |
| 38 | + end |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +local e = defines.events |
| 43 | + |
| 44 | +return { |
| 45 | + events = { |
| 46 | + [e.on_console_chat] = on_console_chat, |
| 47 | + } |
| 48 | +} |
0 commit comments