Subcommands and their Permissions

Discussion in 'Spigot Plugin Development' started by Bidspark, May 16, 2019.

  1. Many plugins such as factions use a header command like /f. When using this command, you can also use subcommands and each of those have a permission tied to it (e.g. /f claim, /f create [group]). I'm wondering how this is done since I am interested in doing something similar with the command structure.
  2. Code (Java):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!cmd.getName().equalsIgnoreCase("command")) return false;
        if (args.length == 0) return true;
        if (args[0].equalsIgnoreCase("1") && sender.hasPermission("permission.1")) {
            // Code
        } else {return true;}
        if (args[0].equalsIgnoreCase("2") && sender.hasPermission("permission.2")) {
            // Code
        } else {return true;}
        return true;
    Of course this is very simple and you could make classes for each, etc.
  3. How would the permissions in the plugin.yml be written for this?
  4. No need for permissions in the plugin.yml if you use this.
  5. Is there another way of writing this or is this pretty much it?
  6. Eventhough this has been solved, this would be the way to do it.
    1. Create an abstract class; SubCommand
    2. Add methods to identify it easily
    3. Create a collection containing all SubCommands in the main command class
    4. Get the appropriate SubCommand
    5. Etc
    I do recommend specifying the permissions in the plugin.yml, I'm sure it will come handy for users and perhaps even yourself.
  7. @Bidspark Seems you opened the thread again? (I tried to answer before but it was locked)
    Note: You can't choose the permission for a "sub command", only the "main command". (You probably know but it doesn't seem Bidspark knows)
    Other than that i said:
    What i meant was what you said @ZYSEA.
  8. Wrong. You can make a permission for each subcommand. When you create the abstract class for your subcommand, you can add a permission, and when executing that specific subcommand, you can check for that permission.
  9. I know that, i was quoting this:
    I meant that it wasn't possible to add a "sub command" permission in the plugin.yml.
  10. You can simply do
    Code (YAML):
    : description if you'd like
    #11 ZYSEA, May 16, 2019
    Last edited: May 16, 2019
  11. Oeps, I've edited my post. It's correct now.
  12. I've seen it in the factions plugin.yml
  13. Thank you everyone for your replies!
  14. I'm curious how the command section in the plugin.yml would look like and how you would write the java method. I'm new to programming with spigot (but not new to programming java) every bit helps me learn more.
  15. BUMP

    I can't seem to register my commands because they aren't in the plugin.yml do I get around this?
  16. You don't need to register sub commands. If it doesn't work, then there is a problem with your sub command handler.

Share This Page