Minecraft Get the object from a list and be abbe to manipulate it

140 views Asked by At

Hey in my minecraft mod I am trying to get the near entity's from my entity and be able to manipulate them.

This is the way I have been getting the near entitys:

    List entitylist = this.getEntityWorld().getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(5.0D, 10.0D, 5.0D));

So how do I get the entitys out of the list?

3

There are 3 answers

2
Emax On

This code should work!, it get all the entity out of the selected entity

// World of the entity
World entityWorld = getEntityWorld();

// Who is near the entity ?
List nearEntities = entityWorld.getEntitiesWithinAABBExcludingEntity(this, getEntityBoundingBox().expand(5.0D, 10.0D, 5.0D));

// All the entity in the world
List allEntities = entityWorld.loadedEntityList;

// Create a new list that will contains the outer entity
List<Entity> outerEntities = new ArrayList<Entity>();

// Add all the entity in the world to the list
outerEntities.addAll(allEntities);

// Remove all the near entity
outerEntities.removeAll(nearEntities); 

// outerEntity now contains all outer entity
for (Entity outerEntity : outerEntities) {
    // Action on outer entity
}
13
Kasper Sanguesa-Franz On

Something like this might do it :)

List entitylist = this.getEntityWorld().getEntitiesWithinAABBExcludingEntity(this, this.getEntityBoundingBox().expand(5.0D, 10.0D, 5.0D));
for (int i = 0; i < entitylist.size(); i++) {
 //Do stuff with each entity  
 Entity entity = (Entity) entitylist.get(i);
}
1
epb On

Your entitylist is something that now contains objects. In this case, you would say, "it's a list that contains entities". (I'm just assuming they are entities... We don't know for sure!)

You can look at each thing in the list using a for-loop, like this:

for (Object entity : entitylist){ //for each entity in the list
    if (o instanceof EntityLiving){ //<-- replace EntityLiving with what you expect

        ((EntityLiving) o).doStuff() //for example... I'm not familiar with Minecraft code!
        //...etc
    }
}

(This is more complicated than it should be, because the Minecraft code is strange, and doesn't tell us what type of thing is contained in the list. Just my opinion!)