commands icon indicating copy to clipboard operation
commands copied to clipboard

CommandAlias and Subcommand on inner classes leads to unexpected behavior

Open dumptruckman opened this issue 6 years ago • 7 comments

I have the following setup:

@CommandAlias("main")
public class MainCommand extends BaseCommand {
    
    @Subcommand("setup")
    public void setup() { /* stuff */ }
   
    @Subcommand("sub")
    @CommandAlias("sc")
    public class SubCommand extends BaseCommand {
        
        @Subcommand("create")
        public void create() { /* stuff */ }
    }
}

My expectation is that the following commands exist:

/main setup
/main sub create
/sc create

However, the actual commands end up being:

/main setup
/main sub create
/sc sub create

Is this the intended behavior?

dumptruckman avatar Jun 24 '19 03:06 dumptruckman

do you have @CommandAlias("main") on your MainCommand class?

MiniDigger avatar Jun 24 '19 05:06 MiniDigger

Well yeah this is intended behavior. So that you can break subcommands into its own root class file, do CA/SC on it and define all commands in that file is for example /root sub

not sure if theres a clean way to achieve both

aikar avatar Jun 24 '19 14:06 aikar

@MiniDigger yes. I updated the example.

@aikar Are you saying the desired behavior is not possible?

dumptruckman avatar Jun 26 '19 02:06 dumptruckman

@dumptruckman you'll need another inner class sadly.

@CommandAlias("main")
public class MainCommand extends BaseCommand {
    
    @Subcommand("setup")
    public void setup() { /* stuff */ }
   
    @Subcommand("sub")
    public class SubCommand extends BaseCommand {

    @CommandAlias("sc")
    public class SubCommandInner extends BaseCommand {   
        @Subcommand("create")
        public void create() { /* stuff */ }
    }
    }
}

Your desired syntax doesn't make sense on a root class, what would this be?

@CommandAlias("foo")
@Subcommand("bar")
@CommandPermission("command.foo.bar")
public class FooCommand extends BaseCommand {
    @Subcommand("baz")
    public void onBaz() {}
}

As it stands now, they are combined. /foo bar baz

I use this in my own commands to group sections of subcommands into their own files.

Alternatively, PR a feature to do like manager.registerCommandAlias("sc", "main sub");

aikar avatar Jun 26 '19 15:06 aikar

@aikar I tried the extra inner class thing but ended up with the same result. I'm not sure why you would attempt this on a root class.

dumptruckman avatar Jun 26 '19 15:06 dumptruckman

@dumptruckman because how it works currently DOES work on a root class and behaves consistently on a subclass. Ultimately, the syntax combination is already in use for a feature for ACF and the feature it currently provides is more consistent than your proposed idea, so not going to consider a breaking change there.

So to achieve what you are looking for, you need a different solution.

aikar avatar Jun 26 '19 15:06 aikar

Ahhh, I see. Yes, I also considered a boolean param on the CommandAlias annotation as another way to deal with this but I couldn't think of what to name it. (Or just add another annotation to attain this desired behavior.)

dumptruckman avatar Jun 26 '19 15:06 dumptruckman