Jump to content

Find Entities Around you


pjlasl

Recommended Posts

Thanks for the push in the right direction.  ;) It took me a few minutes, but I figured it out. Now it will identify any EntityMob within 5 blocks (if my understanding of the +5 is correct)

 

For those who actually would like to see some code:

 


List e = player.worldObj.getEntitiesWithinAABB(EntityMob.class, AxisAlignedBB.getBoundingBox(player.posX, player.posY, player.posZ, (player.posX + 5),(player.posY + 5),(player.posZ + 5)));

if (e.size() > 0) {

        	for (int i = 0; i <= e.size() - 1; i++) {
        		EntityMob em = (EntityMob) e.get(i);
        		LogHelper.info(em.getEntityName());
        	}    	
        	
        }   

Link to comment
Share on other sites

You've almost got it right. Now you're setting up a AABB for only a part of the radius. This can be solved by doing this:

List<EntityMob> e = player.worldObj.getEntitiesWithinAABB(EntityMob.class, AxisAlignedBB.getBoundingBox(player.posX - 5, player.posY - 5, player.posZ - 5, player.posX + 5, player.posY + 5, player.posZ + 5));

Checking for e.size() > 0 is unnecessary, as the test of the for loop will be false at the start (0 <= 0 - 1) = false. But this is a small side note.

 

This code should work at the server side as well....

 

By the way, if you want to have a real max of 5 blocks, you can use Pythagoras' theorem to filter out entities that are outside your radius (you're now checking in a box around the player).

Author of PneumaticCraft, MineChess, Minesweeper Mod and Sokoban Mod. Visit www.minemaarten.com to take a look at them.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.