Allow raising event when sending chat message wth API

This commit is contained in:
Sardelka
2022-07-29 15:07:52 +08:00
parent f22ecb03f2
commit 370247eef2
3 changed files with 19 additions and 5 deletions

View File

@ -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() OnChatMessage?.Invoke(this, new ChatEventArgs()
{ {
Sender=sender, Client=sender,
Message=msg Message=msg,
ClaimedSender=clamiedSender
}); });
} }
internal void InvokePlayerConnected(Client client) internal void InvokePlayerConnected(Client client)
@ -200,8 +201,11 @@ namespace RageCoop.Server.Scripting
/// <param name="targets">The clients to send message, leave it null to send to all clients</param> /// <param name="targets">The clients to send message, leave it null to send to all clients</param>
/// <param name="message">The chat message</param> /// <param name="message">The chat message</param>
/// <param name="username">The username which send this message (default = "Server")</param> /// <param name="username">The username which send this message (default = "Server")</param>
public void SendChatMessage(string message, List<Client> targets = null, string username = "Server") /// <param name="raiseEvent">Weather to raise the <see cref="ServerEvents.OnChatMessage"/> event defined in <see cref="API.Events"/></param>
/// <remarks>When <paramref name="raiseEvent"/> is unspecified and <paramref name="targets"/> is null or unspecified, <paramref name="raiseEvent"/> will be set to true</remarks>
public void SendChatMessage(string message, List<Client> targets = null, string username = "Server",bool? raiseEvent=null)
{ {
raiseEvent ??= targets==null;
try try
{ {
if (Server.MainNetServer.ConnectionsCount == 0) 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} <<"); Server.Logger?.Error($">> {e.Message} <<>> {e.Source ?? string.Empty} <<>> {e.StackTrace ?? string.Empty} <<");
} }
if (raiseEvent.Value)
{
Events.InvokeOnChatMessage(message, null, username);
}
} }
/// <summary> /// <summary>

View File

@ -15,11 +15,16 @@ namespace RageCoop.Server.Scripting
/// <summary> /// <summary>
/// The client that sent this message /// The client that sent this message
/// </summary> /// </summary>
public Client Sender { get; set; } public Client Client { get; set; }
/// <summary> /// <summary>
/// Message /// Message
/// </summary> /// </summary>
public string Message { get; set; } public string Message { get; set; }
/// <summary>
/// Only used when sending a message via <see cref="API.SendChatMessage(string, List{Client}, string, bool?)"/>
/// </summary>
public string ClaimedSender { get; set; }
} }
/// <summary> /// <summary>
/// ///

View File

@ -798,6 +798,7 @@ namespace RageCoop.Server
} }
internal void SendChatMessage(string name, string message, Client target) internal void SendChatMessage(string name, string message, Client target)
{ {
if(target == null) { return; }
var msg = MainNetServer.CreateMessage(); var msg = MainNetServer.CreateMessage();
new Packets.ChatMessage() new Packets.ChatMessage()
{ {