Help with Array Lists.

Discussion in 'Plugin Development' started by KaiBB, Dec 25, 2011.

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

    KaiBB

    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! :D
     
  2. Offline

    coldandtired

    public ArrayList<Player> Naughty = new ArrayList<Player>();

    should be

    public ArrayList<String> Naughty = new ArrayList<String>();
     
  3. Offline

    KaiBB

    Ohh. Thank you. And the part where I add Santa.getName() is fine?
     
  4. Offline

    halley

    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.
     
  5. Offline

    KaiBB

    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! :D
     
  6. Offline

    ItsHarry

    Do you understand your mistake?
    Santa.getName(); returns a string.
    But your ArrayList<Player> is for Players, not for Strings.
     
  7. Offline

    user_43347

    Errr, note that the list will reset on server reboot or reload.
     
  8. Change List to Set and ArrayList to HashSet. Nothing else :)
    I REALLY suggest you learn more java :)
     
  9. Offline

    Sagacious_Zed Bukkit Docs

    Although these types of collections structures transcend Java and is applicable to all programming. Although i suppose each languages' implementation has their quirks.
     
Thread Status:
Not open for further replies.

Share This Page