diff --git a/TShockAPI/Commands.cs b/TShockAPI/Commands.cs index d39b84e0c..1cab029a0 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; diff --git a/TShockAPI/Hooks/PlayerHooks.cs b/TShockAPI/Hooks/PlayerHooks.cs index 7a3e20679..437564640 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; @@ -119,6 +120,49 @@ 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 : HandledEventArgs + { + /// + /// The command entered by the player. + /// + public Command Command { get; } + /// + /// Command arguments. + /// + public CommandArgs Arguments { get; } + + public PostPlayerCommandEventArgs(Command command, CommandArgs arguments, bool handled) + { + Command = command; + Arguments = arguments; + Handled = handled; + } + } + /// /// EventArgs used for the event. /// @@ -343,6 +387,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 a 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 a command is run. + /// + public static event PostPlayerCommandD PostPlayerCommand; + /// /// The delegate of the event. /// @@ -449,6 +513,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. /// diff --git a/docs/changelog.md b/docs/changelog.md index d4ab3cfeb..be7c29c29 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -89,6 +89,8 @@ Use past tense when adding new entries; sign your name off when you add or chang * Added a method `TSPlayer.GiveItem`, which has `TShockAPI.NetItem` structure in its arguments. (@AgaSpace) * Added a property `TSPlayer.Hostile`, which gets pvp player mode. (@AgaSpace) * Fixed typo in `/gbuff`. (@sgkoishi, #2955) +* 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)