Cached mob despawning failure

Discussion in 'Plugin Development' started by NemesisMate, Oct 11, 2013.

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

    NemesisMate

    I'm trying to despawn some cached mobs:

    Code:java
    1. for(MyMob mymob : mobList.values()){
    2. if(!mymob.getMob().getLocation().getChunk().isLoaded())
    3. mymob.getMob().getLocation().getChunk().load();
    4. if(mymob.getMob() != null){
    5. mymob.getMob().setRemoveWhenFarAway(true);
    6. if(!mymob.getMob().isEmpty()){
    7. Entity passenger = mymob.getMob().getPassenger();
    8. ((LivingEntity) passenger).setRemoveWhenFarAway(true);
    9. passenger.leaveVehicle();
    10. passenger.remove();
    11. }
    12.  
    13. mymob.getMob().remove();
    14. }
    15. }

    • mobList is a HashMap<Integer, MyMob>
    • mymob.getMob() returns a LivingEntity
    • previously I: mobList.put(mymob.getMob().getEntityId(), this) where this is MyMob
    • If I do a player.teleport(mymob) I get teleported to the mob spawn location (for some reason doesn't teleport to actual mob location)
    When I try to despawn them sometimes (really few times) I get them removed but normally they aren't despawned, they simply still there. Something really strange is that if I do a mymob.getMob().isDead(), it return "true".
    Thanks in advantage for any help.
     
  2. Offline

    xTrollxDudex

    NemesisMate
    I think there's a memory leak when using LivingEntity#remove() (not sure though, I'll have to check), and that's sure a fancy way of looping though a hashmap. Are you sure it works? Usually this is achieved by looping though the entry set of the hashmap ;)
     
  3. Offline

    NemesisMate


    Yes, I use it on many other hashmaps and it works fine.

    If it is helpful, I'm using Bukkit 1.6.4 R-0.1
    If I check if that mob is valid: mymob.getMob().isValid(), it returns false, as with isDead() it returns true.

    I just saw the reason, the mob change it ID so I suppose is a different mob when I try to remove him (his name is the same and all it values are my customized mob but the server despawn/spawn it changing the entity so I can't save it on a variable, now I need a way of doing this).



    I can save the mob by UUID so it never changes, by I still having the problem about removing the mob if this is not loaded so I need a way to load a mob having his UUID stored (I can't find it).

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  4. Offline

    xTrollxDudex

    NemesisMate
    Try looping through the world.getEntities() and check if the entity's UUID matches that in the loop
     
  5. Offline

    NemesisMate


    The problem there is if the mob isn't loaded. Whatever, the only way I found is saving the mob location on each chunk unloading (taking that chunk entities and looking for the mob and saving if so).
     
Thread Status:
Not open for further replies.

Share This Page