    001package net.minecraft.inventory;
    002
    003import net.minecraft.entity.player.EntityPlayer;
    004import net.minecraft.item.ItemStack;
    005
    006public class ContainerChest extends Container
    007{
    008    private IInventory lowerChestInventory;
    009    private int numRows;
    010
    011    public ContainerChest(IInventory par1IInventory, IInventory par2IInventory)
    012    {
    013        this.lowerChestInventory = par2IInventory;
    014        this.numRows = par2IInventory.getSizeInventory() / 9;
    015        par2IInventory.openChest();
    016        int i = (this.numRows - 4) * 18;
    017        int j;
    018        int k;
    019
    020        for (j = 0; j < this.numRows; ++j)
    021        {
    022            for (k = 0; k < 9; ++k)
    023            {
    024                this.addSlotToContainer(new Slot(par2IInventory, k + j * 9, 8 + k * 18, 18 + j * 18));
    025            }
    026        }
    027
    028        for (j = 0; j < 3; ++j)
    029        {
    030            for (k = 0; k < 9; ++k)
    031            {
    032                this.addSlotToContainer(new Slot(par1IInventory, k + j * 9 + 9, 8 + k * 18, 103 + j * 18 + i));
    033            }
    034        }
    035
    036        for (j = 0; j < 9; ++j)
    037        {
    038            this.addSlotToContainer(new Slot(par1IInventory, j, 8 + j * 18, 161 + i));
    039        }
    040    }
    041
    042    public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    043    {
    044        return this.lowerChestInventory.isUseableByPlayer(par1EntityPlayer);
    045    }
    046
    047    /**
    048     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    049     */
    050    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    051    {
    052        ItemStack itemstack = null;
    053        Slot slot = (Slot)this.inventorySlots.get(par2);
    054
    055        if (slot != null && slot.getHasStack())
    056        {
    057            ItemStack itemstack1 = slot.getStack();
    058            itemstack = itemstack1.copy();
    059
    060            if (par2 < this.numRows * 9)
    061            {
    062                if (!this.mergeItemStack(itemstack1, this.numRows * 9, this.inventorySlots.size(), true))
    063                {
    064                    return null;
    065                }
    066            }
    067            else if (!this.mergeItemStack(itemstack1, 0, this.numRows * 9, false))
    068            {
    069                return null;
    070            }
    071
    072            if (itemstack1.stackSize == 0)
    073            {
    074                slot.putStack((ItemStack)null);
    075            }
    076            else
    077            {
    078                slot.onSlotChanged();
    079            }
    080        }
    081
    082        return itemstack;
    083    }
    084
    085    /**
    086     * Callback for when the crafting gui is closed.
    087     */
    088    public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
    089    {
    090        super.onCraftGuiClosed(par1EntityPlayer);
    091        this.lowerChestInventory.closeChest();
    092    }
    093
    094    /**
    095     * Return this chest container's lower chest inventory.
    096     */
    097    public IInventory getLowerChestInventory()
    098    {
    099        return this.lowerChestInventory;
    100    }
    101}