args

Discussion in 'Plugin Development' started by stamline, Mar 2, 2016.

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

    stamline

    Code:
        public void addFriend(Player player, Player target){
            List<String> list = this.getConfig().getStringList(player.getUniqueId().toString() + ".Friends");
            list.add(target.getUniqueId().toString());
            this.getConfig().set(player.getUniqueId() + ".Friends", list);
            this.saveConfig();
            player.sendMessage(ChatColor.GREEN + target.getDisplayName() + "is now your friend.");
        }
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("friend")) {
                if(args[0].equalsIgnoreCase("add")){
                    if(sender instanceof Player){
                        Player player = (Player) sender;
                        if(!(args.length > 1)){
                            Player target = (Bukkit.getServer().getPlayer(args[1]));
                            if(target !=null){
                                addFriend(player, target);
                            }else{
                                player.sendMessage(ChatColor.RED + "Player must be online to add as friend");
                            }
                        }else{
                            player.sendMessage(ChatColor.RED + "Use /friend add <friends name>");
                        }
                    }else{
                        sender.sendMessage(ChatColor.RED + "This must be executed by a player");
                    }
                }else if(args[0].equalsIgnoreCase("remove")){
                    if(sender instanceof Player){
                        Player player = (Player) sender;
                        if(!(args.length > 1)){
                            player.sendMessage("hello");
                            //if is in list
                        }
                    }else{
                        sender.sendMessage(ChatColor.RED + "This must be executed by a player");
                    }
                }else{
                    sender.sendMessage(ChatColor.RED + "Use /friend <add/remove>");
                }
            }
            return false;
        }
    In the console it said its wrong with my arguments, line 35. Am im not doing it right.
     
  2. Offline

    HoeMC

    I'm guessing it says line 35 in the stack trace and it's not line 35 in the code above. If so, you need to post the full class or tell us which line it corresponds to.
     
  3. Offline

    Evonoucono

    @stamline you should probably check if args[0] even exists... Seriously this is basic stuff!
     
    Zombie_Striker likes this.
  4. Offline

    mine-care

    @Evonoucono Exactly.
    You should check if the array "args" contains any elements in it before trying to access one of them.
     
  5. Offline

    teej107

    @stamline You should check if the array length even goes up to the 0 index.
     
Thread Status:
Not open for further replies.

Share This Page