001    package net.minecraft.src;
002    
003    public class ContainerChest extends Container
004    {
005        private IInventory lowerChestInventory;
006        private int numRows;
007    
008        public ContainerChest(IInventory par1IInventory, IInventory par2IInventory)
009        {
010            this.lowerChestInventory = par2IInventory;
011            this.numRows = par2IInventory.getSizeInventory() / 9;
012            par2IInventory.openChest();
013            int var3 = (this.numRows - 4) * 18;
014            int var4;
015            int var5;
016    
017            for (var4 = 0; var4 < this.numRows; ++var4)
018            {
019                for (var5 = 0; var5 < 9; ++var5)
020                {
021                    this.addSlotToContainer(new Slot(par2IInventory, var5 + var4 * 9, 8 + var5 * 18, 18 + var4 * 18));
022                }
023            }
024    
025            for (var4 = 0; var4 < 3; ++var4)
026            {
027                for (var5 = 0; var5 < 9; ++var5)
028                {
029                    this.addSlotToContainer(new Slot(par1IInventory, var5 + var4 * 9 + 9, 8 + var5 * 18, 103 + var4 * 18 + var3));
030                }
031            }
032    
033            for (var4 = 0; var4 < 9; ++var4)
034            {
035                this.addSlotToContainer(new Slot(par1IInventory, var4, 8 + var4 * 18, 161 + var3));
036            }
037        }
038    
039        public boolean canInteractWith(EntityPlayer par1EntityPlayer)
040        {
041            return this.lowerChestInventory.isUseableByPlayer(par1EntityPlayer);
042        }
043    
044        /**
045         * Called to transfer a stack from one inventory to the other eg. when shift clicking.
046         */
047        public ItemStack transferStackInSlot(int par1)
048        {
049            ItemStack var2 = null;
050            Slot var3 = (Slot)this.inventorySlots.get(par1);
051    
052            if (var3 != null && var3.getHasStack())
053            {
054                ItemStack var4 = var3.getStack();
055                var2 = var4.copy();
056    
057                if (par1 < this.numRows * 9)
058                {
059                    if (!this.mergeItemStack(var4, this.numRows * 9, this.inventorySlots.size(), true))
060                    {
061                        return null;
062                    }
063                }
064                else if (!this.mergeItemStack(var4, 0, this.numRows * 9, false))
065                {
066                    return null;
067                }
068    
069                if (var4.stackSize == 0)
070                {
071                    var3.putStack((ItemStack)null);
072                }
073                else
074                {
075                    var3.onSlotChanged();
076                }
077            }
078    
079            return var2;
080        }
081    
082        /**
083         * Callback for when the crafting gui is closed.
084         */
085        public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
086        {
087            super.onCraftGuiClosed(par1EntityPlayer);
088            this.lowerChestInventory.closeChest();
089        }
090    }