mojira.dev
MC-168573

After breaking a shield, the player's off-hand can't finish using some items

The Bug

If the player continues to hold down the 'use' key after they break a shield in their off-hand, they will be unable to properly use items in your main hand. This includes things such as using a bow & arrow, eating an item, where the player will appear to use the item, but will never fully finish the action.

Steps to Reproduce:

(Must be in Survival Mode)

  1. Run this command to place a command block below the player

    /setblock ~1 ~-1 ~ command_block{Command:"/damage @p 3 minecraft:mob_attack at ~1 ~2 ~"} replace
  2. Place a pressure plate above the command block

  3. Give the player a shield with 1 durability (Hold in main hand)

    /give @p shield{Damage:335} 1
  4. Give the player a golden apple (Hold in off-hand)

    /give @p golden_apple
  5. Block with the shield (Continue holding the 'use' key through step 6)

  6. Face the south direction, and activate the command block with the pressure plate

Observed Results:

The player will block the attack damage, and the shield will break. When it does, the player will then attempt to eat the golden apple, but will be unable to. Instead, the animation and sound will loop repeatedly never finishing.

Expected Results:

The player would block the attack, and then eat the golden apple completely.

Screenshots/Videos:

Me reproducing the bug:

[media]

Code Analysis:

Here inside the below method the game sees if the entity which is blocking has taken more than 3 damage points. If they have, the shield is then damaged, or possibly broken. It then checks if the useItem is empty (if the shield has broken) and if it has, set the "EquipmentSlot" to empty. After that, the shield breaking sound is played.

The issue here is that there is nothing telling the blocking entity to stop using the "useItem" (shield) after it has broken. This leads to the behavior where the player cannot use items such as the golden apple, because technically they still have not stopped using their shield. The fix for this, is add "this.stopUsingItem();" after the break sound is played.

Class: net\minecraft\world\entity\player\Player.java - Method: hurtCurrentlyUsedShield()
protected void hurtCurrentlyUsedShield(float damageIn) {
      if (this.useItem.is(Items.SHIELD)) {
         if (!this.level().isClientSide) {
            this.awardStat(Stats.ITEM_USED.get(this.useItem.getItem()));
         }

         if (damageIn >= 3.0F) {
            int i = 1 + Mth.floor(damageIn);
            InteractionHand interactionhand = this.getUsedItemHand();
            this.useItem.hurtAndBreak(i, this, (player) -> {
               player.broadcastBreakEvent(interactionhand);
            });
            if (this.useItem.isEmpty()) {
               if (interactionhand == InteractionHand.MAIN_HAND) {
                  this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
               } else {
                  this.setItemSlot(EquipmentSlot.OFFHAND, ItemStack.EMPTY);
               }
               this.useItem = ItemStack.EMPTY;
               this.playSound(SoundEvents.SHIELD_BREAK, 0.8F, 0.8F + this.level().random.nextFloat() * 0.4F);
Fix Start
               this.stopUsingItem();
Fix End
            }
         }
      }
   }

This provides the following behavior:

[media]

Notes:

  1. Switching items in your main hand will allow you to use the item you swap to.

  2. Releasing the 'use' key and pressing it again will properly allow you to use the item in your mian hand.

Linked issues

Attachments

Comments 11

Hey there!

Please could you provide some screenshots or recordings of this issue occurring whilst the F3 Debug Screen is enabled. This would help myself, along with others, determine what the problem is.

Can't reproduce in 1.16 (video attached)

@unknown, sorry but i think you didn't follow the steps correctly. The apple got eaten but because you let off right click for a second, you might want to use resistance V next time.

I can reproduce it on 1.17.1 and also the reporter of MC-234354 using two shields, so this should be reopened. And i want to point out that it happens on both hands, only goes away when you let off right click.

Yeah I can reproduce this in 1.17.1. Reopened.

Thanks, added a new video now that I can reproduce it

1 more comments

Still an issue in 1.19

Did a bit of testing and managed to reproduce this issue on 1.19.3. Seems the problem is the client side stops using the item, but the serverside still thinks its using a now empty item. Easier way to reproduce the issue is to put a nearly broken shield in the main hand, and the other item in the offhand. Holding right click with the shield will cause the offhand item to visually be used (e.g. eating or blocking animation plays), but the server still thinks the mainhand is being used.

All the cases of "this bug does not happen if" boil down to switching items causes you to stop using items, putting the server and client back in sync. e.g. if you break a shield in the main hand then try using an item in another hotbar slot.

Requesting ownership of this issue to add additional information and maintain it

Related to MC-265697, which has recently been triaged. I mention that, as this issue is identical to the other, just with a different item.

Jack Daly

[Mod] Jingy

(Unassigned)

Confirmed

Player

item-breaking, shield, use-item

1.15.1, 1.17.1, 22w11a, 1.19, 1.19.3, ..., 1.20.3 Release Candidate 1, 1.20.3, 1.20.4, 24w05a, 1.20.6

Retrieved