Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Requirement checkers

Emre Kara edited this page Jun 24, 2021 · 1 revision

What are requirement checkers?

In this system you can add requirement checks to all the commands you want. For example if you want to add "admin only" commands, you can create a requirement checker and add it to the methods or even the whole class. Every requirement checker will run in the main thread, so don't worry about using AltAsync.Do.

How to create a requirement checker?

The requirement checker is your own custom attribute which overrides the "CustomCommandRequirementBase" attribute.
So to create one, you need to create an attribute and implement the "CanExecute" method.

Example for admin level checker:

class NeedAdminLevelCheckerAttribute : CustomCommandRequirementBaseAttribute
{
    public int AdminLevelNeeded { get; set; }

    public NeedAdminLevelCheckerAttribute(int adminLevelNeeded) => AdminLevelNeeded = adminLevelNeeded;

    public override bool CanExecute(IPlayer player, CustomCommandInfo? info, ArraySegment<object?> methodArgs) 
    {
        var playerAdminLevel = player.GetData<int>("AdminLevel");
        return playerAdminLevel >= AdminLevelNeeded;
    }
}

How to add it to my commands?

You can now use this attribute either on a class or a method.
If you add it to a class, every method in that class will have this requirement checker.
You can also combine the requirement checkers, e.g. have methods with 10 checkers, no problem.

Example (class)

[NeedAdminLevelChecker(1)]
class SupporterCommands
{
    [CustomCommand("kick")]
    void Kick(IPlayer player, IPlayer target) { }

    [CustomCommand("spectate")]
    void Spectate(IPlayer player, IPlayer target) { }
}

Example (method)

class AllCommands
{
    [CustomCommand("kick")]
    [NeedAdminLevelChecker(1)]
    void Kick(IPlayer player, IPlayer target) { }

    [CustomCommand("ban")]
    [NeedAdminLevelChecker(2)]
    void Ban(IPlayer player, IPlayer target, int time) { }
}

Clone this wiki locally