Mojira Archive
MC-75532

"minecraft.custom:minecraft.open_<LOCKABLE_BLOCK>" and "minecraft.custom:minecraft.inspect_<LOCKABLE_BLOCK>" incorrectly increase when attempting to open or inspect locked blocks

The Bug:

"minecraft.custom:minecraft.open_<LOCKABLE_BLOCK>" and "minecraft.custom:minecraft.inspect_<LOCKABLE_BLOCK>" incorrectly increase when attempting to open or inspect locked blocks.

Steps to Reproduce:

  1. Create a scoreboard objective for tracking when you open a chest and set it to display on the sidebar by using the commands provided below.
    /scoreboard objectives add OpenChest minecraft.custom:minecraft.open_chest
    /scoreboard objectives setdisplay sidebar OpenChest
  2. Place down a chest, open it, and take note of how the scoreboard correctly increases.
  3. Summon a locked chest by using the command provided below.
    /setblock ~ ~ ~ minecraft:chest{lock:{components:{"minecraft:custom_name":'"Mojira"'}}}
  4. Attempt to open it.
  5. Take note as to whether or not "minecraft.custom:minecraft.open_<LOCKABLE_BLOCK>" and "minecraft.custom:minecraft.inspect_<LOCKABLE_BLOCK>" incorrectly increase when attempting to open or inspect locked blocks.

Observed Behavior:

The scoreboard incorrectly increases.

Expected Behavior:

The scoreboard would not increase.

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 the see whether or not the desired chest contains a lock when attempting to open it, before awarding the appropriate statistic. The awardStat() 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.