Skip to content

Command API: Permissions

Lorenzo Rutayisire edited this page Jun 13, 2020 · 1 revision

Uppercore generates a permissions tree based on the commands hierarchy.

For example, saying your plugin's name is Quake and you have created these commands:

/quake <command>
/quake create <arena>
/quake version
/quake join <arena>
/quake show arenas

The generated permissions will be:

quake.quake
quake.quake.*
quake.quake.create
quake.quake.version
quake.quake.join
quake.quake.show.*
quake.quake.show.arenas

By default, every permission won't be available to common players, just to operators.

Every node command automatically appends the "all" permission: the one that ends with *. Having this permission permits to the owner to access all of the sub-commands in any case.

How to specify permissions default?

The most common thing you'd probably need is to have commands available to all players. Using the example above, you'd probably want to make /quake join available. Depending on which approach to the command-api you're using, there are two ways of doing it:

Class-based

Within the command constructor add the following line:

public JoinArenaCommand() {
    super("join");

    setDescription("Creates a new arena with the given name.");
    setSenderType(SenderType.PLAYER);
    addAliases("c", "make");

    // Sets the leaf permission for the current command.
    setPermissionPortion(new Permission("create", PermissionUser.AVAILABLE.get())); // <---
}
Function-based
@AsCommand(
        description = "Joins an arena.",
        sender = SenderType.PLAYER
)
@WithPermission( // <---
       user = PermissionUser.AVAILABLE
)
public void join(Player player, String arena) {
    // (...)
}

How do I set an absolute permission that doesn't rely on the commands tree?

Class-based
// Still inside of the Command class's constructor.
setPermissionCompleter(PermissionCompleter.NONE);
Function-based
@WithPermission(
    value = "my.absolute.permission",
    completer = PermissionCompleter.NONE
)
// Command's function...

Test it out!

After writing your commands is absolutely important that you test if permissions are set the way you want. Within every root command - the command that starts with / and the one registered with CommandRegistry - Uppercore adds a sub-command to print in a markdown file the commands-permissions table.

Back to the /quake command, the sub-command will be:

/quake printmd