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