mojira.dev
MC-313473

Off-hand item is deleted when the main-hand item breaks from an attack and has its data modified

When a main-hand weapon or tool item breaks from an attack, and the main-hand item is replaced or modified, the off-hand item is deleted. The broken main-hand item itself behaves as expected. Breaking a main-hand item by mining, or breaking armor, does not delete the off-hand item.

A datapack can use the minecraft:item_durability_changed advancement trigger to modify an item as it is breaking.

Steps to Reproduce

  1. Install the minimal datapack:

    1. pack.mcmeta:

      {"pack": {"description": "repro", "min_format": 121, "max_format": 121}}
    2. data/repro/advancement/pickaxe_breaks.json:

      {
      	"criteria": {
      		"breaks": {
      			"trigger": "minecraft:item_durability_changed",
      			"conditions": {
      				"item": {"items": "minecraft:diamond_pickaxe"},
      				"durability": {"max": 0}
      			}
      		}
      	},
      	"rewards": {"function": "repro:pickaxe_breaks"}
      }
    3. data/repro/advancement/sword_breaks.json:

      {
      	"criteria": {
      		"breaks": {
      			"trigger": "minecraft:item_durability_changed",
      			"conditions": {
      				"item": {"items": "minecraft:diamond_sword"},
      				"durability": {"max": 0}
      			}
      		}
      	},
      	"rewards": {"function": "repro:sword_breaks"}
      }
    4. `data/repro/function/pickaxe_breaks.mcfunction`:

      advancement revoke @s only repro:pickaxe_breaks
      item modify entity @s weapon.mainhand {type:"minecraft:set_damage",damage:1.0}
    5. `data/repro/function/sword_breaks.mcfunction`:

      advancement revoke @s only repro:sword_breaks
      item modify entity @s weapon.mainhand {type:"minecraft:set_damage",damage:1.0}
  2. Run:

    /gamemode survival
    /item replace entity @s weapon.offhand with minecraft:shield
    /item replace entity @s weapon.mainhand with minecraft:diamond_sword[damage=1560]
    /summon minecraft:pig ~ ~ ~2 {NoAI:1b}
  3. Hit the pig once with the sword.

  4. Run:

    /gamemode survival
    /item replace entity @s weapon.offhand with minecraft:shield
    /item replace entity @s weapon.mainhand with minecraft:diamond_pickaxe[damage=1560]
  5. Mine any block with the pickaxe.

Observed Result

Sword Attack: The main hand contains the sword at full durability, and the shield in the off-hand is deleted.

Pickaxe Use: The main hand contains the pickaxe at full durability, and the shield stays in the off-hand.

Expected Result

In both cases: The main hand contains the tool at full durability, and the shield stays in the off-hand.

Cause

Relevant Code:

net.minecraft.world.entity.player.Player:

public void attack(final Entity entity) {
    ...
    ItemStack attackingItemStack = this.getWeaponItem();
    ...
    this.itemAttackInteraction(entity, attackingItemStack, damageSource, true);
    ...
}

private void itemAttackInteraction(final Entity entity, final ItemStack attackingItemStack, final DamageSource damageSource, final boolean applyToTarget) {
    ...
    if (itemHurtEnemy) {
        attackingItemStack.postHurtEnemy((LivingEntity)hurtTarget, this);
    }
    
    if (attackingItemStack.isEmpty()) {
          if (attackingItemStack == this.getMainHandItem()) {
              this.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY);
          } else {
              this.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
          }
    }
    ...
}

net.minecraft.world.item.slot.CountingModifier

public class CountingModifier implements Consumer<SlotAccess> {
    ...
    public void accept(final SlotAccess slot) {
        boolean success = slot.set((ItemStack)this.modifier.apply(slot.get().copy()));
        if (success) {
            this.updatedCount++;
        }
    }
    ...
}

net.minecraft.world.item.ItemStack

public void postHurtEnemy(final LivingEntity mob, final LivingEntity attacker) {
      ...
      Weapon weapon = this.get(DataComponents.WEAPON);
      if (weapon != null) {
          this.hurtAndBreak(weapon.itemDamagePerAttack(), attacker, EquipmentSlot.MAINHAND);
      }
}

public void hurtAndBreak(final int amount, final ServerLevel level, final @Nullable ServerPlayer player, final Consumer<ItemStack> onBreak) {
      ...          
      this.applyDamage(this.getDamageValue() + newAmount, player, onBreak);
      ...
}
   
private void applyDamage(final int newDamage, final @Nullable ServerPlayer player, final Consumer<ItemStack> onBreak) {
      if (player != null) {
          CriteriaTriggers.ITEM_DURABILITY_CHANGED.trigger(player, this, newDamage);
      }
      
      this.setDamageValue(newDamage);
      if (this.isBroken()) {
          ItemStack broken = this.copy();
          this.shrink(1);
          onBreak.accept(broken);
      }
}

When a tool or weapon is used to make an attack it calls into Player.attack, which saves a local reference to attackingItemStack. It then passes attackingItemStack to itemAttackInteraction, which calls into attackingItemStack.postHurtEnemy(...) which calls into hurtAndBreak.

In applyDamage, the advancement is triggered, and uses CountingModifier. Note that in CountingModifier, rather than apply the modification directly to the ItemStack, it instead creates a new item stack and puts that into the slot. This causes the saved attackingItemStack to become stale.

After the advancement trigger, itemAttackInteraction checks the stale attackingItemStack.isEmpty(), which is true as the original item has count 0 (after applyDamage), then checks if the stale attackingItemStack is equal to the current main-hand. Since it is not, then the off-hand is deleted without checking if the off-hand is equal to the attackingItemStack.

Fix

if (attackingItemStack == this.getMainHandItem()) {
    this.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY);
} else if (attackingItemStack == this.getOffhandItem()) {
    this.setItemInHand(InteractionHand.OFF_HAND, ItemStack.EMPTY);
}

Attachments

Comments 1

Thank you for helping us improve Minecraft! We saved your files:

[media]

Art Lu

(Unassigned)

Unconfirmed

26.3

Retrieved