Implement handled pattern on the PlayerChat hook

This commit is contained in:
Chris 2021-04-13 13:14:56 +09:30
parent 32a0e2f1b6
commit 784a75b7c5
2 changed files with 21 additions and 5 deletions

View file

@ -449,14 +449,16 @@ namespace TShockAPI.Hooks
/// <param name="ply">The player firing the event.</param> /// <param name="ply">The player firing the event.</param>
/// <param name="rawtext">The raw chat text sent by the player.</param> /// <param name="rawtext">The raw chat text sent by the player.</param>
/// <param name="tshockText">The chat text after being formatted.</param> /// <param name="tshockText">The chat text after being formatted.</param>
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) if (PlayerChat == null)
return; return false;
var args = new PlayerChatEventArgs {Player = ply, RawText = rawtext, TShockFormattedText = tshockText}; var args = new PlayerChatEventArgs {Player = ply, RawText = rawtext, TShockFormattedText = tshockText};
PlayerChat(args); PlayerChat(args);
tshockText = args.TShockFormattedText; tshockText = args.TShockFormattedText;
return args.Handled;
} }
/// <summary> /// <summary>

View file

@ -1330,9 +1330,17 @@ namespace TShockAPI
{ {
text = String.Format(Config.Settings.ChatFormat, tsplr.Group.Name, tsplr.Group.Prefix, tsplr.Name, tsplr.Group.Suffix, text = String.Format(Config.Settings.ChatFormat, tsplr.Group.Name, tsplr.Group.Prefix, tsplr.Name, tsplr.Group.Suffix,
args.Text); 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; args.Handled = true;
if (cancelChat)
{
return;
}
Utils.Broadcast(text, tsplr.Group.R, tsplr.Group.G, tsplr.Group.B);
} }
else else
{ {
@ -1344,7 +1352,13 @@ namespace TShockAPI
//Give that poor player their name back :'c //Give that poor player their name back :'c
ply.name = name; 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 //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( Terraria.Net.NetPacket packet = Terraria.GameContent.NetModules.NetTextModule.SerializeServerMessage(