From c472c71eca48a959bb37015a2dbec9f2abf0c1be Mon Sep 17 00:00:00 2001
From: Shane Freeder <theboyetronic@gmail.com>
Date: Tue, 30 Nov 2021 05:30:35 +0000
Subject: [PATCH] Prevent ContainerOpenersCounter openCount from going negative

---
 .../level/block/entity/ContainerOpenersCounter.java.patch    | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/paper-server/patches/sources/net/minecraft/world/level/block/entity/ContainerOpenersCounter.java.patch b/paper-server/patches/sources/net/minecraft/world/level/block/entity/ContainerOpenersCounter.java.patch
index 4985952c79..b594786d46 100644
--- a/paper-server/patches/sources/net/minecraft/world/level/block/entity/ContainerOpenersCounter.java.patch
+++ b/paper-server/patches/sources/net/minecraft/world/level/block/entity/ContainerOpenersCounter.java.patch
@@ -45,11 +45,12 @@
          if (i == 0) {
              this.onOpen(world, pos, state);
              world.gameEvent((Entity) player, (Holder) GameEvent.CONTAINER_OPEN, pos);
-@@ -42,8 +68,19 @@
+@@ -42,8 +68,20 @@
      }
  
      public void decrementOpeners(Player player, Level world, BlockPos pos, BlockState state) {
 +        int oldPower = Math.max(0, Math.min(15, this.openCount)); // CraftBukkit - Get power before new viewer is added
++        if (this.openCount == 0) return; // Paper - Prevent ContainerOpenersCounter openCount from going negative
          int i = this.openCount--;
  
 +        // CraftBukkit start - Call redstone event
@@ -65,7 +66,7 @@
          if (this.openCount == 0) {
              this.onClose(world, pos, state);
              world.gameEvent((Entity) player, (Holder) GameEvent.CONTAINER_CLOSE, pos);
-@@ -72,6 +109,7 @@
+@@ -72,6 +110,7 @@
          }
  
          int i = list.size();