From ffed36c6fd9f1003f7d9f7094867d89420212e4d Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 12:00:13 +0700 Subject: [PATCH 01/10] Added hooks --- TShockAPI/Hooks/PlayerHooks.cs | 101 +++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index 7a3e20679..e6b685816 100644 --- a/TShockAPI/Hooks/PlayerHooks.cs +++ b/TShockAPI/Hooks/PlayerHooks.cs @@ -119,6 +119,53 @@ public class PlayerCommandEventArgs : HandledEventArgs public string CommandPrefix { get; set; } } + /// + /// EventArgs used for the event. + /// + public class PrePlayerCommandEventArgs : HandledEventArgs + { + /// + /// The command entered by the player. + /// + public Command Command { get; } + /// + /// Command arguments. + /// + public CommandArgs Arguments { get; set; } + + public PrePlayerCommandEventArgs(Command command, CommandArgs args) + { + Command = command; + Arguments = args; + } + } + + /// + /// EventArgs used for the event. + /// + public class PostPlayerCommandEventArgs + { + /// + /// The command entered by the player. + /// + public Command Command { get; } + /// + /// Command arguments. + /// + public CommandArgs Arguments { get; } + /// + /// Is the command executed. + /// + public bool Handled { get; } + + public PostPlayerCommandEventArgs(Command command, CommandArgs arguments, bool handled) + { + Command = command; + Arguments = arguments; + Handled = handled; + } + } + /// /// EventArgs used for the event. /// @@ -343,6 +390,26 @@ public static class PlayerHooks /// public static event PlayerCommandD PlayerCommand; + /// + /// The delegate of the event. + /// + /// The EventArgs for this event. + public delegate void PrePlayerCommandD(PrePlayerCommandEventArgs e); + /// + /// Fired before the command is run. + /// + public static event PrePlayerCommandD PrePlayerCommand; + + /// + /// The delegate of the event. + /// + /// The EventArgs for this event. + public delegate void PostPlayerCommandD(PostPlayerCommandEventArgs e); + /// + /// Fired after the command is run. + /// + public static event PostPlayerCommandD PostPlayerCommand; + /// /// The delegate of the event. /// @@ -449,6 +516,40 @@ public static bool OnPlayerCommand(TSPlayer player, string cmdName, string cmdTe return playerCommandEventArgs.Handled; } + /// + /// Fires the event. + /// + /// Command to be executed + /// Command arguments + /// True if the event has been handled. + public static bool OnPrePlayerCommand(Command cmd, ref CommandArgs arguments) + { + if (PrePlayerCommand == null) + return false; + + PrePlayerCommandEventArgs args = new PrePlayerCommandEventArgs(cmd, arguments); + + PrePlayerCommand(args); + + arguments = args.Arguments; + return args.Handled; + } + + /// + /// Fires the event. + /// + /// Executed command. + /// Command arguments. + /// Is the command executed. + public static void OnPostPlayerCommand(Command cmd, CommandArgs arguments, bool handled) + { + if (PostPlayerCommand == null) + return; + + PostPlayerCommandEventArgs args = new PostPlayerCommandEventArgs(cmd, arguments, handled); + PostPlayerCommand(args); + } + /// /// Fires the event. /// From 380f823e6186667e5379f867ec09b66b66b0fa60 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 12:00:49 +0700 Subject: [PATCH 02/10] Added a `Run` overload to `Command`. The overload executes the CommandArgs you want. --- TShockAPI/Commands.cs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index 2214f252d..f10be370b 100644 --- a/TShockAPI/Commands.cs +++ b/TShockAPI/Commands.cs @@ -148,24 +148,29 @@ public Command(CommandDelegate cmd, params string[] names) Permissions = new List(); } - public bool Run(string msg, bool silent, TSPlayer ply, List parms) + public bool Run(CommandArgs args) { - if (!CanRun(ply)) + if (!CanRun(args.Player)) return false; try { - CommandDelegate(new CommandArgs(msg, silent, ply, parms)); + CommandDelegate(args); } catch (Exception e) { - ply.SendErrorMessage(GetString("Command failed, check logs for more details.")); + args.Player.SendErrorMessage(GetString("Command failed, check logs for more details.")); TShock.Log.Error(e.ToString()); } return true; } + public bool Run(string msg, bool silent, TSPlayer ply, List parms) + { + return Run(new CommandArgs(msg, silent, ply, parms)); + } + public bool Run(string msg, TSPlayer ply, List parms) { return Run(msg, false, ply, parms); @@ -704,7 +709,12 @@ public static bool HandleCommand(TSPlayer player, string text) TShock.Utils.SendLogs(GetString("{0} executed: {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdText), Color.PaleVioletRed, player); else TShock.Utils.SendLogs(GetString("{0} executed (args omitted): {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdName), Color.PaleVioletRed, player); - cmd.Run(cmdText, silent, player, args); + + CommandArgs arguments = new CommandArgs(cmdText, silent, player, args); + bool handled = PlayerHooks.OnPrePlayerCommand(cmd, ref arguments); + if (!handled) + cmd.Run(arguments); + PlayerHooks.OnPostPlayerCommand(cmd, arguments, handled); } } return true; From 7a3b2e051f6d7fe7dd012d0457f83050238360d0 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 12:11:44 +0700 Subject: [PATCH 03/10] Update changelog.md --- docs/changelog.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/changelog.md b/docs/changelog.md index 3f64f3554..f5a0003c1 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -78,7 +78,8 @@ Use past tense when adding new entries; sign your name off when you add or chang * If there is no section called "Upcoming changes" below this line, please add one with `## Upcoming changes` as the first line, and then a bulleted item directly after with the first change. --> ## Upcoming changes -Your changes could be here! +* Added `PlayerHooks.PrePlayerCommand` hook, which fired before command execution. +* Added `PlayerHooks.PostPlayerCommand` hook, which fired after command execution. ## TShock 5.2 * An additional option `pvpwithnoteam` is added at `PvPMode` to enable PVP with no team. (@CelestialAnarchy, #2617, @ATFGK) From a656a1cc198014e06514c62b4f11456b29db0773 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 12:17:11 +0700 Subject: [PATCH 04/10] Added authorship --- docs/changelog.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index f5a0003c1..528683904 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -78,8 +78,8 @@ Use past tense when adding new entries; sign your name off when you add or chang * If there is no section called "Upcoming changes" below this line, please add one with `## Upcoming changes` as the first line, and then a bulleted item directly after with the first change. --> ## Upcoming changes -* Added `PlayerHooks.PrePlayerCommand` hook, which fired before command execution. -* Added `PlayerHooks.PostPlayerCommand` hook, which fired after command execution. +* Added `PlayerHooks.PrePlayerCommand` hook, which fired before command execution. (@AgaSpace) +* Added `PlayerHooks.PostPlayerCommand` hook, which fired after command execution. (@AgaSpace) ## TShock 5.2 * An additional option `pvpwithnoteam` is added at `PvPMode` to enable PVP with no team. (@CelestialAnarchy, #2617, @ATFGK) From 1ef96f9537131aa5df55900ecbc39b69bc210935 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 13:01:52 +0700 Subject: [PATCH 05/10] Marked the `PlayerCommand` hook as obsolete. --- TShockAPI/Hooks/PlayerHooks.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index e6b685816..15cac88dd 100644 --- a/TShockAPI/Hooks/PlayerHooks.cs +++ b/TShockAPI/Hooks/PlayerHooks.cs @@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +using System; using System.Collections.Generic; using System.ComponentModel; using TShockAPI.DB; @@ -388,6 +389,7 @@ public static class PlayerHooks /// /// Fired by players when using a command. /// + [Obsolete("There is an alternative to PlayerHooks.PrePlayerCommand")] public static event PlayerCommandD PlayerCommand; /// From 21422f25fd3b4c3a0476ea1e83ee0de8d8895cf4 Mon Sep 17 00:00:00 2001 From: Zoom L1 <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 15:51:01 +0700 Subject: [PATCH 06/10] Update TShockAPI/Hooks/PlayerHooks.cs Co-authored-by: Arthri <41360489+Arthri@users.noreply.github.com> --- TShockAPI/Hooks/PlayerHooks.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index 15cac88dd..8135a3f9b 100644 --- a/TShockAPI/Hooks/PlayerHooks.cs +++ b/TShockAPI/Hooks/PlayerHooks.cs @@ -408,7 +408,7 @@ public static class PlayerHooks /// The EventArgs for this event. public delegate void PostPlayerCommandD(PostPlayerCommandEventArgs e); /// - /// Fired after the command is run. + /// Fired after a command is run. /// public static event PostPlayerCommandD PostPlayerCommand; From b40f0e632e1dfb8966dca14a9ee707318a1ed12f Mon Sep 17 00:00:00 2001 From: Zoom L1 <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 16:01:43 +0700 Subject: [PATCH 07/10] Update TShockAPI/Hooks/PlayerHooks.cs Co-authored-by: Arthri <41360489+Arthri@users.noreply.github.com> --- TShockAPI/Hooks/PlayerHooks.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index 8135a3f9b..dd35d662e 100644 --- a/TShockAPI/Hooks/PlayerHooks.cs +++ b/TShockAPI/Hooks/PlayerHooks.cs @@ -398,7 +398,7 @@ public static class PlayerHooks /// The EventArgs for this event. public delegate void PrePlayerCommandD(PrePlayerCommandEventArgs e); /// - /// Fired before the command is run. + /// Fired before a command is run. /// public static event PrePlayerCommandD PrePlayerCommand; From db40d5034833e0a0833d50de3e87d2da99b08af7 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 16:03:43 +0700 Subject: [PATCH 08/10] Updated the message --- TShockAPI/Hooks/PlayerHooks.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index dd35d662e..183205fed 100644 --- a/TShockAPI/Hooks/PlayerHooks.cs +++ b/TShockAPI/Hooks/PlayerHooks.cs @@ -389,7 +389,7 @@ public static class PlayerHooks /// /// Fired by players when using a command. /// - [Obsolete("There is an alternative to PlayerHooks.PrePlayerCommand")] + [Obsolete("Use PlayerHooks.PrePlayerCommand.")] public static event PlayerCommandD PlayerCommand; /// From 723719350b07daf84ec14aa12d57e1d6691697b4 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 17:37:22 +0700 Subject: [PATCH 09/10] Made `PostPlayerCommandEventArgs` inherited from `HandledEventArgs`. --- TShockAPI/Hooks/PlayerHooks.cs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index 183205fed..b50482742 100644 --- a/TShockAPI/Hooks/PlayerHooks.cs +++ b/TShockAPI/Hooks/PlayerHooks.cs @@ -144,7 +144,7 @@ public PrePlayerCommandEventArgs(Command command, CommandArgs args) /// /// EventArgs used for the event. /// - public class PostPlayerCommandEventArgs + public class PostPlayerCommandEventArgs : HandledEventArgs { /// /// The command entered by the player. @@ -154,10 +154,6 @@ public class PostPlayerCommandEventArgs /// Command arguments. /// public CommandArgs Arguments { get; } - /// - /// Is the command executed. - /// - public bool Handled { get; } public PostPlayerCommandEventArgs(Command command, CommandArgs arguments, bool handled) { From 87fabd3d260b8bc77a47e939fdc7c756dc967b59 Mon Sep 17 00:00:00 2001 From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com> Date: Thu, 15 Jun 2023 18:44:30 +0700 Subject: [PATCH 10/10] Removed the "obsolete" tag --- TShockAPI/Hooks/PlayerHooks.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index b50482742..437564640 100644 --- a/TShockAPI/Hooks/PlayerHooks.cs +++ b/TShockAPI/Hooks/PlayerHooks.cs @@ -385,7 +385,6 @@ public static class PlayerHooks /// /// Fired by players when using a command. /// - [Obsolete("Use PlayerHooks.PrePlayerCommand.")] public static event PlayerCommandD PlayerCommand; ///