From 370247eef24210cde0f55afd4238e3ef7e958d6e Mon Sep 17 00:00:00 2001 From: Sardelka Date: Fri, 29 Jul 2022 15:07:52 +0800 Subject: [PATCH] Allow raising event when sending chat message wth API --- RageCoop.Server/Scripting/API.cs | 16 ++++++++++++---- RageCoop.Server/Scripting/EventArgs/EventArgs.cs | 7 ++++++- RageCoop.Server/Server.cs | 1 + 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/RageCoop.Server/Scripting/API.cs b/RageCoop.Server/Scripting/API.cs index b7c6b18..21c1845 100644 --- a/RageCoop.Server/Scripting/API.cs +++ b/RageCoop.Server/Scripting/API.cs @@ -99,12 +99,13 @@ namespace RageCoop.Server.Scripting } } - internal void InvokeOnChatMessage(string msg, Client sender) + internal void InvokeOnChatMessage(string msg, Client sender, string clamiedSender=null) { OnChatMessage?.Invoke(this, new ChatEventArgs() { - Sender=sender, - Message=msg + Client=sender, + Message=msg, + ClaimedSender=clamiedSender }); } internal void InvokePlayerConnected(Client client) @@ -200,8 +201,11 @@ namespace RageCoop.Server.Scripting /// The clients to send message, leave it null to send to all clients /// The chat message /// The username which send this message (default = "Server") - public void SendChatMessage(string message, List targets = null, string username = "Server") + /// Weather to raise the event defined in + /// When is unspecified and is null or unspecified, will be set to true + public void SendChatMessage(string message, List targets = null, string username = "Server",bool? raiseEvent=null) { + raiseEvent ??= targets==null; try { if (Server.MainNetServer.ConnectionsCount == 0) @@ -218,6 +222,10 @@ namespace RageCoop.Server.Scripting { Server.Logger?.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<"); } + if (raiseEvent.Value) + { + Events.InvokeOnChatMessage(message, null, username); + } } /// diff --git a/RageCoop.Server/Scripting/EventArgs/EventArgs.cs b/RageCoop.Server/Scripting/EventArgs/EventArgs.cs index 31cdd92..643191d 100644 --- a/RageCoop.Server/Scripting/EventArgs/EventArgs.cs +++ b/RageCoop.Server/Scripting/EventArgs/EventArgs.cs @@ -15,11 +15,16 @@ namespace RageCoop.Server.Scripting /// /// The client that sent this message /// - public Client Sender { get; set; } + public Client Client { get; set; } /// /// Message /// public string Message { get; set; } + + /// + /// Only used when sending a message via + /// + public string ClaimedSender { get; set; } } /// /// diff --git a/RageCoop.Server/Server.cs b/RageCoop.Server/Server.cs index 9ecd382..156d82e 100644 --- a/RageCoop.Server/Server.cs +++ b/RageCoop.Server/Server.cs @@ -798,6 +798,7 @@ namespace RageCoop.Server } internal void SendChatMessage(string name, string message, Client target) { + if(target == null) { return; } var msg = MainNetServer.CreateMessage(); new Packets.ChatMessage() {