Multiple command arguments

Discussion in 'Plugin Development' started by TryHardCoder, Jan 5, 2015.

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

    TryHardCoder

    So i've been having this problem that i can only use one argument after my command, it will not change however i tried.. Here's my code:
    (this is a requested pl)

    Code:
    package main;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Mainclass extends JavaPlugin {
    
        String desc = ChatColor.AQUA + "";
        String camd = ChatColor.BLUE + "";
    
        boolean Enabled = false;
    
        public void Start() {
            System.out.println("Enabled");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
    
            Player p = (Player) sender;
    
            if (cmd.getName().equalsIgnoreCase("cc")) {
                if (p.hasPermission("CC.USE")) {
                    if (args.length == 0) {
                        p.sendMessage(ChatColor.GOLD + "~-=Ccraft Commandline=-~");
                        p.sendMessage(camd + "/cc enable" + desc
                                + " enables commandline");
                        p.sendMessage(camd + "/cc disable" + desc
                                + " disables commandline");
                        p.sendMessage(camd + "/cc kick / <Player>" + desc
                                + " kick a player by cc");
                    }
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("enable")) {
                            if (Enabled == false) {
                                Enabled = true;
                                p.sendMessage("You enabled your commandline "
                                        + p.getName());
    
                                System.out.println(p.getName()
                                        + " Enabled his commandline");
                                return false;
                            } else {
                                p.sendMessage(ChatColor.DARK_RED
                                        + "Your commandline was already enabled!");
                            }
    
                        }
                        if (args[0].equalsIgnoreCase("disable")) {
                            if (Enabled == true) {
                                Enabled = false;
                                p.sendMessage("You disabled your commandline "
                                        + p.getName());
    
                                System.out.println(p.getName()
                                        + " disabled his commandline");
                                return false;
                            } else {
                                p.sendMessage(ChatColor.DARK_RED
                                        + "Your commandline was already disabled!");
                            }
                        }
                        if (args[0].equalsIgnoreCase("kick")) {
                            if(Enabled == false) {
                                p.sendMessage("Commandline not enabled!");
                            } else {
                            if(args.length == 1) {
                                p.kickPlayer("You kicked yourself..");
                            }
                            if(args.length == 2) { // This should be right? right? >> problem on this
                                @SuppressWarnings("deprecation")
                                Player TP = Bukkit.getServer().getPlayer(args[1]);
                                TP.kickPlayer("Kicked by: " + p.getName());
                                p.sendMessage("Player has been kicked"); // until this <<
                            }
                            }
                        }
                    }
                }
    
            }
            return false;
    
        }
    
    }
    
    maybe you can see what i did wrong at that place
     
  2. Offline

    TacticalQuiver

    I dont see what you did wrong.
     
  3. Offline

    SuperOriginal

    I'm not exactly sure what you mean, but here's what I think:

    If your args length is greater than 1, nothing will happen, because your if statement only passes if the arg length is EXACTLY 1. The solution to this would be to check if args length is GREATER THAN OR EQUAL TO 1.

    All the if statements inside of your "kick" block are useless because if args length isn't 1, it wouldn't have even reached there.
     
  4. Offline

    spy_1134

    You check to see if the length of args is 1 before entering the block of if statements. Change that to greater than or equal to 1.
    That keeps it from reaching the later parts of your code.

    EDIT: An example.
    Code:
    if(args.length == 1) {
        if(args[0] == "kick") {
            if(args.length == 2) {
                // Will NEVER be reached due to this code only being run when the surrounding if is satisfied.
            }
        }
    }
    
    if(args.length >= 1) {
        if(args[0] == "kick") {
            if(args.length == 2) {
                // This area can now be reached when args is 2.
            }
        }
    }
     
    Last edited: Jan 5, 2015
  5. Offline

    BitBoxGaming

    Also, you are casting the commandSender to a Player, without checking. This could lead to some errors if you are sending these commands from the console.
     
  6. Offline

    TryHardCoder

  7. @TryHardCoder Additionally, you have a "Start()" method. I'm not sure why, but the method doesn't really do anything, it doesn't follow the Java naming convention, and you never call it. You might want to just get rid of it :)
     
Thread Status:
Not open for further replies.

Share This Page