Arraylist of players

Discussion in 'Plugin Development' started by CraftBang, Jul 25, 2014.

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

    CraftBang

    Hey there!

    I had a little problem with an arraylist of Players.
    It seemed like if you rejoined the server you were a different player.
    I didn't know how or why, so I fixed it by using an arraylist of strings and save the playername.
    Can someone maybe explain me how the Player is different when he rejoins?

    Arraylist String:
    Code:
    static ArrayList<String> aliveteamplayers = new ArrayList<String>();
    
    Arraylist Player:
    Code:
    static ArrayList<Player> aliveteamplayers = new ArrayList<Player>();
    
    Thanks in advance,
    CB
     
  2. Offline

    mine-care

    Try to read bukkits documentation related to player object. A player object holds a whole world of information, it's not preferable to use it in maps unless you are sure you remove them in disconnect ect. Therefore you can use the players name as string and then use Bukkit.getplayer(); to retrive the object.
     
  3. Offline

    dsouzamatt

    A UUID would be a better option.
     
    Garris0n likes this.
  4. Offline

    Traks

    That and possible memory leaks are the reasons for which you shouldn't store Player references. Instead of using Strings, I recommend UUIDs (Universally Unique Identifiers). You can get the UUID of a Player by calling Player#getUniqueId(). The unique identifiers of players (mojang accounts) are essentially immutable, a player will always have the same one. Names on the other hand, are liable to change.

    But why can storing Player references wreak memory leaks?
    One of the objects a Player instance (indirectly) holds a reference to, is a world object. When a client disconnects from a Bukkit server, and you keep the Player object referenced, only you hold the reference to that object. So upon unloading the world, that player was previously in (before disconnecting), the world reference in the Player object won't be cleaned up, which keeps all the non-unloaded world values in memory. This is not what you want, and thus inevitably causes a memory leak.

    And why is the Player object different after a re-log?
    For two Player objects to be logically equal, Bukkit overrode Object#equals(Object), which basically requires the input Object to be an OfflinePlayer, so it can compare their UUIDs. However, if the argument is a Player, Bukkit also compares their entity IDs, which most likely differs after re-log.

    As a side-note, I suggest utilising a HashSet instead of an ArrayList. Sets don't allow duplicate objects and there is no need to have duplicates in your Collection anyway. However, especially HashSets are incredibly efficient, since they're built on a so-called hash table data structure. This results in #add, #contains, #size and #remove being (very close to) O(1), which means the time it takes to perform these operations are (nearly) always equivalent, independent of the size of the HashSet.
     
  5. Offline

    mine-care

  6. Offline

    fireblast709

    weak referencing.
     
    Traks and xTigerRebornx like this.
Thread Status:
Not open for further replies.

Share This Page