block.getNearbyLivingEntities()

Discussion in 'Plugin Development' started by Ne0nx3r0, Apr 6, 2011.

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

    Ne0nx3r0

    The end goal I'm going for is to grab a list of living entities within X distance (likely using the basic sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2) formula); however every way I try to tackle getting the entities seems to have a road block.

    My latest attempt was:
    Code:
    Entity[] entities = event.getBlock().getChunk().getEntities();
    For some reason this is causing a typecast Object error though.

    How can I get a list of nearby living entities? I'm fine with doing it chunk by chunk and filtering the entities, but as stated above that's not working for me yet.

    Is it possible I'm running into this?
    http://redmine.bukkit.org/issues/607

    (I assume "closed" means it was corrected)
     
  2. Offline

    Carnes

    Try this: http://javadoc.lukegb.com/BukkitJD/...tml#getNearbyEntities(double, double, double)

    A snippet of my code using it:
    Code:
            List entities = mob.entity.getNearbyEntities(mob.attack_distance, mob.attack_distance, mob.attack_distance);
            for(Object entity: entities)
            {
                if(entity instanceof LivingEntity)
                {
                    if(entity instanceof Player || entity instanceof Animals) //Player? kill it!
                    {
    
    ...
    
    mob.attack_distance is a double
    mob.entity is a CraftCreature

    I'd clean it up more but man i'm tired, haha
     
  3. Offline

    Ne0nx3r0

    (should have mentioned this before) I saw that, but I'm running this from a block's perspective - is there something like getNearbyEntities that uses a block?
     
  4. Offline

    Carnes

    I looked but couldn't find anything. You can get all the entities in a given world or within range of another entity. You could perhaps use the Player as your center point? Or spawn an entity on/near your block.. but that feels crufty : /
     
  5. Offline

    Ne0nx3r0

    Well, at least I don't feel as bad about being stuck on this. I'd be happy to do some math and grab the nearby chunks, then just append the list of entities, if I could get Chunk.getEntities() to work! lol.
     
Thread Status:
Not open for further replies.

Share This Page