Frag Posted September 15, 2015 Posted September 15, 2015 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? Quote
coolAlias Posted September 15, 2015 Posted September 15, 2015 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. Quote http://i.imgur.com/NdrFdld.png[/img]
Frag Posted September 15, 2015 Author Posted September 15, 2015 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. Quote
Frag Posted September 16, 2015 Author Posted September 16, 2015 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; } } Quote
TrashCaster Posted September 16, 2015 Posted September 16, 2015 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 Quote
Frag Posted September 16, 2015 Author Posted September 16, 2015 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. Quote
Recommended Posts
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.