Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.
Emre Kara edited this page Jul 16, 2021 · 4 revisions

Are there any events?

Yes:

  • BeforeCommandExecute
  • AfterCommandExecute

All events are in CustomCommandsSystem.Integration.
Check their comments for more info.

What could I do with events?

Do things you would want to do before or after every command execute.
For example: Logging

Example

The check in BeforeCommandExecute in this example is not a good way to check for a requirement.
Better use Requirement checkers instead.

using CustomCommandsSystem.Integration;

public void AddEvents() 
{
    Events.BeforeCommandExecute += BeforeCommandExecute;
    Events.AfterCommandExecute += AfterCommandExecute;
}

private void BeforeCommandExecute(IPlayer player, UserInputData userInputData, object?[] args, CancelEventArgs cancelEventArgs) 
{
    // If we have only one kick method with one parameter and BeforeCommandExecute is triggered
    // the entry in args will be valid and must exist. So there has to be a player entry in args[0]
    if (userInputData.Command == "kick" && args[0] is IPlayer { Name: "Bonus" }) 
    {
        player.SendChatMessage("You can't kick Bonus!!");
        cancelEventArgs.Cancel = true;
        return;
    }
    Console.WriteLine($"{player.Name} is executing command");
}

private void AfterCommandExecute(IPlayer player, UserInputData userInputData, object?[] args) 
{
    Console.WriteLine($"{player.Name} executed command {userInputData.Command}. The text the player wrote: \"{userInputData.Message}\"");
}

Clone this wiki locally