From 784a75b7c5e85e2f2945ffa743db16030a18904b Mon Sep 17 00:00:00 2001
From: Chris <2648373+QuiCM@users.noreply.github.com>
Date: Tue, 13 Apr 2021 13:14:56 +0930
Subject: [PATCH] Implement handled pattern on the PlayerChat hook
---
TShockAPI/Hooks/PlayerHooks.cs | 6 ++++--
TShockAPI/TShock.cs | 20 +++++++++++++++++---
2 files changed, 21 insertions(+), 5 deletions(-)
diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs
index 9056fe55..3114020b 100644
--- a/TShockAPI/Hooks/PlayerHooks.cs
+++ b/TShockAPI/Hooks/PlayerHooks.cs
@@ -449,14 +449,16 @@ namespace TShockAPI.Hooks
/// The player firing the event.
/// The raw chat text sent by the player.
/// The chat text after being formatted.
- public static void OnPlayerChat(TSPlayer ply, string rawtext, ref string tshockText)
+ public static bool OnPlayerChat(TSPlayer ply, string rawtext, ref string tshockText)
{
if (PlayerChat == null)
- return;
+ return false;
var args = new PlayerChatEventArgs {Player = ply, RawText = rawtext, TShockFormattedText = tshockText};
PlayerChat(args);
tshockText = args.TShockFormattedText;
+
+ return args.Handled;
}
///
diff --git a/TShockAPI/TShock.cs b/TShockAPI/TShock.cs
index 6b0e36ad..52bef011 100644
--- a/TShockAPI/TShock.cs
+++ b/TShockAPI/TShock.cs
@@ -1330,9 +1330,17 @@ namespace TShockAPI
{
text = String.Format(Config.Settings.ChatFormat, tsplr.Group.Name, tsplr.Group.Prefix, tsplr.Name, tsplr.Group.Suffix,
args.Text);
- Hooks.PlayerHooks.OnPlayerChat(tsplr, args.Text, ref text);
- Utils.Broadcast(text, tsplr.Group.R, tsplr.Group.G, tsplr.Group.B);
+
+ //Invoke the PlayerChat hook. If this hook event is handled then we need to prevent sending the chat message
+ bool cancelChat = PlayerHooks.OnPlayerChat(tsplr, args.Text, ref text);
args.Handled = true;
+
+ if (cancelChat)
+ {
+ return;
+ }
+
+ Utils.Broadcast(text, tsplr.Group.R, tsplr.Group.G, tsplr.Group.B);
}
else
{
@@ -1344,7 +1352,13 @@ namespace TShockAPI
//Give that poor player their name back :'c
ply.name = name;
- PlayerHooks.OnPlayerChat(tsplr, args.Text, ref text);
+
+ bool cancelChat = PlayerHooks.OnPlayerChat(tsplr, args.Text, ref text);
+ if (cancelChat)
+ {
+ args.Handled = true;
+ return;
+ }
//This netpacket is used to send chat text from the server to clients, in this case on behalf of a client
Terraria.Net.NetPacket packet = Terraria.GameContent.NetModules.NetTextModule.SerializeServerMessage(