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
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.
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 Code:java // If there's one CharlesUUID charlesUniqueId = null; // If there are a lots of CharlesSet<UUID> charlesUniqueIds = new Set<UUID>(); Just simply instantiate them: Code:java charlesUniqueId = <charles>.getUniqueId(); // or charlesUniqueIds.add(<charles>.getUniqueId()); To remove them: Code:java for (Entity entity : Bukkit.getWorld("world").getLivingEntities()) { if (entity.getUniqueId().equals(charlesUniqueId)) { entity.remove(); break; } // or if (charlesUniqueIds.contains(entity.getUniqueId()) { entity.remove(); break; }}