Mojira Archive
MC-222950

Piglins become aggressive when attempting to open locked blocks that they guard

The Bug:

Piglins become aggressive when attempting to open locked blocks that they guard.

Steps to Reproduce:

  1. Enter survival mode and equip a piece of golden armor.
  2. Summon a locked chest by using the command provided below.
    /setblock ~ ~ ~ minecraft:chest{lock:{components:{"minecraft:custom_name":'"Mojira"'}}}
  3. Summon a piglin by using the command provided below.
    /summon minecraft:piglin ~ ~ ~ {IsImmuneToZombification:1b}
  4. Attempt to open the chest and observe the behavior of the piglin.

Observed Behavior:

Piglins become aggressive.

Expected Behavior:

Piglins would not become aggressive.

Code Analysis:

Code analysis by [Mod] Avoma can be found below.

The following is based on a decompiled version of Minecraft 1.18.2 using MCP-Reborn.

net.minecraft.world.level.block.ChestBlock.java
public class ChestBlock extends AbstractChestBlock<ChestBlockEntity> implements SimpleWaterloggedBlock {
   ...
   public InteractionResult use(BlockState $bs, Level $l, BlockPos $bp, Player $p, InteractionHand $ih, BlockHitResult $bhr) {
      if ($l.isClientSide) {
         return InteractionResult.SUCCESS;
      } else {
         MenuProvider menuprovider = this.getMenuProvider($bs, $l, $bp);
         if (menuprovider != null) {
            $p.openMenu(menuprovider);
            $p.awardStat(this.getOpenChestStat());
            PiglinAi.angerNearbyPiglins($p, true);
         }

         return InteractionResult.CONSUME;
      }
   }
   ...

If we look at the above class, we can see that no checks are carried out to see whether the desired chest contains a lock when attempting to open it, before angering nearby piglins. The angerNearbyPiglins() method will always be called when the chest is right-clicked on, regardless of whether or not it has a lock, resulting in this issue occurring.