So I need help with adding a player to an ArrayList. My ArrayList is Naughty. The Player is Santa. What is wrong with this? Code: Naughty.add(Santa.getName()) Here is where I made the ArrayList: Code: public ArrayList<Player> Naughty = new ArrayList<Player>(); Here is the whole section that uses this ArrayList: Code: public void onPlayerJoin(PlayerJoinEvent login) { Player Santa = login.getPlayer(); if(!(Naughty.contains(Santa.getName()))) { Santa.sendMessage("Merry Christmas from KaiBB, " + Santa.getName() + ", here is your present! A diamond pickaxe!"); final Inventory inv = Santa.getInventory(); inv.addItem(new ItemStack(Material.DIAMOND_PICKAXE, 1)); Naughty.add(Santa.getName()); } If you could not tell by the codes, I want it to only do these two functions once per player. Thanks!
public ArrayList<Player> Naughty = new ArrayList<Player>(); should be public ArrayList<String> Naughty = new ArrayList<String>();
Santa's song about lists aside, you should be using a Set for keeping track of who is Naughty or not. Searching for whether someone is naughty is much slower on a List, because every name up to the match must be checked.
Well I just named it Naughty for fun. It's really a list of players who have already received a gift. That way they don't get a gift every time they join!
Do you understand your mistake? Santa.getName(); returns a string. But your ArrayList<Player> is for Players, not for Strings.
Although these types of collections structures transcend Java and is applicable to all programming. Although i suppose each languages' implementation has their quirks.