001package net.minecraft.tileentity;
002
003import cpw.mods.fml.common.registry.GameRegistry;
004import cpw.mods.fml.relauncher.Side;
005import cpw.mods.fml.relauncher.SideOnly;
006import net.minecraft.block.Block;
007import net.minecraft.block.BlockFurnace;
008import net.minecraft.block.material.Material;
009import net.minecraft.entity.player.EntityPlayer;
010import net.minecraft.inventory.ISidedInventory;
011import net.minecraft.item.Item;
012import net.minecraft.item.ItemBlock;
013import net.minecraft.item.ItemHoe;
014import net.minecraft.item.ItemStack;
015import net.minecraft.item.ItemSword;
016import net.minecraft.item.ItemTool;
017import net.minecraft.item.crafting.FurnaceRecipes;
018import net.minecraft.nbt.NBTTagCompound;
019import net.minecraft.nbt.NBTTagList;
020import net.minecraftforge.common.ForgeDirection;
021import net.minecraftforge.common.ForgeDummyContainer;
022
023public class TileEntityFurnace extends TileEntity implements ISidedInventory, net.minecraftforge.common.ISidedInventory
024{
025    private static final int[] field_102010_d = new int[] {0};
026    private static final int[] field_102011_e = new int[] {2, 1};
027    private static final int[] field_102009_f = new int[] {1};
028
029    /**
030     * The ItemStacks that hold the items currently being used in the furnace
031     */
032    private ItemStack[] furnaceItemStacks = new ItemStack[3];
033
034    /** The number of ticks that the furnace will keep burning */
035    public int furnaceBurnTime = 0;
036
037    /**
038     * The number of ticks that a fresh copy of the currently-burning item would keep the furnace burning for
039     */
040    public int currentItemBurnTime = 0;
041
042    /** The number of ticks that the current item has been cooking for */
043    public int furnaceCookTime = 0;
044    private String field_94130_e;
045
046    /**
047     * Returns the number of slots in the inventory.
048     */
049    public int getSizeInventory()
050    {
051        return this.furnaceItemStacks.length;
052    }
053
054    /**
055     * Returns the stack in slot i
056     */
057    public ItemStack getStackInSlot(int par1)
058    {
059        return this.furnaceItemStacks[par1];
060    }
061
062    /**
063     * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
064     * new stack.
065     */
066    public ItemStack decrStackSize(int par1, int par2)
067    {
068        if (this.furnaceItemStacks[par1] != null)
069        {
070            ItemStack itemstack;
071
072            if (this.furnaceItemStacks[par1].stackSize <= par2)
073            {
074                itemstack = this.furnaceItemStacks[par1];
075                this.furnaceItemStacks[par1] = null;
076                return itemstack;
077            }
078            else
079            {
080                itemstack = this.furnaceItemStacks[par1].splitStack(par2);
081
082                if (this.furnaceItemStacks[par1].stackSize == 0)
083                {
084                    this.furnaceItemStacks[par1] = null;
085                }
086
087                return itemstack;
088            }
089        }
090        else
091        {
092            return null;
093        }
094    }
095
096    /**
097     * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
098     * like when you close a workbench GUI.
099     */
100    public ItemStack getStackInSlotOnClosing(int par1)
101    {
102        if (this.furnaceItemStacks[par1] != null)
103        {
104            ItemStack itemstack = this.furnaceItemStacks[par1];
105            this.furnaceItemStacks[par1] = null;
106            return itemstack;
107        }
108        else
109        {
110            return null;
111        }
112    }
113
114    /**
115     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
116     */
117    public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
118    {
119        this.furnaceItemStacks[par1] = par2ItemStack;
120
121        if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
122        {
123            par2ItemStack.stackSize = this.getInventoryStackLimit();
124        }
125    }
126
127    /**
128     * Returns the name of the inventory.
129     */
130    public String getInvName()
131    {
132        return this.isInvNameLocalized() ? this.field_94130_e : "container.furnace";
133    }
134
135    /**
136     * If this returns false, the inventory name will be used as an unlocalized name, and translated into the player's
137     * language. Otherwise it will be used directly.
138     */
139    public boolean isInvNameLocalized()
140    {
141        return this.field_94130_e != null && this.field_94130_e.length() > 0;
142    }
143
144    public void func_94129_a(String par1Str)
145    {
146        this.field_94130_e = par1Str;
147    }
148
149    /**
150     * Reads a tile entity from NBT.
151     */
152    public void readFromNBT(NBTTagCompound par1NBTTagCompound)
153    {
154        super.readFromNBT(par1NBTTagCompound);
155        NBTTagList nbttaglist = par1NBTTagCompound.getTagList("Items");
156        this.furnaceItemStacks = new ItemStack[this.getSizeInventory()];
157
158        for (int i = 0; i < nbttaglist.tagCount(); ++i)
159        {
160            NBTTagCompound nbttagcompound1 = (NBTTagCompound)nbttaglist.tagAt(i);
161            byte b0 = nbttagcompound1.getByte("Slot");
162
163            if (b0 >= 0 && b0 < this.furnaceItemStacks.length)
164            {
165                this.furnaceItemStacks[b0] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
166            }
167        }
168
169        this.furnaceBurnTime = par1NBTTagCompound.getShort("BurnTime");
170        this.furnaceCookTime = par1NBTTagCompound.getShort("CookTime");
171        this.currentItemBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
172
173        if (par1NBTTagCompound.hasKey("CustomName"))
174        {
175            this.field_94130_e = par1NBTTagCompound.getString("CustomName");
176        }
177    }
178
179    /**
180     * Writes a tile entity to NBT.
181     */
182    public void writeToNBT(NBTTagCompound par1NBTTagCompound)
183    {
184        super.writeToNBT(par1NBTTagCompound);
185        par1NBTTagCompound.setShort("BurnTime", (short)this.furnaceBurnTime);
186        par1NBTTagCompound.setShort("CookTime", (short)this.furnaceCookTime);
187        NBTTagList nbttaglist = new NBTTagList();
188
189        for (int i = 0; i < this.furnaceItemStacks.length; ++i)
190        {
191            if (this.furnaceItemStacks[i] != null)
192            {
193                NBTTagCompound nbttagcompound1 = new NBTTagCompound();
194                nbttagcompound1.setByte("Slot", (byte)i);
195                this.furnaceItemStacks[i].writeToNBT(nbttagcompound1);
196                nbttaglist.appendTag(nbttagcompound1);
197            }
198        }
199
200        par1NBTTagCompound.setTag("Items", nbttaglist);
201
202        if (this.isInvNameLocalized())
203        {
204            par1NBTTagCompound.setString("CustomName", this.field_94130_e);
205        }
206    }
207
208    /**
209     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
210     * this more of a set than a get?*
211     */
212    public int getInventoryStackLimit()
213    {
214        return 64;
215    }
216
217    @SideOnly(Side.CLIENT)
218
219    /**
220     * Returns an integer between 0 and the passed value representing how close the current item is to being completely
221     * cooked
222     */
223    public int getCookProgressScaled(int par1)
224    {
225        return this.furnaceCookTime * par1 / 200;
226    }
227
228    @SideOnly(Side.CLIENT)
229
230    /**
231     * Returns an integer between 0 and the passed value representing how much burn time is left on the current fuel
232     * item, where 0 means that the item is exhausted and the passed value means that the item is fresh
233     */
234    public int getBurnTimeRemainingScaled(int par1)
235    {
236        if (this.currentItemBurnTime == 0)
237        {
238            this.currentItemBurnTime = 200;
239        }
240
241        return this.furnaceBurnTime * par1 / this.currentItemBurnTime;
242    }
243
244    /**
245     * Returns true if the furnace is currently burning
246     */
247    public boolean isBurning()
248    {
249        return this.furnaceBurnTime > 0;
250    }
251
252    /**
253     * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count
254     * ticks and creates a new spawn inside its implementation.
255     */
256    public void updateEntity()
257    {
258        boolean flag = this.furnaceBurnTime > 0;
259        boolean flag1 = false;
260
261        if (this.furnaceBurnTime > 0)
262        {
263            --this.furnaceBurnTime;
264        }
265
266        if (!this.worldObj.isRemote)
267        {
268            if (this.furnaceBurnTime == 0 && this.canSmelt())
269            {
270                this.currentItemBurnTime = this.furnaceBurnTime = getItemBurnTime(this.furnaceItemStacks[1]);
271
272                if (this.furnaceBurnTime > 0)
273                {
274                    flag1 = true;
275
276                    if (this.furnaceItemStacks[1] != null)
277                    {
278                        --this.furnaceItemStacks[1].stackSize;
279
280                        if (this.furnaceItemStacks[1].stackSize == 0)
281                        {
282                            this.furnaceItemStacks[1] = this.furnaceItemStacks[1].getItem().getContainerItemStack(furnaceItemStacks[1]);
283                        }
284                    }
285                }
286            }
287
288            if (this.isBurning() && this.canSmelt())
289            {
290                ++this.furnaceCookTime;
291
292                if (this.furnaceCookTime == 200)
293                {
294                    this.furnaceCookTime = 0;
295                    this.smeltItem();
296                    flag1 = true;
297                }
298            }
299            else
300            {
301                this.furnaceCookTime = 0;
302            }
303
304            if (flag != this.furnaceBurnTime > 0)
305            {
306                flag1 = true;
307                BlockFurnace.updateFurnaceBlockState(this.furnaceBurnTime > 0, this.worldObj, this.xCoord, this.yCoord, this.zCoord);
308            }
309        }
310
311        if (flag1)
312        {
313            this.onInventoryChanged();
314        }
315    }
316
317    /**
318     * Returns true if the furnace can smelt an item, i.e. has a source item, destination stack isn't full, etc.
319     */
320    private boolean canSmelt()
321    {
322        if (this.furnaceItemStacks[0] == null)
323        {
324            return false;
325        }
326        else
327        {
328            ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.furnaceItemStacks[0]);
329            if (itemstack == null) return false;
330            if (this.furnaceItemStacks[2] == null) return true;
331            if (!this.furnaceItemStacks[2].isItemEqual(itemstack)) return false;
332            int result = furnaceItemStacks[2].stackSize + itemstack.stackSize;
333            return (result <= getInventoryStackLimit() && result <= itemstack.getMaxStackSize());
334        }
335    }
336
337    /**
338     * Turn one item from the furnace source stack into the appropriate smelted item in the furnace result stack
339     */
340    public void smeltItem()
341    {
342        if (this.canSmelt())
343        {
344            ItemStack itemstack = FurnaceRecipes.smelting().getSmeltingResult(this.furnaceItemStacks[0]);
345
346            if (this.furnaceItemStacks[2] == null)
347            {
348                this.furnaceItemStacks[2] = itemstack.copy();
349            }
350            else if (this.furnaceItemStacks[2].isItemEqual(itemstack))
351            {
352                furnaceItemStacks[2].stackSize += itemstack.stackSize;
353            }
354
355            --this.furnaceItemStacks[0].stackSize;
356
357            if (this.furnaceItemStacks[0].stackSize <= 0)
358            {
359                this.furnaceItemStacks[0] = null;
360            }
361        }
362    }
363
364    /**
365     * Returns the number of ticks that the supplied fuel item will keep the furnace burning, or 0 if the item isn't
366     * fuel
367     */
368    public static int getItemBurnTime(ItemStack par0ItemStack)
369    {
370        if (par0ItemStack == null)
371        {
372            return 0;
373        }
374        else
375        {
376            int i = par0ItemStack.getItem().itemID;
377            Item item = par0ItemStack.getItem();
378
379            if (par0ItemStack.getItem() instanceof ItemBlock && Block.blocksList[i] != null)
380            {
381                Block block = Block.blocksList[i];
382
383                if (block == Block.woodSingleSlab)
384                {
385                    return 150;
386                }
387
388                if (block.blockMaterial == Material.wood)
389                {
390                    return 300;
391                }
392            }
393
394            if (item instanceof ItemTool && ((ItemTool) item).getToolMaterialName().equals("WOOD")) return 200;
395            if (item instanceof ItemSword && ((ItemSword) item).getToolMaterialName().equals("WOOD")) return 200;
396            if (item instanceof ItemHoe && ((ItemHoe) item).func_77842_f().equals("WOOD")) return 200;
397            if (i == Item.stick.itemID) return 100;
398            if (i == Item.coal.itemID) return 1600;
399            if (i == Item.bucketLava.itemID) return 20000;
400            if (i == Block.sapling.blockID) return 100;
401            if (i == Item.blazeRod.itemID) return 2400;
402            return GameRegistry.getFuelValue(par0ItemStack);
403        }
404    }
405
406    /**
407     * Return true if item is a fuel source (getItemBurnTime() > 0).
408     */
409    public static boolean isItemFuel(ItemStack par0ItemStack)
410    {
411        return getItemBurnTime(par0ItemStack) > 0;
412    }
413
414    /**
415     * Do not make give this method the name canInteractWith because it clashes with Container
416     */
417    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
418    {
419        return this.worldObj.getBlockTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : par1EntityPlayer.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
420    }
421
422    public void openChest() {}
423
424    public void closeChest() {}
425
426    /**
427     * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot.
428     */
429    public boolean isStackValidForSlot(int par1, ItemStack par2ItemStack)
430    {
431        return par1 == 2 ? false : (par1 == 1 ? isItemFuel(par2ItemStack) : true);
432    }
433
434    /**
435     * Get the size of the side inventory.
436     */
437    public int[] getSizeInventorySide(int par1)
438    {
439        return par1 == 0 ? field_102011_e : (par1 == 1 ? field_102010_d : field_102009_f);
440    }
441
442    public boolean func_102007_a(int par1, ItemStack par2ItemStack, int par3)
443    {
444        return this.isStackValidForSlot(par1, par2ItemStack);
445    }
446
447    public boolean func_102008_b(int par1, ItemStack par2ItemStack, int par3)
448    {
449        return par3 != 0 || par1 != 1 || par2ItemStack.itemID == Item.bucketEmpty.itemID;
450    }
451
452    /***********************************************************************************
453     * This function is here for compatibilities sake, Modders should Check for
454     * Sided before ContainerWorldly, Vanilla Minecraft does not follow the sided standard
455     * that Modding has for a while.
456     *
457     * In vanilla:
458     *
459     *   Top: Ores
460     *   Sides: Fuel
461     *   Bottom: Output
462     *
463     * Standard Modding:
464     *   Top: Ores
465     *   Sides: Output
466     *   Bottom: Fuel
467     *
468     * The Modding one is designed after the GUI, the vanilla one is designed because its
469     * intended use is for the hopper, which logically would take things in from the top.
470     *
471     * This will possibly be removed in future updates, and make vanilla the definitive
472     * standard.
473     */
474
475    @Override
476    public int getStartInventorySide(ForgeDirection side)
477    {
478        if (ForgeDummyContainer.legacyFurnaceSides)
479        {
480            if (side == ForgeDirection.DOWN) return 1;
481            if (side == ForgeDirection.UP) return 0;
482            return 2;
483        }
484        else
485        {
486            if (side == ForgeDirection.DOWN) return 2;
487            if (side == ForgeDirection.UP) return 0;
488            return 1;
489        }
490    }
491
492    @Override
493    public int getSizeInventorySide(ForgeDirection side)
494    {
495        return 1;
496    }
497}