dimanche 3 décembre 2017

if statement won't check through my array list

I was trying not to resort to posting on the forums but everything I've done will not make this work. What I'm trying to do is if an ArrayList doesn't contain a players name then it will add them to the ArrayList, but it's skipping over that and just removing them from the list they were never added to. Ultimate goal out of this plugin is if they are in god mode they can't be hurt. Command Class:

    public static List<String> playerList = new ArrayList<String>();

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String str, String[] args) {
        Player player = (Player) sender;
        String p = player.getName();
        List<String> isGod = new ArrayList<String>();

        if (cmd.getName().equalsIgnoreCase("god") && sender instanceof Player) {
            if (!isGod.contains(p)) {
                isGod.add(p);
                playerList.add(p);
                player.sendMessage(ChatColor.GREEN + "Godmode is enabled");
                return true;
            } else {
                isGod.remove(p);
                playerList.remove(p);
                player.sendMessage(ChatColor.GREEN + "Godmode is disabled");
                return true;
            }
        }
        return true;
    }
}

Listener Class:

    @EventHandler
    public void isDamaged(EntityDamageEvent e) {
        String player = e.getEntity().getName();
        if (playerList.contains(player)) {
            e.setCancelled(true);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire