Checking if ItemStack is entity

I want to get the location of where a item is. So if the ItemStack is the Item entity, i can grab the location, same goes if its in someones inventory, i just grab the location of the player. I have worked out how to grab the inventory that’s the item maybe held in, just need help with converting ItemStacks to Item entity if the itemStack is a Entity.

If that was not clear here is a example.

My plugin holds the data for a ItemStack, but a player throws that item on the floor turning that ItemStack into a Entity. I want to be able to grab that Entity.

-Edit-

I know I could grab all the Entities in the server then check if that entity is a Item entity. Then check if the ItemStack is the ItemStack I hold, but that seems a bit inefficient

-/edit-

Thanks

An ItemStack isn’t one Item. It’s a type of ann Item. I’m not entirely sure how this could be done easily.

I understand that a ItyemStack is not a single Item. If it came out sounding like i though that, then sorry

An Item entity holds an ItemStack, so you could just grab the Item object and ask for the ItemStack.

You can also test around to see if the item drop triggers something like EntitySpawnEvent. If it does, you’ll be able to grab it from there. Though, this seems like we can improve the current ItemDropEvent by changing the returned set to be the Item as opposed to the ItemStack.

I suggest you open the issue on the Github Repository for SpongeAPI.

hmm, i guess thats one way to do it. Ill use it if no other way come to me

ItemDropEvent is called before the Item entity is spawned in the event the event is cancelled, no Item entity is created unnecessarily. The correct way to listen for this is with the EntitySpawnEvent.

1 Like

I guessed as much. Thanks