Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New two hooks PrePlayerCommand and PostPlayerCommand #2960

Open
wants to merge 11 commits into
base: general-devel
Choose a base branch
from
20 changes: 15 additions & 5 deletions TShockAPI/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,29 @@ public Command(CommandDelegate cmd, params string[] names)
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;

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<string> parms)
{
return Run(new CommandArgs(msg, silent, ply, parms));
}

public bool Run(string msg, TSPlayer ply, List<string> parms)
{
return Run(msg, false, ply, parms);
Expand Down Expand Up @@ -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;
Expand Down
98 changes: 98 additions & 0 deletions TShockAPI/Hooks/PlayerHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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/>.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using TShockAPI.DB;
Expand Down Expand Up @@ -119,6 +120,49 @@ public class PlayerCommandEventArgs : HandledEventArgs
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>
/// EventArgs used for the <see cref="PlayerHooks.PlayerChat"/> event.
/// </summary>
Expand Down Expand Up @@ -343,6 +387,26 @@ public static class PlayerHooks
/// </summary>
public static event PlayerCommandD PlayerCommand;
Arthri marked this conversation as resolved.
Show resolved Hide resolved

/// <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>
/// The delegate of the <see cref="PlayerChat"/> event.
/// </summary>
Expand Down Expand Up @@ -449,6 +513,40 @@ public static bool OnPlayerCommand(TSPlayer player, string cmdName, string cmdTe
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>
/// Fires the <see cref="PlayerPreLogin"/> event.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down