Solved Multiple args command doesn't work

Discussion in 'Plugin Development' started by SloopyGames, Aug 16, 2019.

Thread Status:
Not open for further replies.
  1. Hey, i'm creating a simple joinmessage plugin. But i need some help with the command to edit the messages in the config.

    Code:
            if(cmd.getName().equalsIgnoreCase("advancedjoin")) {
                if(args.length == 0) {
                    if (main.getConfig().getBoolean("Commands.AdvancedJoin.Need permission")) {
                        // check for perms
                        // ONLY if above is true
                        if (p.hasPermission("advancedjoin.commands.advancedjoin")) {
                            p.sendMessage(Utils.format("&cAdvanced Join &6plugin information"));
                            p.sendMessage(Utils.format("&6Plugin version: &c" + pdfFile.getVersion() + "&6!"));
                            p.sendMessage(Utils.format("&6Plugin created by &cSloopyGamesYT"));
                        } else {
                            // if player has no perms
                            p.sendMessage(Utils.noperms(""));
                        }
                    } else {
                        // ONLY if above is false
                        p.sendMessage(Utils.format("&cAdvanced Join &6plugin information"));
                        p.sendMessage(Utils.format("&6Plugin version: " + pdfFile.getVersion()));
                        p.sendMessage(Utils.format("&6Plugin created by &cSloopyGamesYT"));
                    }
                } if (args.length == 0 && args[0] == "setfirstjoin") {
                    p.sendMessage("test");
                }
    When i try to run the '/advancedjoin setfirstjoin', it says nothing, and does nothing.

    Thanks,
    Sloopy
     
    Last edited: Aug 16, 2019
  2. Online

    timtower Administrator Administrator Moderator

    @SloopyGames You need to compare strings with .equals,
    args.length == 0 && args[0] == "setfirstjoin"
    This statement will never be true.
     
  3. Thank you!

    Verstuurd vanaf mijn SM-A705FN met Tapatalk
     
  4. Offline

    Strahan

    Plus even if you did it right, if (args.length == 0 && args[0].equalsIgnoreCase("setfirstjoin")) will cause a crash if they didn't pass any arguments because your logic says "if there aren't any arguments and if the first arg is setfirstjoin".

    Personally, when I do multiarg commands I do it like
    Code:
    if (args.length == 0) {
      // show global help
      return true;
    }
    
    switch (args[0].toLowerCase()) {
    case "setfirstjoin":
      // do stuff
      break;
    
    case "somecommand":
      if (args.length < 2) {
        // show contextual help
        return true;
      }
    
      // do stuff
      break;
    
    default:
      // send "invalid command" error or show global help
    }
    return true;
    This would be if /advancedjoin setfirstjoin has no args and I have a command /advancedjoin somecommand that requires two arguments to work.

    PS if /advancedjoin is the only command this plugin has, or if this class is specifically set to handle execution of /advancedjoin then you don't need to worry with checking the command's name as it will only ever run this code for that command.
     
    SloopyGames likes this.
Thread Status:
Not open for further replies.

Share This Page