Minecraft Inventory Management – Subtract Item from Offhand and Add Remainder Back

minecraft-commandsminecraft-java-edition

I'm in Java 1.14.4, and I'm trying to detect if the player has dragon breath in their offhand slot. If they do, then I want to subtract 1 from that stack and then add the remaining parts of the stack back into the inventory.

execute as @a[nbt={Inventory:[{id:"minecraft:dragon_breath",Slot:-106b}]} store result score @s offHandCount run data get entity @s Inventory[-1].Count 1 won't work because only tag items cannot have their data modified, so when I subtract from the count nothing would happen.

Clearing the player of all dragon breath in their inventory, and then using give to give the player back the dragon breath with the original count, minus 1 works. However, this can be very distracting, especially if the player has a large amount of that item.

My question is unique in that I am looking to remove the item from the offhand slot as mentioned in the title and post.

Best Answer

Here's a bit of a wacky workaround that I think does what you want.

To start, there's no way to guarantee you'll be removing one item from a player's main-hand / off-hand slot if they might have the same item in other parts of their inventory.

Solution? Make them not the same item.

Whenever a player puts dragon breath bottles in their hotbar, clear them and replace them with dragon's breath bottles with a custom nbt tag specific to that hotbar slot (there may be some scoreboard math involved in this). This should only happen once as a setup step when they "equip" the dragon's breath to their hotbar. You'll get that annoying "redrop" thing once initially from the clear and regive, but not during active gameplay. This means the dragon's breath bottles in the player's hotbar are different than the ones in the rest of their inventory.

Now, whenever you detect that a dragon's breath with a custom tag (hotbarSlotNumber:X) is in their offhand slot (when a player puts it there), you can clear the player of 1 dragon breath bottle (with custom tag hotbarSlotNumber:X). That will remove one dragon breath bottle from their offhand. We need to specify the hotbar slot so that we don't clear any dragon breath bottles that might be in other locations in the hotbar.

You can simplify a lot of this by adding the restriction that the dragon's breath can only be in a specific hotbar slot, or the hotbar will have a maximum of one stack dragon's breath bottles. That way you don't need to keep track of nine tags.

You say in comments that you want the items to bounce back into the player's mainhand slot afterwards, which doesn't really make sense to me because at that point you'll have to do the "redrop" thing every time anyway.

All this said, using a retextured carrot on a stick would probably be much easier. You could use the xp bar or the title command with a score to represent how many usages of the item you have left.