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