Mojira Archive
MC-89347

The 'using []' part of a player's death message can show the wrong item

The death message of a dead player can show the name of an item which is unrelated to their death. For instance; this can occur when one player shoots another with a bow/crossbow, but then replaces the bow/crossbow in their main-hand with a different (renamed) item before the second player dies. The same can also be observed when the player has a bow in their off-hand, but a renamed item in their main hand. The general issue here being that the game does not properly detect the source of a death when choosing what item name to display, but instead uses the name of the item in the main hand (of the 'killer' player) upon death.

Steps to Reproduce:

  1. Get a renamed item:
    /give @s dirt[minecraft:custom_name='{"text":"Custom Name","color":"red"}']
    
  2. Kill the player:
    (Must be holding the renamed item when you do)
    /damage @p 20 minecraft:arrow by @p
    

Observed & Expected Behavior:

- The game will output the death message "[Player] was shot by [Player] using [Renamed Dirt Block]", despite dying to arrow damage.
- The death message would properly display the name of the item used to shoot the player. If a name is not applicable, it would give a vague message such as "[Player] shot [Player]"

Code Analysis:

This issue stems specifically from the return value of the getLocalizedDeathMessage() method in the DamageSource class.
As stated in the post, the game checks the name of the item the player is currently holding rather than the item used.

   public Component getLocalizedDeathMessage(LivingEntity livingEntity) {
      String s = "death.attack." + this.type().msgId();
      if (this.causingEntity == null && this.directEntity == null) {
         LivingEntity livingentity1 = livingEntity.getKillCredit();
         String s1 = s + ".player";
         return livingentity1 != null ? Component.translatable(s1, livingEntity.getDisplayName(), livingentity1.getDisplayName()) : Component.translatable(s, livingEntity.getDisplayName());
      } else {
         Component component = this.causingEntity == null ? this.directEntity.getDisplayName() : this.causingEntity.getDisplayName();
         Entity entity = this.causingEntity;
         ItemStack itemstack1;
         if (entity instanceof LivingEntity) {
            LivingEntity livingentity = (LivingEntity)entity;
            itemstack1 = livingentity.getMainHandItem();
         } else {
            itemstack1 = ItemStack.EMPTY;
         }

         ItemStack itemstack = itemstack1;
--- > ISSUE START
         return !itemstack.isEmpty() && itemstack.hasCustomHoverName() ? Component.translatable(s + ".item", livingEntity.getDisplayName(), component, itemstack.getDisplayName()) : Component.translatable(s, livingEntity.getDisplayName(), component);
--- > ISSUE END
      }
   }

decompiled code via MCP Reborn

Old Description
This is the original description, when the issue was triaged:

When you kill another player with bow (In second hand) and you have a renamed item (A dirt block with name "Hello worrld" for example) the death message is:

player was shot by <Me> using [Item in main hand (EX: Hello World")