Sub-commands

Discussion in 'Spigot Plugin Development' started by OPIA, May 14, 2016.

  1. Hello,

    I'm having a small problem with creating a sub-command to one of my main commands as shown in the code below, '/gg' sends a message and I would like it to have '/gg version' displays message about the version etc
    I am not an experienced coder, please don't assume I am.

    The subcommand works fine but when using '/gg version' it displays the main message and the version message how do i stop '/gg' from sending the main message when doing '/gg version' ?


    Code (Text):
     if (command.getLabel().equalsIgnoreCase("gg") && commandSender instanceof Player) {
           Player sender = (Player) commandSender;
           sender.sendMessage(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "This is the test main message");
           if (args.length == 1) {
             if (args[0].equalsIgnoreCase("version")) {
               sender.sendMessage(ChatColor.DARK_BLUE + "This is the current version message");
                  }
                }
      return true;
     
    If you don't get what i mean.
    Command imput: /gg
    Message: This is the main message

    Command imput: /gg version
    Message: This is the main message
    Message: This is the current version message



    Plugin information:
    Running on 1.9.x and 1.8.x
    If you need any more information about this just let me know.

    Questions:
    How do i stop /gg from sending the message when using a sub-command? (Maybe an error message?)
    How do i make multiple subcommands using this one command?

    It would be kindly appreciated to know where I am going wrong lol.
     
  2. Why not check if the length equals 0 and send the message if it is?
     
  3. Not entirely sure what you mean, could you maybe explain it? :)
     
  4. It's exactly what you are doing for the /gg version only check if the length is 0 then you send the message.
     
  5. Well, don't you see what's happening in the code you posted?

    You're sending
    Code (Java):
    sender.sendMessage(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "This is the test main message");
    before you check for arguments.

    Always remember: computers are super dumb and follow every instruction literally. You're telling it to send the main message and then check if an argument exists. :p
     
    • Like Like x 1
  6. Thank you for pointing this out ;-;
    I have a problem with my sub-command now, after inputting the command I get no message
    Did i miss something again? xd
     
  7. Code (Java):
     if (command.getLabel().equalsIgnoreCase("gg") && commandSender instanceof Player) {
           Player sender = (Player) commandSender;
           sender.sendMessage(ChatColor.DARK_PURPLE + "" + ChatColor.BOLD + "This is the test main message");
           if (args.length == 1) {
             if (args[0].equalsIgnoreCase("version")) {
               sender.sendMessage(ChatColor.DARK_BLUE + "This is the current version message");
                  }
                }
      return true;
    The code is working. It does exactly like you told it to.

    Send the main message first.
    Done.

    Check if it has 1 argument.
    True -> |
    Check if first arg is version
    True -> |
    Send version message.
    |
    False ->
    Move on.