Pryaxis / TShock

☕️⚡️TShock provides Terraria servers with server-side characters, anti-cheat, and community management tools.
GNU General Public License v3.0
2.42k stars 378 forks source link

New two hooks PrePlayerCommand and PostPlayerCommand #2960

Open AgaSpace opened 1 year ago

AgaSpace commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@sgkoishi Is everything correct now?