How to remove an item from the stack?

If I already have access to an item stack, how can I remove one item, and do I need to do anything differently if I’m trying to remove the final item?

Answer: Trying to remove one item from the hand? stack.setQuantity(stack.getQuantity() - 1); player.setItemInHand(HandTypes.MAIN_HAND, stack);

get count
calculate newcount = count-1
if newcount=0 --> clear stack
else set count=newcount

I guess, what are the methods I need? I’ve tried stack.setQuantity() but it didn’t work.

I believe you need to get the inventory slot and #poll(int)

How can I get the inventory slot?

setQuantity should work fine. Perhaps you could give us more context for what you’re doing (and provide code samples).
For example, if the ItemStack came from an inventory, you need to give the inventory the new item stack back (because inventories deal with copies of stacks).

That was the main problem, but I’m not sure how to offer the ItemStack.

player.getInventory().offer(stack.setQuantity(stack.getQuantity() - 1)); was my main idea but it says “The method offer(ItemStack) in the type Inventory is not applicable for the arguments (void)”. I’ve tried creating a new ItemStack() but that doesn’t change anything.

setQuantity returns void not the ItemStack. Need to split up the code into two lines.

stack.setQuantity(stack.getQuantity() - 1);

I see. I thought I needed to create a new stack. Thanks. But that still gives a new stack. How can I just remove the one?

Edit: Figured it out. Using setItemInHand(HandTypes.MAIN_HAND, stack).