Ok, so I’m working on a “shop” plugin for vanillasponge and I’m trying to remove the item from the shop inventory upon purchase.
so my current code is something along the lines of
@Listener
public void onInventoryClicked(ClickInventoryEvent e) {
Optional<Player> optPlayer = e.getCause().first(Player.class);
ItemStack tmp = e.getCursorTransaction().getDefault().createStack();
optPlayer.get().getInventory().offer(tmp);
optPlayer.get().sendMessage(Text.of("Items successfully purchased!"));
}
So the item gets purchased, but I have no idea how to remove it from the shop inventory, which is just a double chest inventory.
So far I’ve tried
npc.storage.query(QueryOperationTypes.ITEM_STACK_EXACT.of(tmp)).poll();
and
Optional<ItemStack> is = npc.storage.query(QueryOperationTypes.ITEM_STACK_EXACT.of(tmp)).poll();
is.ifPresent(itemStack -> itemStack.setQuantity(0));
and
for(int i = 0; i < npc.storage.capacity(); i++) {
Optional<ItemStack> is = npc.storage.query(QueryOperationTypes.INVENTORY_PROPERTY.of(SlotIndex.of(i))).peek();
if(is.isPresent() && is.get().equals(tmp)){
is.get().setQuantity(0);
break;
}
}
but none of the above seems to do anything.
Also, even if I cancel the event before running the last piece of code, the inventory is missing the clicked itemstack.
The inventory is stored in the ArrayList, so something could be up with the mutability. I just don’t know how this complicated spongeapi works.
I’m a long-term Bukkit/Spigot dev, so I know (at least partially ;)) how to code, but this is my first plugin in Sponge, so please don’t hate.
Kind regards, MGlolenstine