    001package net.minecraft.inventory;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import net.minecraft.entity.player.EntityPlayer;
    006import net.minecraft.entity.player.InventoryPlayer;
    007import net.minecraft.item.ItemStack;
    008import net.minecraft.tileentity.TileEntityBrewingStand;
    009
    010public class ContainerBrewingStand extends Container
    011{
    012    private TileEntityBrewingStand tileBrewingStand;
    013
    014    /** Instance of Slot. */
    015    private final Slot theSlot;
    016    private int brewTime = 0;
    017
    018    public ContainerBrewingStand(InventoryPlayer par1InventoryPlayer, TileEntityBrewingStand par2TileEntityBrewingStand)
    019    {
    020        this.tileBrewingStand = par2TileEntityBrewingStand;
    021        this.addSlotToContainer(new SlotBrewingStandPotion(par1InventoryPlayer.player, par2TileEntityBrewingStand, 0, 56, 46));
    022        this.addSlotToContainer(new SlotBrewingStandPotion(par1InventoryPlayer.player, par2TileEntityBrewingStand, 1, 79, 53));
    023        this.addSlotToContainer(new SlotBrewingStandPotion(par1InventoryPlayer.player, par2TileEntityBrewingStand, 2, 102, 46));
    024        this.theSlot = this.addSlotToContainer(new SlotBrewingStandIngredient(this, par2TileEntityBrewingStand, 3, 79, 17));
    025        int i;
    026
    027        for (i = 0; i < 3; ++i)
    028        {
    029            for (int j = 0; j < 9; ++j)
    030            {
    031                this.addSlotToContainer(new Slot(par1InventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
    032            }
    033        }
    034
    035        for (i = 0; i < 9; ++i)
    036        {
    037            this.addSlotToContainer(new Slot(par1InventoryPlayer, i, 8 + i * 18, 142));
    038        }
    039    }
    040
    041    public void addCraftingToCrafters(ICrafting par1ICrafting)
    042    {
    043        super.addCraftingToCrafters(par1ICrafting);
    044        par1ICrafting.sendProgressBarUpdate(this, 0, this.tileBrewingStand.getBrewTime());
    045    }
    046
    047    /**
    048     * Looks for changes made in the container, sends them to every listener.
    049     */
    050    public void detectAndSendChanges()
    051    {
    052        super.detectAndSendChanges();
    053
    054        for (int i = 0; i < this.crafters.size(); ++i)
    055        {
    056            ICrafting icrafting = (ICrafting)this.crafters.get(i);
    057
    058            if (this.brewTime != this.tileBrewingStand.getBrewTime())
    059            {
    060                icrafting.sendProgressBarUpdate(this, 0, this.tileBrewingStand.getBrewTime());
    061            }
    062        }
    063
    064        this.brewTime = this.tileBrewingStand.getBrewTime();
    065    }
    066
    067    @SideOnly(Side.CLIENT)
    068    public void updateProgressBar(int par1, int par2)
    069    {
    070        if (par1 == 0)
    071        {
    072            this.tileBrewingStand.setBrewTime(par2);
    073        }
    074    }
    075
    076    public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    077    {
    078        return this.tileBrewingStand.isUseableByPlayer(par1EntityPlayer);
    079    }
    080
    081    /**
    082     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    083     */
    084    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    085    {
    086        ItemStack itemstack = null;
    087        Slot slot = (Slot)this.inventorySlots.get(par2);
    088
    089        if (slot != null && slot.getHasStack())
    090        {
    091            ItemStack itemstack1 = slot.getStack();
    092            itemstack = itemstack1.copy();
    093
    094            if ((par2 < 0 || par2 > 2) && par2 != 3)
    095            {
    096                if (!this.theSlot.getHasStack() && this.theSlot.isItemValid(itemstack1))
    097                {
    098                    if (!this.mergeItemStack(itemstack1, 3, 4, false))
    099                    {
    100                        return null;
    101                    }
    102                }
    103                else if (SlotBrewingStandPotion.canHoldPotion(itemstack))
    104                {
    105                    if (!this.mergeItemStack(itemstack1, 0, 3, false))
    106                    {
    107                        return null;
    108                    }
    109                }
    110                else if (par2 >= 4 && par2 < 31)
    111                {
    112                    if (!this.mergeItemStack(itemstack1, 31, 40, false))
    113                    {
    114                        return null;
    115                    }
    116                }
    117                else if (par2 >= 31 && par2 < 40)
    118                {
    119                    if (!this.mergeItemStack(itemstack1, 4, 31, false))
    120                    {
    121                        return null;
    122                    }
    123                }
    124                else if (!this.mergeItemStack(itemstack1, 4, 40, false))
    125                {
    126                    return null;
    127                }
    128            }
    129            else
    130            {
    131                if (!this.mergeItemStack(itemstack1, 4, 40, true))
    132                {
    133                    return null;
    134                }
    135
    136                slot.onSlotChange(itemstack1, itemstack);
    137            }
    138
    139            if (itemstack1.stackSize == 0)
    140            {
    141                slot.putStack((ItemStack)null);
    142            }
    143            else
    144            {
    145                slot.onSlotChanged();
    146            }
    147
    148            if (itemstack1.stackSize == itemstack.stackSize)
    149            {
    150                return null;
    151            }
    152
    153            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
    154        }
    155
    156        return itemstack;
    157    }
    158}