From 5c115116ef25eadb3a5e54b4d093c2c940c7857f Mon Sep 17 00:00:00 2001
From: bloodshot <jdroque@gmail.com>
Date: Mon, 6 Jan 2014 16:29:51 -0500
Subject: [PATCH] Update chest animation after cancelling open event. Fixes
 BUKKIT-1440

Currently if a plugin cancels an InventoryOpenEvent for vanilla chests,
the chest animation for clients is stuck in the open state since
IInventory's closeContainer method is never called. To fix the issue,
closeContainer is called before exiting the display GUI method.
---
 .../net/minecraft/server/EntityPlayer.java    | 56 +++++++++++++++----
 1 file changed, 44 insertions(+), 12 deletions(-)

diff --git a/src/main/java/net/minecraft/server/EntityPlayer.java b/src/main/java/net/minecraft/server/EntityPlayer.java
index 9b80aef541..b5ebce6bf8 100644
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
@@ -576,7 +576,9 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void startCrafting(int i, int j, int k) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerWorkbench(this.inventory, this.world, i, j, k));
-        if(container == null) return;
+        if (container == null) {
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -589,7 +591,9 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void startEnchanting(int i, int j, int k, String s) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerEnchantTable(this.inventory, this.world, i, j, k));
-        if(container == null) return;
+        if (container == null) {
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -602,7 +606,9 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openAnvil(int i, int j, int k) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerAnvil(this.inventory, this.world, i, j, k, this));
-        if(container == null) return;
+        if (container == null) {
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -619,7 +625,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
 
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerChest(this.inventory, iinventory));
-        if(container == null) return;
+        if (container == null) {
+            iinventory.l_(); // Should be closeContainer
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -632,7 +641,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openHopper(TileEntityHopper tileentityhopper) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerHopper(this.inventory, tileentityhopper));
-        if(container == null) return;
+        if (container == null) {
+            tileentityhopper.l_(); // Should be closeContainer
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -645,7 +657,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openMinecartHopper(EntityMinecartHopper entityminecarthopper) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerHopper(this.inventory, entityminecarthopper));
-        if(container == null) return;
+        if (container == null) {
+            entityminecarthopper.l_(); // Should be closeContainer
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -658,7 +673,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openFurnace(TileEntityFurnace tileentityfurnace) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerFurnace(this.inventory, tileentityfurnace));
-        if(container == null) return;
+        if (container == null) {
+            tileentityfurnace.l_(); // Should be closeContainer
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -671,7 +689,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openDispenser(TileEntityDispenser tileentitydispenser) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerDispenser(this.inventory, tileentitydispenser));
-        if(container == null) return;
+        if (container == null) {
+            tileentitydispenser.l_(); // Should be closeContainer
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -684,7 +705,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openBrewingStand(TileEntityBrewingStand tileentitybrewingstand) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerBrewingStand(this.inventory, tileentitybrewingstand));
-        if(container == null) return;
+        if (container == null) {
+            tileentitybrewingstand.l_(); // Should be closeContainer
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -697,7 +721,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openBeacon(TileEntityBeacon tileentitybeacon) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerBeacon(this.inventory, tileentitybeacon));
-        if(container == null) return;
+        if (container == null) {
+            tileentitybeacon.l_(); // Should be closeContainer
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -710,7 +737,9 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openTrade(IMerchant imerchant, String s) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerMerchant(this.inventory, imerchant, this.world));
-        if(container == null) return;
+        if (container == null) {
+            return;
+        }
         // CraftBukkit end
 
         this.nextContainerCounter();
@@ -740,7 +769,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
     public void openHorseInventory(EntityHorse entityhorse, IInventory iinventory) {
         // CraftBukkit start - Inventory open hook
         Container container = CraftEventFactory.callInventoryOpenEvent(this, new ContainerHorse(this.inventory, iinventory, entityhorse));
-        if(container == null) return;
+        if (container == null) {
+            iinventory.l_(); // Should be closeContainer
+            return;
+        }
         // CraftBukkit end
 
         if (this.activeContainer != this.defaultContainer) {