How can i get all dropped items in the chunk?


#1

I need to delete specific dropped(!) items from the chunk, how can i get all dropped items in the chunk?


#2

Something like.

Chunk chunk;
Collection<Entity> items = chunk.getEntities(e -> e instance Item);

While this does give a collection of entities, its actually a collection of Items. You can do a unsafe cast if you wish.


#3

Without using instanceof:

Chunk chunk = ...;

chunk.getEntities()
     .stream()
     .filter(entity -> entity.getType().equals(EntityTypes.ITEM))
     .filter(entity -> ...)
     .forEach(entity -> entity.remove());

#4

OK, i can get all dropped items, but how can i get only Gravel or Dirt for example?
entity.getType() only give me minecraft:item


#5

Well it would as the Item is an Entity, not a itemstack. You can get the itemType that the entity is by the following.

Item item;
ItemType type = item.getItemType();

You can also use the key.

Keys.REPRESENTED_ITEM

#6

theris no such method getItemType


#7

Its very new to Sponge, if you wish to use it make sure your targeting the latest Sponge (Im guessing your targeting 7.0.0 and the latest is 7.1/7.2). As mentioned before you can use the Key. Key will work in all Sponge 7.x builds.

You can see that it is included by the github page.


#8
Extent extent = ...;

extent.getEntities()
      .stream()
      .filter(entity -> entity.getType().equals(EntityTypes.ITEM))
      .map(entity -> (Item) entity)
      .filter(itemEntity -> itemEntity.getItemType().equals(ItemTypes.GRAVEL))
      .forEach(itemEntity -> itemEntity.remove());