Solved Sub Commands

Discussion in 'Plugin Development' started by Pizza371, Jul 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline


    As everyone knows, sub commands within bukkit are a nightmare, well for me anyway.
    Can anyone post what they think the most efficient and easy way to make sub-commands are (like, a big bunch of them).

    Or just comment your favourite way of dealing with sub commands in quantity ;P.

  2. Offline


    I would detect the subCommand and move the code to execute that subCommand to a different class just to keep things cleaner

    public boolean onCommand(CommandSender sender, Command command, String s, String[] strings) {
    String subCommand = strings[0];
    if(subCommand.equalsIgnoreCase("<your command>")) {
        new subCommandClass(sender, strings);

    PS. I have done this with 22 sub-commands
  3. Offline



    I assume you're handling commands within a CommandExecutor. This is how I handle my subcommands:

    1. @Override
    2. public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
    4. if (args.length < 1){
    5. HandleBaseCommand(sender);
    6. return false;
    7. }
    9. if (args[0].equalsIgnoreCase("create")) CreateKit(sender, Utils.Trim(args));
    10. else if (args[0].equalsIgnoreCase("edit")) EditKit(sender, Utils.Trim(args));
    11. else if (args[0].equalsIgnoreCase("remove")) RemoveKit(sender, Utils.Trim(args));
    12. else SpawnKit(sender, args);
    14. return false;
    15. }
    17. private void HandleBaseCommand(CommandSender sender){
    18. //Code for base command
    19. }
    21. private void CreateKit(CommandSender sender, String[] args){
    22. //Code to handle create subcommand
    23. }
    25. private void EditKit(CommandSender sender, String[] args){
    26. //Code to handle edit subcommand
    27. }
    29. private void RemoveKit(CommandSender sender, String[] args){
    30. //Code to handle remove subcommand
    31. }
    33. private void SpawnKit(CommandSender sender, String[] args){
    34. //Code to handle spawn subcommand
    35. }


    1. public static String[] Trim(String[] args){
    2. return Arrays.copyOfRange(args, 1, args.length);
    3. }

    The Trim method removes the first element from an array. The reason I do this is so that I can handle subsubcommands within my subcommand's args starting from an index of 0.

    You could also make each subcommand method's return value a boolean, so that your onCommand method will return whether the command was valid or not.
  4. Offline


    To check for subcommands, i'd do something like this:

        case "subcommand1":
        case "subcommand2":
    But that's my way of doing it. You could also set up a bunch of if/else-statements, but it looks so much cleaner with a switch :)
  5. Offline


    For my plugin system, I've created various different types of commands. One of them is a command specifically for handling commands with subcommands (I call them meta commands in my plugin).

    If you'd like to check them out, you can do so here.

    Its pretty in depth, and I've actually got a writeup on how my stuff handles commands here on my devblog (not a lot of code though, mind you sorry!)

    Honestly, I'm fairly proud of it, and if you were interested, I'd love to explain more :D
  6. Offline


    I semi get what you mean, althought I dont know what you'd do with the object you just instiantated
    Thanks! Although I prefer not to have a method for every command, personal preference and there's still alot of if else's which can get me confused.

    Always good to hear how other people d it though ;)
    but that wouldn't work with multiple arguments? unless you ran a switrch inside of another?
    Sounds interesting, looks like a quite complex command handler. the ruleset and subcommands look promising.
    I was really liking the annotation based command handlers that were posted everywhere, but then when it came to dynamic arguments I hit a wall.

    I am interested in this.

    Thanks all!
  7. Offline



    Yeah. Or honestly, i'd use if-statements after the subcommand. Mostly because it looks very clean and good.

    Remember, switches are like if-statements, they got pretty much the same functionality.
  8. Offline



    It's still a lot lot neater and easier to manage than handling all of your subcommands within a collection of nested if-statements. I used to do that a lot and I found that when I wanted to expand my code by adding additional subcommands, it became messy and hard to manage. Take a look at this for example.

    In the end it all comes down to personal preference; decide the easiest and best way for you to personally feel comfortable with how you manage your subcommands.
  9. Offline


    Oh my eyes!
    You just create it and pass the args that you need for the command. Then you will have a method that you run in the new class. That is were all the logic goes. Keeps things VERY clean and no scrolling forever.
    Dragonphase likes this.
  10. Offline


    Another way to do it is using a "if" style switch which will allow users who run java 6 to run your plugin also

    2. String[] arg = args;
    3. if(arg[0].equalsIgnoreCase("fish")){
    4. RunFish()
    5. }
    6. if(args[0].equalsIgnoreCase("dog")){
    7. RunDog()
    8. }

    -Some spelling may be off right now as I'm on a different PC and don't have my Physical plugin code in-front on me to show you a real example of this
  11. Offline


    Toyz People are still using Java 6? :p

    We are in Java 8 now D:
  12. Offline


    Java 6 is still one of the most powerful and widely used versions of java out there android just went to 7. the reason alot of people don't use 8 is cause of the some major changes to the code and if your linux most use OpenJDK which is still at 7
  13. Offline


    Why do you create a new instance of args? You could just use args.
  14. Offline


    Just a quick off the head example I do that cause it's just my method cause I have my own CommandBase command that extend's CommandBase so i have args as a global in the base
  15. Offline


    Thanks all who have helped me, I have created my own small command handler and it fits everything I need.
    CorrieKay likes this.
Thread Status:
Not open for further replies.

Share This Page