Solved How to remove player from ArrayList (DeathEvent)

Discussion in 'Plugin Development' started by Space_BR, Jul 29, 2014.

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

    Space_BR

    I need to remove one player out of ArrayList, but it nor worked...
    I am using this to remove, see:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void PlayerDeathEvent(PlayerDeathEvent e) {
    3. Player p = e.getEntity();
    4. jogadores.remove(p);
    5.  
    6. }


    Code:java
    1. ArrayList<Player> jogadores = new ArrayList<Player>();
     
  2. Offline

    Msrules123

    Get entity would detect a mob type, it would need to be e.getPlayer()
     
  3. Offline

    rfsantos1996

    Eu não sei se você sabe os prejuizos de guardar uma instance de Player numa Lista, mas procure sobre Memory leak on Java no google

    E você poderia diretamente usar jogadores.remove(e.getEntity()), eu pelo menos acho mais "economico"... E tem certeza que ele é adicionado a lista? Tente usar debug (Bukkit.getLogger() ou o System.out mesmo depois de adicionar, confira se jogadores.contains(player), veja onde a lista é usada e se é usada corretamente)

    (I know i'm talking in portuguese, but you can translate via Google translator like everyone else on the globe that don't know english does)
     
  4. Offline

    HeadGam3z

    Don't use Player in your ArrayList, use String. If you use Player, you're going to get EVERYTHING that the player contains (inventory, location, etc). All you need is the player's name. So just use String.

    Also, if you do change Player to String, use jogadores.remove(p.getName());

    Just a reminder, don't forget to register your event(s).
     
    Space_BR likes this.
  5. Offline

    Traks

    Msrules123 HeadGam3z Ehm, no, PlayerDeathEvent#getEntity() returns the player that died.

    Here's something you might find useful, mind you that weak references could also be used as mentioned by fireblast709.
    By the way, what exactly didn't work? I don't see any code that checks whether the Player has been removed from the List, nor do I see code that adds Players to it...
     
    Space_BR likes this.
  6. Offline

    Space_BR

    Thx, i just change Player to String and fix it :D
     
Thread Status:
Not open for further replies.

Share This Page