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

Conversation

AgaSpace
Copy link
Contributor

@AgaSpace AgaSpace commented Jun 15, 2023

There is a hook PlayerHooks.PlayerCommand which is called before using a command. In my personal opinion, this hook is obsolete because it could be used before calling a command (cmd.Run in HandleCommand). This would make it easier to work with commands because you wouldn't have to look for a command that might not exist in Commands.ChatCommands. That's why I added the PlayerHooks.PrePlayerCommand hook.
Also, the PlayerCommand hook does not implement the ability to handle commands after they are used, so I added the PostPlayerCommand hook.

If you would like to add "invisible" commands, which can be done with PlayerCommand, we already have a similar alternative (TSPlayer.AwaitingResponse).

Now some examples of how to use hooks:

PlayerHooks.PrePlayerCommand += (args) =>
{
	if (args.Command.Names.Contains("who"))
	{
		args.Arguments.Player.SendInfoMessage("Staff Online: {0}",
			string.Join(", ", TShock.Players.Where(i => i?.Active == true && i.HasPermission("staffonline"))));
	}
};
PlayerHooks.PostPlayerCommand += (args) =>
{
	if (args.Command.Names.Contains("who"))
	{
		int somePlayersInDimensions = 0;
		args.Arguments.Player.SendInfoMessage("There are {0} players in dimensions.",
			somePlayersInDimensions);
	}
};
PlayerHooks.PrePlayerCommand += (args) =>
{
	if (args.Command.Names.Contains("group"))
	{
		List<string> parameters = args.Arguments.Parameters;
		if (parameters.Count >= 2 && parameters[1].ToLower() == "owner")
		{
			TSPlayer player = args.Arguments.Player;
			player.SendErrorMessage("I'm sorry, but we have a monarchy. You are now banned.");

			player.SilentKickInProgress = true;
			player.Disconnect("You are banned!\n(but that's a joke)");
			args.Handled = true;
		}
	}
};

@sgkoishi
Copy link
Contributor

If there are already two /who commands, using this hook will result in two Staff Online: a, b, c or There are 5 players in dimensions; it's understandable for this hook but not quite expected for the actual use case 🤔

@AgaSpace
Copy link
Contributor Author

AgaSpace commented Jun 15, 2023

If there are already two /who commands, using this hook will result in two Staff Online: a, b, c or There are 5 players in dimensions; it's understandable for this hook but not quite expected for the actual use case 🤔

You can get the command you need using Commands.ChatCommands.Find, but I was too lazy to describe it :p

Although yes, you're right. It was inappropriate to include this example in the description of my changes.

@AgaSpace
Copy link
Contributor Author

If there are already two /who commands, using this hook will result in two Staff Online: a, b, c or There are 5 players in dimensions; it's understandable for this hook but not quite expected for the actual use case 🤔

Then how about marking the old hook (PlayerCommand) as obsolete?

@AgaSpace
Copy link
Contributor Author

@sgkoishi Is everything correct now?

Comment on lines 157 to 160
/// <summary>
/// Is the command executed.
/// </summary>
public bool Handled { get; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this class should inherit from HandledEventArgs instead of redefining Handled

@@ -341,8 +389,29 @@ public static class PlayerHooks
/// <summary>
/// Fired by players when using a command.
/// </summary>
[Obsolete("There is an alternative to PlayerHooks.PrePlayerCommand")]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The obsolete message says PlayerCommand is an alternative, rather than use PrePlayerCommand

TShockAPI/Hooks/PlayerHooks.cs Outdated Show resolved Hide resolved
TShockAPI/Hooks/PlayerHooks.cs Outdated Show resolved Hide resolved
@AgaSpace AgaSpace requested a review from Arthri June 15, 2023 09:04
Arthri
Arthri previously approved these changes Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants