Uploaded image for project: 'Minecraft: Java Edition'
  1. Minecraft: Java Edition
  2. MC-168573

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • None
    • 1.15.1, 1.17.1, 22w11a, 1.19, 1.19.3, 1.20.2, 23w41a, 23w42a, 1.20.3 Release Candidate 1, 1.20.3, 1.20.4, 24w05a
    • Confirmed
    • Player

      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:
      2023-10-13_13-40-30.mp4

      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:
      2023-10-13_14-44-35.mp4

      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.

            Unassigned Unassigned
            Jingy Jiingy
            Votes:
            7 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              CHK: