1

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.

  • @pppery My question is definitely not a duplicate of that. That is using /clear to remove a specific amount of an item from the player's inventory as a whole. I am exclusively looking for items that are in the offhand slot. The clear command doesn't include clearing items from specific slots. Also, the command syntax used in that question is way out of date – Hydra Jul 23 at 1:48
  • You explicitly said "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". This implies that the part you are actually stuck on is the ability to clear only one dragon breath as opposed to removing all of them and then re-giving them to the player, which is answered by the duplicate. – pppery Jul 23 at 2:03
  • @pppery I said clearing the player and then giving the original count -1 was successful. This doesn't imply that I am stuck on clearing only one of them. It implies I was able to do so as from me saying it works. I then said that the method was distracting if the player has a large amount of the item in the inventory. Also if you notice the title I say "and then add the remainder back to the inventory..." which this question does not answer. And nevertheless, the syntax in that question does not meet up to Java 1.14.4 which is something clearly stated in the question. – Hydra Jul 23 at 2:25
  • Do I understand this question right that when the player has e.g. 10 items in their offhand, you want to remove 1 and also give the resulting amount to them, ending up with 9 items in the offhand and 9 items in some other slot? – Fabian Röling Jul 23 at 9:42
  • 1
    @FabianRöling The result would play out like the player has 10 dragon breath in their main hand, the player presses "F" or whatever their offhand keybind is, then 1 dragon breath is subtracted from that stack of 10, and then the player has 9 dragon breath in their main hand slot, the off-hand has nothing. I'm trying to simulate the player "using" an item. I'm trying to use the offhand slot for getting an event/input. – Hydra Jul 23 at 21:30
1

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.

  • The reason I want them to bounce back is that I'm trying to simulate the player using/drinking the item. Checking for it being in the offhand slot is just a way of getting input and I really don't want to get into resource packs for this project. – Hydra Jul 26 at 1:01
0

I ended up just deciding to hardcode the count of dragon_breath in the player's inventory with replaceitem.

execute as @a[nbt={Inventory:[{id:"minecraft:dragon_breath",Count:2b,Slot:-106b}]}] unless data entity @s SelectedItem run replaceitem entity @s weapon.mainhand dragon_breath 1

execute as @a[nbt={SelectedItem:{id:"minecraft:dragon_breath"},Inventory:[{id:"minecraft:dragon_breath",Slot:-106b}]}] run replaceitem entity @s weapon.offhand air

Of course, the count has to be increased for both the Count and the amount that has been given until I reach the max stack size.

I decided to just write a simple program to generate these commands for me and then I just copy-pasted the output into an mcfunction, since writing the commands for each slot manually would be very tedious.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.