--- a/net/minecraft/world/level/block/entity/BaseContainerBlockEntity.java
+++ b/net/minecraft/world/level/block/entity/BaseContainerBlockEntity.java
@@ -1,5 +1,6 @@
 package net.minecraft.world.level.block.entity;
 
+import java.util.Iterator;
 import javax.annotation.Nullable;
 import net.minecraft.core.BlockPos;
 import net.minecraft.core.HolderLookup;
@@ -23,12 +24,14 @@
 import net.minecraft.world.level.block.state.BlockState;
 
 public abstract class BaseContainerBlockEntity extends BlockEntity implements Container, MenuProvider, Nameable {
-    public LockCode lockKey = LockCode.NO_LOCK;
+
+    public LockCode lockKey;
     @Nullable
     public Component name;
 
     protected BaseContainerBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
         super(type, pos, state);
+        this.lockKey = LockCode.NO_LOCK;
     }
 
     @Override
@@ -38,6 +41,7 @@
         if (nbt.contains("CustomName", 8)) {
             this.name = parseCustomNameSafe(nbt.getString("CustomName"), registryLookup);
         }
+
     }
 
     @Override
@@ -47,6 +51,7 @@
         if (this.name != null) {
             nbt.putString("CustomName", Component.Serializer.toJson(this.name, registryLookup));
         }
+
     }
 
     @Override
@@ -68,17 +73,44 @@
     protected abstract Component getDefaultName();
 
     public boolean canOpen(Player player) {
-        return canUnlock(player, this.lockKey, this.getDisplayName());
+        return BaseContainerBlockEntity.canUnlock(player, this.lockKey, this.getDisplayName(), this); // Paper - Add BlockLockCheckEvent
     }
 
+    @Deprecated @io.papermc.paper.annotation.DoNotUse // Paper - Add BlockLockCheckEvent
     public static boolean canUnlock(Player player, LockCode lock, Component containerName) {
+        // Paper start - Add BlockLockCheckEvent
+        return canUnlock(player, lock, containerName, null);
+    }
+    public static boolean canUnlock(Player player, LockCode lock, Component containerName, @Nullable BlockEntity blockEntity) {
+        if (player instanceof net.minecraft.server.level.ServerPlayer serverPlayer && blockEntity != null && blockEntity.getLevel() != null && blockEntity.getLevel().getBlockEntity(blockEntity.getBlockPos()) == blockEntity) {
+            final org.bukkit.block.Block block = org.bukkit.craftbukkit.block.CraftBlock.at(blockEntity.getLevel(), blockEntity.getBlockPos());
+            net.kyori.adventure.text.Component lockedMessage = net.kyori.adventure.text.Component.translatable("container.isLocked", io.papermc.paper.adventure.PaperAdventure.asAdventure(containerName));
+            net.kyori.adventure.sound.Sound lockedSound = net.kyori.adventure.sound.Sound.sound(org.bukkit.Sound.BLOCK_CHEST_LOCKED, net.kyori.adventure.sound.Sound.Source.BLOCK, 1.0F, 1.0F);
+            final io.papermc.paper.event.block.BlockLockCheckEvent event = new io.papermc.paper.event.block.BlockLockCheckEvent(block, (io.papermc.paper.block.LockableTileState) block.getState(), serverPlayer.getBukkitEntity(), lockedMessage, lockedSound);
+            event.callEvent();
+            if (event.getResult() == org.bukkit.event.Event.Result.ALLOW) {
+                return true;
+            } else if (event.getResult() == org.bukkit.event.Event.Result.DENY || (!player.isSpectator() && !lock.unlocksWith(event.isUsingCustomKeyItemStack() ? org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(event.getKeyItem()) : player.getMainHandItem()))) {
+                if (event.getLockedMessage() != null) {
+                    event.getPlayer().sendActionBar(event.getLockedMessage());
+                }
+                if (event.getLockedSound() != null) {
+                    event.getPlayer().playSound(event.getLockedSound());
+                }
+                return false;
+            } else {
+                return true;
+            }
+        } else { // logic below is replaced by logic above
+        // Paper end - Add BlockLockCheckEvent
         if (!player.isSpectator() && !lock.unlocksWith(player.getMainHandItem())) {
-            player.displayClientMessage(Component.translatable("container.isLocked", containerName), true);
+            player.displayClientMessage(Component.translatable("container.isLocked", containerName), true); // Paper - diff on change
             player.playNotifySound(SoundEvents.CHEST_LOCKED, SoundSource.BLOCKS, 1.0F, 1.0F);
             return false;
         } else {
             return true;
         }
+        } // Paper - Add BlockLockCheckEvent
     }
 
     protected abstract NonNullList<ItemStack> getItems();
@@ -87,28 +119,35 @@
 
     @Override
     public boolean isEmpty() {
-        for (ItemStack itemStack : this.getItems()) {
-            if (!itemStack.isEmpty()) {
-                return false;
+        Iterator iterator = this.getItems().iterator();
+
+        ItemStack itemstack;
+
+        do {
+            if (!iterator.hasNext()) {
+                return true;
             }
-        }
 
-        return true;
+            itemstack = (ItemStack) iterator.next();
+        } while (itemstack.isEmpty());
+
+        return false;
     }
 
     @Override
     public ItemStack getItem(int slot) {
-        return this.getItems().get(slot);
+        return (ItemStack) this.getItems().get(slot);
     }
 
     @Override
     public ItemStack removeItem(int slot, int amount) {
-        ItemStack itemStack = ContainerHelper.removeItem(this.getItems(), slot, amount);
-        if (!itemStack.isEmpty()) {
+        ItemStack itemstack = ContainerHelper.removeItem(this.getItems(), slot, amount);
+
+        if (!itemstack.isEmpty()) {
             this.setChanged();
         }
 
-        return itemStack;
+        return itemstack;
     }
 
     @Override
@@ -144,9 +183,9 @@
     @Override
     protected void applyImplicitComponents(BlockEntity.DataComponentInput components) {
         super.applyImplicitComponents(components);
-        this.name = components.get(DataComponents.CUSTOM_NAME);
-        this.lockKey = components.getOrDefault(DataComponents.LOCK, LockCode.NO_LOCK);
-        components.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY).copyInto(this.getItems());
+        this.name = (Component) components.get(DataComponents.CUSTOM_NAME);
+        this.lockKey = (LockCode) components.getOrDefault(DataComponents.LOCK, LockCode.NO_LOCK);
+        ((ItemContainerContents) components.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY)).copyInto(this.getItems());
     }
 
     @Override
@@ -166,4 +205,12 @@
         nbt.remove("Lock");
         nbt.remove("Items");
     }
+
+    // CraftBukkit start
+    @Override
+    public org.bukkit.Location getLocation() {
+        if (this.level == null) return null;
+        return new org.bukkit.Location(this.level.getWorld(), this.worldPosition.getX(), this.worldPosition.getY(), this.worldPosition.getZ());
+    }
+    // CraftBukkit end
 }
