diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index 041b1936d..ff207c9f4 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -3973,8 +3973,20 @@ namespace Barotrauma.Networking var hookChatMsg = ChatMessage.Create(senderName, message, (ChatMessageType)type, senderCharacter, senderClient, changeType); - var should = LuaCsSetup.Instance.Hook.Call("modifyChatMessage", hookChatMsg, senderRadio); + bool shouldSkip = false; + LuaCsSetup.Instance.EventService.PublishEvent(sub => + { + if (sub.OnModifyMessagePredicate(hookChatMsg, senderRadio) is true) + { + shouldSkip = true; + } + }); + if (shouldSkip) + { + return; + } + //check which clients can receive the message and apply distance effects foreach (Client client in ConnectedClients) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs index d7fa5761b..095ec22bd 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs @@ -79,6 +79,39 @@ internal interface IEventSettingInstanceLifetime : IEvent +/// Allows the user to modify a chat message on the server before it is sent to clients, or reject the message altogether. +/// +/// Legacy Lua Event Name: "modifyChatMessage" +internal interface IEventModifyChatMessage : IEvent +{ + bool? OnModifyMessagePredicate(ChatMessage message, WifiComponent senderRadio); + + static IEventModifyChatMessage IEvent.GetLuaRunner(IDictionary luaFunc) => + new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventModifyChatMessage + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + /// + /// Called before a chat message is sent to clients. + /// + /// Message to be sent. + /// [CanBeNull] The source , if any. + /// Whether to reject the message. + public bool? OnModifyMessagePredicate(ChatMessage message, WifiComponent senderRadio) + { + return (bool?)LuaFuncs[nameof(OnModifyMessagePredicate)](message, senderRadio); + } + } +} + +#endif + internal interface IEventAfflictionUpdate : IEvent { void OnAfflictionUpdate(Affliction affliction, CharacterHealth characterHealth, Limb targetLimb, float deltaTime); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs index ccae1545c..af8d61df0 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs @@ -312,6 +312,7 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService, // Compatibility _eventService.RegisterLuaEventAlias("clientConnected", nameof(IEventClientConnected.OnClientConnected)); _eventService.RegisterLuaEventAlias("clientDisconnected", nameof(IEventClientDisconnected.OnClientDisconnected)); + _eventService.RegisterLuaEventAlias("modifyChatMessage", nameof(IEventModifyChatMessage.OnModifyMessagePredicate)); #elif CLIENT _eventService.RegisterLuaEventAlias("netMessageReceived", nameof(IEventServerRawNetMessageReceived.OnReceivedServerNetMessage)); #endif