Hierarchical permissions?

So in my plugin, I do not wish to have every command have their own permission as the only option. I would like have permissions such as “plugin.user”, “plugin.mod”, or “plugin.owner”, while of course still supporting the individual permissions,to make it less work for server owners and being able to suggest permissions. I believe it is possible by just using .permission() when making a CommandSpec more than once, but I feel like there may be a better way. Any suggestions?

EDIT: Second question, sorry. If a player has the permission for a parent command do they have access to all of its child commands?

The answer to question 2 is yes. If a player has the permission myplugin.command1, they have both myplugin.command1.use and myplugin.command1.other. This is why the docs recommend adding a .use to the end of permissions where there would be other subpermissions.
.permission() is just an easy way for you not to have to do extra permission-checking. If you need to check permissions during command execution, use checkPermission() on the CommandContext object.
And lastly, if you have many permissions, or want some permissions to be enabled by default, you can use PermissionDescription. Consider this code:

PermissionService service = game.getServiceManager().provideUnchecked(PermissionService.class);
Optional<PermissionDescription.Builder> builder = service.newDescriptionBuilder(this);
if (builder.isPresent()) {
    builder.get().id("myplugin.command1.use").description(Text.of("Allows usage of /command1.")).assign(PermissionService.SUBJECTS_USER, true).register();
    service.newDescriptionBuilder(this).get().id("myplugin.command1.other").description(Text.of("Allows application of /command1 to other players.")).register();
    service.newDescriptionBuilder(this).get().id("myplugin.command1").assign(PermissionService.SUBJECTS_SYSTEM, true).assign(PermissionService.SUBJECTS_COMMAND_BLOCK, true).register();
}
1 Like