Despawn specific animal

Discussion in 'Plugin Development' started by najm, Sep 17, 2014.

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

    najm

    Hey,

    I searched 1 hour for this but didn't find anything:/
    So I spawned an animal, lets call it Charles. Now I wanne despawn him when someone leftclicks with wheat. The event stuff is no problem, onliest problem is that I have no idea how to remove Charles again.
    Another dev gave me the tip to try world.getLivingEntities, if that helps^^
    Thx for your reply!
    ~Najm
     
  2. Offline

    Garris0n

    If the entity is a per-player thing, save their UUIDs in a map.

    If there's just one, save its UUID. If there are many, save their UUIDs in a set.

    Alternatively, tag them with metadata.
     
  3. najm
    If you store Charles (or its UUID) anywhere like a HashMap, you can fetch him back and call Entity#remove().

    If not, use the World#getLivingEntities (or getLivingEntitiesByClass to be specific) to loop through the entities in that world, check if the entity is Charles (e.g. by comparing custom name), then again call Entity#remove().

    edit: ninja'd.
     
    najm likes this.
  4. Offline

    najm

    Assist
    Sorry, could you give me an example?:)
     
  5. Offline

    Panjab

    najm

    Code:java
    1.  
    2. // If there's one Charles
    3. UUID charlesUniqueId = null;
    4.  
    5. // If there are a lots of Charles
    6. Set<UUID> charlesUniqueIds = new Set<UUID>();
    7.  


    Just simply instantiate them:

    Code:java
    1.  
    2. charlesUniqueId = <charles>.getUniqueId();
    3.  
    4. // or
    5.  
    6. charlesUniqueIds.add(<charles>.getUniqueId());
    7.  


    To remove them:

    Code:java
    1.  
    2. for (Entity entity : Bukkit.getWorld("world").getLivingEntities()) {
    3. if (entity.getUniqueId().equals(charlesUniqueId)) {
    4. entity.remove();
    5. break;
    6. }
    7.  
    8. // or
    9. if (charlesUniqueIds.contains(entity.getUniqueId()) {
    10. entity.remove();
    11. break;
    12. }
    13. }
     
Thread Status:
Not open for further replies.

Share This Page