    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.item.crafting.FurnaceRecipes;
    009import net.minecraft.tileentity.TileEntityFurnace;
    010
    011public class ContainerFurnace extends Container
    012{
    013    private TileEntityFurnace furnace;
    014    private int lastCookTime = 0;
    015    private int lastBurnTime = 0;
    016    private int lastItemBurnTime = 0;
    017
    018    public ContainerFurnace(InventoryPlayer par1InventoryPlayer, TileEntityFurnace par2TileEntityFurnace)
    019    {
    020        this.furnace = par2TileEntityFurnace;
    021        this.addSlotToContainer(new Slot(par2TileEntityFurnace, 0, 56, 17));
    022        this.addSlotToContainer(new Slot(par2TileEntityFurnace, 1, 56, 53));
    023        this.addSlotToContainer(new SlotFurnace(par1InventoryPlayer.player, par2TileEntityFurnace, 2, 116, 35));
    024        int i;
    025
    026        for (i = 0; i < 3; ++i)
    027        {
    028            for (int j = 0; j < 9; ++j)
    029            {
    030                this.addSlotToContainer(new Slot(par1InventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
    031            }
    032        }
    033
    034        for (i = 0; i < 9; ++i)
    035        {
    036            this.addSlotToContainer(new Slot(par1InventoryPlayer, i, 8 + i * 18, 142));
    037        }
    038    }
    039
    040    public void addCraftingToCrafters(ICrafting par1ICrafting)
    041    {
    042        super.addCraftingToCrafters(par1ICrafting);
    043        par1ICrafting.sendProgressBarUpdate(this, 0, this.furnace.furnaceCookTime);
    044        par1ICrafting.sendProgressBarUpdate(this, 1, this.furnace.furnaceBurnTime);
    045        par1ICrafting.sendProgressBarUpdate(this, 2, this.furnace.currentItemBurnTime);
    046    }
    047
    048    /**
    049     * Looks for changes made in the container, sends them to every listener.
    050     */
    051    public void detectAndSendChanges()
    052    {
    053        super.detectAndSendChanges();
    054
    055        for (int i = 0; i < this.crafters.size(); ++i)
    056        {
    057            ICrafting icrafting = (ICrafting)this.crafters.get(i);
    058
    059            if (this.lastCookTime != this.furnace.furnaceCookTime)
    060            {
    061                icrafting.sendProgressBarUpdate(this, 0, this.furnace.furnaceCookTime);
    062            }
    063
    064            if (this.lastBurnTime != this.furnace.furnaceBurnTime)
    065            {
    066                icrafting.sendProgressBarUpdate(this, 1, this.furnace.furnaceBurnTime);
    067            }
    068
    069            if (this.lastItemBurnTime != this.furnace.currentItemBurnTime)
    070            {
    071                icrafting.sendProgressBarUpdate(this, 2, this.furnace.currentItemBurnTime);
    072            }
    073        }
    074
    075        this.lastCookTime = this.furnace.furnaceCookTime;
    076        this.lastBurnTime = this.furnace.furnaceBurnTime;
    077        this.lastItemBurnTime = this.furnace.currentItemBurnTime;
    078    }
    079
    080    @SideOnly(Side.CLIENT)
    081    public void updateProgressBar(int par1, int par2)
    082    {
    083        if (par1 == 0)
    084        {
    085            this.furnace.furnaceCookTime = par2;
    086        }
    087
    088        if (par1 == 1)
    089        {
    090            this.furnace.furnaceBurnTime = par2;
    091        }
    092
    093        if (par1 == 2)
    094        {
    095            this.furnace.currentItemBurnTime = par2;
    096        }
    097    }
    098
    099    public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    100    {
    101        return this.furnace.isUseableByPlayer(par1EntityPlayer);
    102    }
    103
    104    /**
    105     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    106     */
    107    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    108    {
    109        ItemStack itemstack = null;
    110        Slot slot = (Slot)this.inventorySlots.get(par2);
    111
    112        if (slot != null && slot.getHasStack())
    113        {
    114            ItemStack itemstack1 = slot.getStack();
    115            itemstack = itemstack1.copy();
    116
    117            if (par2 == 2)
    118            {
    119                if (!this.mergeItemStack(itemstack1, 3, 39, true))
    120                {
    121                    return null;
    122                }
    123
    124                slot.onSlotChange(itemstack1, itemstack);
    125            }
    126            else if (par2 != 1 && par2 != 0)
    127            {
    128                if (FurnaceRecipes.smelting().getSmeltingResult(itemstack1) != null)
    129                {
    130                    if (!this.mergeItemStack(itemstack1, 0, 1, false))
    131                    {
    132                        return null;
    133                    }
    134                }
    135                else if (TileEntityFurnace.isItemFuel(itemstack1))
    136                {
    137                    if (!this.mergeItemStack(itemstack1, 1, 2, false))
    138                    {
    139                        return null;
    140                    }
    141                }
    142                else if (par2 >= 3 && par2 < 30)
    143                {
    144                    if (!this.mergeItemStack(itemstack1, 30, 39, false))
    145                    {
    146                        return null;
    147                    }
    148                }
    149                else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))
    150                {
    151                    return null;
    152                }
    153            }
    154            else if (!this.mergeItemStack(itemstack1, 3, 39, false))
    155            {
    156                return null;
    157            }
    158
    159            if (itemstack1.stackSize == 0)
    160            {
    161                slot.putStack((ItemStack)null);
    162            }
    163            else
    164            {
    165                slot.onSlotChanged();
    166            }
    167
    168            if (itemstack1.stackSize == itemstack.stackSize)
    169            {
    170                return null;
    171            }
    172
    173            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
    174        }
    175
    176        return itemstack;
    177    }
    178}