Get Entity From UUID?

Discussion in 'Spigot Plugin Development' started by ReadySetPawn, May 10, 2015.

  1. Okay so I have the UUID. Now how do I get an entity from that UUID? If there is a way to get this without iterating through a long list of entities, it will be perfect :D
     
  2. As far as I know, you have to iterate through the entity list of each world:
    Code (Java):
    public Entity getEntityByUniqueId(UUID uniqueId) {
        for (World world : Bukkit.getWorlds()) {
            for (Entity entity : world.getEntities()) {
                if (entity.getUniqueId().equals(uniqueId))
                    return entity;
             }
        }

        return null;
    }
    I'm not sure if you should only iterate through loaded chunks, so here is a version that only checks loaded chunks:
    Code (Java):
    public Entity getEntityByUniqueId(UUID uniqueId){
        for (World world : Bukkit.getWorlds()) {
            for (Chunk chunk : world.getLoadedChunks()) {
                for (Entity entity : chunk.getEntities()) {
                    if (entity.getUniqueId().equals(uniqueId))
                        return entity;
                }
            }
        }

        return null;
    }
     
    • Informative Informative x 4
    • Agree Agree x 1
    • Useful Useful x 1
  3. @CraftedFury

    Hmm, I'll use that for now until I can find a better way (assuming there is a better way).
    Thanks!
     
  4. You could store a weak reference to the Entity object yourself (which is what I prefer as opposed to iterating).
     
  5. You need to loop through all entities or store the entity somewhere.
     
  6. In entity tameable for nms on getowner() it shows this.world.b(uuid). I'm using 1.8.7. Sorry to bring this thread back alive but maybe this is very helpful!

    So if you're going through bukkit cast the world to craft world and get the nms world with gethandle and b(uuid) should return the entity living
     
    • Informative Informative x 2