From 84400e66b8c1dff0e1b5395af06bc3b3d1bd7f40 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sun, 5 May 2019 18:39:58 +1000 Subject: [PATCH] SPIGOT-4871: Manually opened double chest doesn't have close animation --- .../bukkit/craftbukkit/block/CraftChest.java | 3 +- .../craftbukkit/entity/CraftHumanEntity.java | 11 +- .../inventory/CraftInventoryDoubleChest.java | 12 +- .../inventory/InventoryWrapper.java | 155 ------------------ .../inventory/InventoryWrapperTest.java | 23 --- 5 files changed, 19 insertions(+), 185 deletions(-) delete mode 100644 src/main/java/org/bukkit/craftbukkit/inventory/InventoryWrapper.java delete mode 100644 src/test/java/org/bukkit/craftbukkit/inventory/InventoryWrapperTest.java diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java b/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java index 0e8f1a89fc..6beb992622 100644 --- a/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java +++ b/src/main/java/org/bukkit/craftbukkit/block/CraftChest.java @@ -3,7 +3,6 @@ package org.bukkit.craftbukkit.block; import net.minecraft.server.BlockChest; import net.minecraft.server.Blocks; import net.minecraft.server.ITileInventory; -import net.minecraft.server.InventoryLargeChest; import net.minecraft.server.TileEntityChest; import org.bukkit.Material; import org.bukkit.block.Block; @@ -51,7 +50,7 @@ public class CraftChest extends CraftLootable implements Chest ITileInventory nms = blockChest.getInventory(data, world.getHandle(), this.getPosition()); if (nms instanceof BlockChest.DoubleInventory) { - inventory = new CraftInventoryDoubleChest(((BlockChest.DoubleInventory) nms).inventorylargechest); + inventory = new CraftInventoryDoubleChest((BlockChest.DoubleInventory) nms); } return inventory; } diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java index b7da600ef2..d6d04995ab 100644 --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftHumanEntity.java @@ -55,6 +55,7 @@ import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.event.CraftEventFactory; import org.bukkit.craftbukkit.inventory.CraftContainer; import org.bukkit.craftbukkit.inventory.CraftInventory; +import org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest; import org.bukkit.craftbukkit.inventory.CraftInventoryPlayer; import org.bukkit.craftbukkit.inventory.CraftInventoryView; import org.bukkit.craftbukkit.inventory.CraftItemStack; @@ -319,7 +320,15 @@ public class CraftHumanEntity extends CraftLivingEntity implements HumanEntity { InventoryType type = inventory.getType(); Container formerContainer = getHandle().activeContainer; - IInventory iinventory = (inventory instanceof CraftInventory) ? ((CraftInventory) inventory).getInventory() : new org.bukkit.craftbukkit.inventory.InventoryWrapper(inventory); + ITileInventory iinventory = null; + if (inventory instanceof CraftInventoryDoubleChest) { + iinventory = ((CraftInventoryDoubleChest) inventory).tile; + } else if (inventory instanceof CraftInventory) { + CraftInventory craft = (CraftInventory) inventory; + if (craft.getInventory() instanceof ITileInventory) { + iinventory = (ITileInventory) craft.getInventory(); + } + } if (iinventory instanceof ITileInventory) { if (iinventory instanceof TileEntity) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryDoubleChest.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryDoubleChest.java index 8932d45085..e917db0b01 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryDoubleChest.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftInventoryDoubleChest.java @@ -1,5 +1,7 @@ package org.bukkit.craftbukkit.inventory; +import net.minecraft.server.BlockChest; +import net.minecraft.server.ITileInventory; import net.minecraft.server.InventoryLargeChest; import org.bukkit.Location; import org.bukkit.block.DoubleChest; @@ -8,13 +10,15 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; public class CraftInventoryDoubleChest extends CraftInventory implements DoubleChestInventory { + public ITileInventory tile; private final CraftInventory left; private final CraftInventory right; - public CraftInventoryDoubleChest(CraftInventory left, CraftInventory right) { - super(new InventoryLargeChest(left.getInventory(), right.getInventory())); - this.left = left; - this.right = right; + public CraftInventoryDoubleChest(BlockChest.DoubleInventory block) { + super(block.inventorylargechest); + this.tile = block; + this.left = new CraftInventory(block.inventorylargechest.left); + this.right = new CraftInventory(block.inventorylargechest.right); } public CraftInventoryDoubleChest(InventoryLargeChest largeChest) { diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/InventoryWrapper.java b/src/main/java/org/bukkit/craftbukkit/inventory/InventoryWrapper.java deleted file mode 100644 index a16fc6b03d..0000000000 --- a/src/main/java/org/bukkit/craftbukkit/inventory/InventoryWrapper.java +++ /dev/null @@ -1,155 +0,0 @@ -package org.bukkit.craftbukkit.inventory; - -import com.google.common.base.Predicates; -import com.google.common.collect.Iterables; -import java.util.ArrayList; -import java.util.List; -import net.minecraft.server.EntityHuman; -import net.minecraft.server.IInventory; -import net.minecraft.server.ItemStack; -import org.bukkit.Location; -import org.bukkit.craftbukkit.entity.CraftHumanEntity; -import org.bukkit.entity.HumanEntity; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.InventoryHolder; - -public class InventoryWrapper implements IInventory { - - private final Inventory inventory; - private final List viewers = new ArrayList(); - - public InventoryWrapper(Inventory inventory) { - this.inventory = inventory; - } - - @Override - public int getSize() { - return inventory.getSize(); - } - - @Override - public ItemStack getItem(int i) { - return CraftItemStack.asNMSCopy(inventory.getItem(i)); - } - - @Override - public ItemStack splitStack(int i, int j) { - // Copied from CraftItemStack - ItemStack stack = getItem(i); - ItemStack result; - if (stack.isEmpty()) { - return stack; - } - if (stack.getCount() <= j) { - this.setItem(i, ItemStack.a); - result = stack; - } else { - result = CraftItemStack.copyNMSStack(stack, j); - stack.subtract(j); - } - this.update(); - return result; - } - - @Override - public ItemStack splitWithoutUpdate(int i) { - // Copied from CraftItemStack - ItemStack stack = getItem(i); - ItemStack result; - if (stack.isEmpty()) { - return stack; - } - if (stack.getCount() <= 1) { - this.setItem(i, ItemStack.a); - result = stack; - } else { - result = CraftItemStack.copyNMSStack(stack, 1); - stack.subtract(1); - } - return result; - } - - @Override - public void setItem(int i, ItemStack itemstack) { - inventory.setItem(i, CraftItemStack.asBukkitCopy(itemstack)); - } - - @Override - public int getMaxStackSize() { - return inventory.getMaxStackSize(); - } - - @Override - public void update() { - } - - @Override - public boolean a(EntityHuman entityhuman) { - return true; - } - - @Override - public void startOpen(EntityHuman entityhuman) { - } - - @Override - public void closeContainer(EntityHuman entityhuman) { - } - - @Override - public boolean b(int i, ItemStack itemstack) { - return true; - } - - @Override - public void clear() { - inventory.clear(); - } - - @Override - public List getContents() { - int size = getSize(); - List items = new ArrayList(size); - - for (int i = 0; i < size; i++) { - items.add(getItem(i)); - } - - return items; - } - - @Override - public void onOpen(CraftHumanEntity who) { - viewers.add(who); - } - - @Override - public void onClose(CraftHumanEntity who) { - viewers.remove(who); - } - - @Override - public List getViewers() { - return viewers; - } - - @Override - public InventoryHolder getOwner() { - return inventory.getHolder(); - } - - @Override - public void setMaxStackSize(int size) { - inventory.setMaxStackSize(size); - } - - @Override - public Location getLocation() { - return inventory.getLocation(); - } - - @Override - public boolean isNotEmpty() { - return Iterables.any(inventory, Predicates.notNull()); - } -} diff --git a/src/test/java/org/bukkit/craftbukkit/inventory/InventoryWrapperTest.java b/src/test/java/org/bukkit/craftbukkit/inventory/InventoryWrapperTest.java deleted file mode 100644 index 64c3a7fa9c..0000000000 --- a/src/test/java/org/bukkit/craftbukkit/inventory/InventoryWrapperTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.bukkit.craftbukkit.inventory; - -import junit.framework.Assert; -import org.bukkit.Material; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import org.junit.Test; - -public class InventoryWrapperTest { - - @Test - public void test() { - Inventory raw = new CraftInventoryCustom(null, 27); - raw.addItem(new ItemStack(Material.STONE)); - - Assert.assertTrue(raw.contains(Material.STONE)); - - InventoryWrapper wrapper = new InventoryWrapper(raw); - CraftInventory proxy = new CraftInventory(wrapper); - - Assert.assertTrue(proxy.contains(Material.STONE)); - } -}