Command Arguments

Discussion in 'Resources' started by x_Jake_s, Jul 23, 2016.

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

    x_Jake_s

    Command Arguments are useful for big plugins as there a way to extend a command.

    Basic Arg Example

    Code:
    if (command.getName().equalsIgnoreCase("test") {
            if (args.length == 0) {
                player.sendMessage("");
            }
           if (args.length == 1) {
                    if (args[0].equalsIgnoreCase("reload")) {
                        player.sendMessage(plugin.prefix + "This will reload the plugin");
                    }
                    if (args[0].equalsIgnoreCase("word")) {
                        player.sendMessage(plugin.prefix + "/test word | Missing Arguments");
                    }
                    if (args[0].equalsIgnoreCase("block")) {
                        player.sendMessage(plugin.prefix + "/test block | Missing Arguments");
                    }
            }
            if (args.length == 2) {
                if (args[0].equalsIgnoreCase("word")) {
                    if (args[1].equalsIgnoreCase("list")) {
                        player.sendMessage("this would list words");
                        return true;
                    }
                    if (args[1].equals("add")) {
                        player.sendMessage("this would add words");
                        return true;
                    }
                    if (args[1].equals("remove")) {
                        player.sendMessage("this would remove words");
                        return true;
                    }
                    if (args[1].equals("on")) {
                        player.sendMessage("this would turn on words");
                        return true;
                    }
                    if (args[1].equals("off")) {
                        player.sendMessage("this would turn off words");
                        return true;
                    }
                    //if the player typed in a random argument:
                    player.sendMessage("Wrong argument!");
                    player.sendMessage("the argument you have written is: " + args[1]);
                    return false;
     
  2. There's a ) missing.
    Also, how is this a util? Because I can see nothing new (no new features) in it
     
  3. @x_Jake_s Look into using else ifs, the first arg cannot be 3 things at the same time. And how is this a until?!?
     
  4. Offline

    ChipDev

    This is simple Bukkit / Java knowledge, and it is not an external util because you don't have to implement anybody else's code. This same thing with more explanation could be found on the Bukkit tutorial / wiki.
    EDIT: @bwfcwalshy and @FisheyLP ninja'd me by about 12 hours, had it open overnight.
     
  5. Offline

    Zombie_Striker

    @ChipDev
    Always remember: Refresh the page if it has bee open for more than ten minutes.

    @x_Jake_s Again, remember to use Else statements. Also,
    All this does is send a blank line to the player. Unless this was intended, this is not very helpful. You could return if you don't want to do anything, or just simply not check for if there are no args.
     
  6. Offline

    Ragnarok_

  7. Offline

    Zombie_Striker

    @Ragnarok_
    If your problem has been solved, mark this thread as solved.
     
  8. Offline

    Ragnarok_

  9. Offline

    PhantomUnicorns

    @Zombie_Striker This is a 'util' it wasn't a thread for a problem
    @x_Jake_s You should at least update the code to be more efficient, like this:
    Code:
        @Override
        public boolean onCommand (CommandSender sender, Command command, String label, String[] args) {
            String pluginPrefix = "<MyPlugin> ";
            if (command.getName().equalsIgnoreCase("test")) { // Testing if it's the command you want, if you only have one command registered you don't need this
                if (args.length == 0) { // Tests how many arguments they added (not including the base command)
                    sender.sendMessage(""); // Recommended help page h
                } else if (args.length == 1) { // Tests how many arguments they added (not including the base command)
                    if (args[0].equalsIgnoreCase("reload")) { // Testing if the one arg equals reload
                        sender.sendMessage(pluginPrefix + "This will reload the plugin");
                    }else if (args[0].equalsIgnoreCase("word")) { // Testing if the one arg equals word
                        sender.sendMessage(pluginPrefix + "/test word | Missing Arguments");
                    }else if (args[0].equalsIgnoreCase("block")) { // Testing if the one arg equals block
                        sender.sendMessage(pluginPrefix + "/test block | Missing Arguments");
                    }else {
                        // if the player typed in a random argument:
                        sender.sendMessage(pluginPrefix + "Wrong argument!");
                        sender.sendMessage(pluginPrefix + "the argument you have written is: " + args[1]);
                    }
                } else if (args.length == 2) {
                    if (args[0].equalsIgnoreCase("word")) {
                        if (args[1].equalsIgnoreCase("list")) {
                            sender.sendMessage(pluginPrefix + "This would list words");
                        }else if (args[1].equals("add")) {
                            sender.sendMessage(pluginPrefix + "This would add words");
                        }else if (args[1].equals("remove")) {
                            sender.sendMessage(pluginPrefix + "This would remove words");
                        }else if (args[1].equals("on")) {
                            sender.sendMessage(pluginPrefix + "This would turn on words");
                        }else if (args[1].equals("off")) {
                            sender.sendMessage(pluginPrefix + "This would turn off words");
                        }else {
                            // if the player typed in a random argument:
                            sender.sendMessage(pluginPrefix + "Wrong argument!");
                            sender.sendMessage(pluginPrefix + "The argument you have written is: " + args[1]);
                        }
                    }
                }
                return true;
            }
    }
    
    
    2nd Post:
    @x_Jake_s your sig. is wrong, there is more then 5 different styles that people do for coding...
     
    Last edited: Oct 27, 2016
Thread Status:
Not open for further replies.

Share This Page