Merge pull request #2960 from AgaSpace/commandhooks

New two hooks PrePlayerCommand and PostPlayerCommand
This commit is contained in:
Lucas Nicodemus 2025-01-27 10:09:36 +09:00 committed by GitHub
commit 5e4f17b47a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 115 additions and 5 deletions

View file

@ -148,24 +148,29 @@ namespace TShockAPI
Permissions = new List<string>(); Permissions = new List<string>();
} }
public bool Run(string msg, bool silent, TSPlayer ply, List<string> parms) public bool Run(CommandArgs args)
{ {
if (!CanRun(ply)) if (!CanRun(args.Player))
return false; return false;
try try
{ {
CommandDelegate(new CommandArgs(msg, silent, ply, parms)); CommandDelegate(args);
} }
catch (Exception e) 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()); TShock.Log.Error(e.ToString());
} }
return true; return true;
} }
public bool Run(string msg, bool silent, TSPlayer ply, List<string> parms)
{
return Run(new CommandArgs(msg, silent, ply, parms));
}
public bool Run(string msg, TSPlayer ply, List<string> parms) public bool Run(string msg, TSPlayer ply, List<string> parms)
{ {
return Run(msg, false, ply, parms); return Run(msg, false, ply, parms);
@ -704,7 +709,12 @@ namespace TShockAPI
TShock.Utils.SendLogs(GetString("{0} executed: {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdText), Color.PaleVioletRed, player); TShock.Utils.SendLogs(GetString("{0} executed: {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdText), Color.PaleVioletRed, player);
else else
TShock.Utils.SendLogs(GetString("{0} executed (args omitted): {1}{2}.", player.Name, silent ? SilentSpecifier : Specifier, cmdName), Color.PaleVioletRed, player); 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; return true;

View file

@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using TShockAPI.DB; using TShockAPI.DB;
@ -119,6 +120,49 @@ namespace TShockAPI.Hooks
public string CommandPrefix { get; set; } public string CommandPrefix { get; set; }
} }
/// <summary>
/// EventArgs used for the <see cref="PlayerHooks.PrePlayerCommand"/> event.
/// </summary>
public class PrePlayerCommandEventArgs : HandledEventArgs
{
/// <summary>
/// The command entered by the player.
/// </summary>
public Command Command { get; }
/// <summary>
/// Command arguments.
/// </summary>
public CommandArgs Arguments { get; set; }
public PrePlayerCommandEventArgs(Command command, CommandArgs args)
{
Command = command;
Arguments = args;
}
}
/// <summary>
/// EventArgs used for the <see cref="PlayerHooks.PostPlayerCommand"/> event.
/// </summary>
public class PostPlayerCommandEventArgs : HandledEventArgs
{
/// <summary>
/// The command entered by the player.
/// </summary>
public Command Command { get; }
/// <summary>
/// Command arguments.
/// </summary>
public CommandArgs Arguments { get; }
public PostPlayerCommandEventArgs(Command command, CommandArgs arguments, bool handled)
{
Command = command;
Arguments = arguments;
Handled = handled;
}
}
/// <summary> /// <summary>
/// EventArgs used for the <see cref="PlayerHooks.PlayerChat"/> event. /// EventArgs used for the <see cref="PlayerHooks.PlayerChat"/> event.
/// </summary> /// </summary>
@ -343,6 +387,26 @@ namespace TShockAPI.Hooks
/// </summary> /// </summary>
public static event PlayerCommandD PlayerCommand; public static event PlayerCommandD PlayerCommand;
/// <summary>
/// The delegate of the <see cref="PrePlayerCommand"/> event.
/// </summary>
/// <param name="e">The EventArgs for this event.</param>
public delegate void PrePlayerCommandD(PrePlayerCommandEventArgs e);
/// <summary>
/// Fired before a command is run.
/// </summary>
public static event PrePlayerCommandD PrePlayerCommand;
/// <summary>
/// The delegate of the <see cref="PostPlayerCommand"/> event.
/// </summary>
/// <param name="e">The EventArgs for this event.</param>
public delegate void PostPlayerCommandD(PostPlayerCommandEventArgs e);
/// <summary>
/// Fired after a command is run.
/// </summary>
public static event PostPlayerCommandD PostPlayerCommand;
/// <summary> /// <summary>
/// The delegate of the <see cref="PlayerChat"/> event. /// The delegate of the <see cref="PlayerChat"/> event.
/// </summary> /// </summary>
@ -449,6 +513,40 @@ namespace TShockAPI.Hooks
return playerCommandEventArgs.Handled; return playerCommandEventArgs.Handled;
} }
/// <summary>
/// Fires the <see cref="PrePlayerCommand"/> event.
/// </summary>
/// <param name="cmd">Command to be executed</param>
/// <param name="arguments">Command arguments</param>
/// <returns>True if the event has been handled.</returns>
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;
}
/// <summary>
/// Fires the <see cref="PostPlayerCommand"/> event.
/// </summary>
/// <param name="cmd">Executed command.</param>
/// <param name="arguments">Command arguments.</param>
/// <param name="handled">Is the command executed.</param>
public static void OnPostPlayerCommand(Command cmd, CommandArgs arguments, bool handled)
{
if (PostPlayerCommand == null)
return;
PostPlayerCommandEventArgs args = new PostPlayerCommandEventArgs(cmd, arguments, handled);
PostPlayerCommand(args);
}
/// <summary> /// <summary>
/// Fires the <see cref="PlayerPreLogin"/> event. /// Fires the <see cref="PlayerPreLogin"/> event.
/// </summary> /// </summary>

View file

@ -105,6 +105,8 @@ Use past tense when adding new entries; sign your name off when you add or chang
* Added a property `TSPlayer.Hostile`, which gets pvp player mode. (@AgaSpace) * Added a property `TSPlayer.Hostile`, which gets pvp player mode. (@AgaSpace)
* Fixed bug where when the `UseSqlLogs` config property is true, an empty log file would still get created. (@ZakFahey) * Fixed bug where when the `UseSqlLogs` config property is true, an empty log file would still get created. (@ZakFahey)
* Fixed typo in `/gbuff`. (@sgkoishi, #2955) * 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 ## TShock 5.2
* An additional option `pvpwithnoteam` is added at `PvPMode` to enable PVP with no team. (@CelestialAnarchy, #2617, @ATFGK) * An additional option `pvpwithnoteam` is added at `PvPMode` to enable PVP with no team. (@CelestialAnarchy, #2617, @ATFGK)