Solved Help with Commands

Discussion in 'Plugin Development' started by shadow5353, Mar 31, 2014.

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

    shadow5353

    I am trying to make a plugin with a few SubCommands to it.

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(!(sender instanceof Player)){
                sender.sendMessage(ChatColor.DARK_RED + "Only Players can use landmine");
                return true;
            }
           
            Player p = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("landmine")){
                if(p.hasPermission("landmine.use")){
                    p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.GOLD + "/landmine give" + ChatColor.BLACK + " : " + ChatColor.YELLOW + "Gives you a landmine you can use");
                    if(args.length == 0){
                        if(args[0].equalsIgnoreCase("give")){
                            p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.GREEN + "You ");
                            return true;
                        }
                    }
                }else {
                    p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.RED + "You don't have permission to do that!");
                    return true;
                }
            }
            return true;
        }
    What is it I do wrong?
     
  2. Offline

    TopTobster5

    Basicly, you are checking if the list of arguments is empty, and then trying to get the first result from it, which of course does not exist. You basicly need to change the 'if (args.length == 0)' to 'if (args.length == 1)'
     
  3. Offline

    shadow5353

    TopTobster5 sorry for late answer, but now I have changed it to 1 instead of 0. But what I am doing wrong this time?

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
           
            if (!(sender instanceof Player)) {
                    sender.sendMessage(ChatColor.AQUA + "The console ran the test command! Good job!");
                    return true;
            }
         
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("landmine")) {
                if(p.hasPermission("landmine.use")){
                    p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.GOLD + "/landmine give" + ChatColor.BLACK + " : " + ChatColor.YELLOW + "This will give you " + getConfig().getString("landmines") + " landmines");
                   
                    if(p.hasPermission("landmine.admin")){
                        p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.GOLD + "/landmine reload" + ChatColor.BLACK + " : " + ChatColor.YELLOW + "Reload the plugins config.yml");
                        return true;
                        }
                    if(p.hasPermission("landmine.use")){
                        if(args.length == 1){
                            if(args[1].equalsIgnoreCase("give")){
                                p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.GOLD +"test");
                                return true;
                            }
                        }
                    }
                    }else if(!p.hasPermission("landmine.use")){
                        p.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "LandMine" + ChatColor.BLACK + "] " + ChatColor.RED + "You don't have permission!");
                        return true;
                    }
                }
            return true;
            }
     
  4. Offline

    TopTobster5

    You still need to keep the 'args[1]' as 'args [0]'. It seems strange at first but it it just how it works. shadow5353
     
  5. Offline

    shadow5353

Thread Status:
Not open for further replies.

Share This Page