Jump to content

[SOLVED][1.7.10] Searching for a dropped item on the ground...


Recommended Posts

Posted

Hi guys,

 

I am looking for something similar to

 

Entity.worldObj.getEntitiesWithinAABB ....

 

but that would return a specific searched item that was dropped on the ground. Any idea what method I could use?

 

Here is what I want to achieve.

 

My mob scan 50 squares around him and can smell a raw steak that was dropped on the ground. I am looking for a method that would return the list of instance of a specific item class within a specific range of a position.

 

I googled the heck out of this ... might be my choice of words, but I was not able to find anything.

 

Where should I start to look at?

Posted

That's the method you have to use - specify EntityItem as the class of Entity to retrieve, and you will have a list of only EntityItems. Iterate through the list, cast to EntityItem, and check if it is the item you want.

 

Holy hell that does make sense. I was not figuring out that an item, when dropped on the ground ... becomes an EntityItem, which contains an ItemStack, which contains an Item.

 

Thanks a thousand time CoolAlias. I will come back here with my final result.

Posted

That's the method you have to use - specify EntityItem as the class of Entity to retrieve, and you will have a list of only EntityItems. Iterate through the list, cast to EntityItem, and check if it is the item you want.

 

Ok so here is the way I did it .... works perfectly

 

protected EntityItem GetClosestItem(Entity fromEntity, Class anItemClass, int xDistance, int yDistance, int zDistance)
{
	try
	{
		if (fromEntity!=null)
		{
			List _itemList = fromEntity.worldObj.getEntitiesWithinAABB(EntityItem.class, AxisAlignedBB.getBoundingBox(fromEntity.posX - xDistance, fromEntity.posY - yDistance, fromEntity.posZ - zDistance, fromEntity.posX + xDistance, fromEntity.posY + yDistance, fromEntity.posZ + zDistance));
			if ((_itemList!=null) && (_itemList.size()>0))
			{
				EntityItem _entityItem = null;
				Iterator _iterator = _itemList.iterator();

				while (_iterator.hasNext())
				{
					_entityItem = (EntityItem)_iterator.next();
					ItemStack _stack = _entityItem.getEntityItem();
					Item _item = _stack.getItem();
					//Check if the stack is the correct item type
					if (_item.getClass().equals(anItemClass))
					{
						return _entityItem;
					}
				}
			}
			return null;
		}
		return null;
	}
	catch (Exception e)
	{
		return null;
	}
}

Posted

Just putting it out there, it's generally bad practice to test for an item class since they tend to be used for multiple instances.

You can test stack.getItem().equals(Items.steak)

 

There is the odd time where you'll test for a class to group the instances, but you can then instead use stack.getItem() instanceof ItemSteak

Posted

Just putting it out there, it's generally bad practice to test for an item class since they tend to be used for multiple instances.

You can test stack.getItem().equals(Items.steak)

 

There is the odd time where you'll test for a class to group the instances, but you can then instead use stack.getItem() instanceof ItemSteak

 

Good point you have there. Thanks for the hint.

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.

×
×
  • Create New...

Important Information

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