001package net.minecraft.block;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.util.Random;
006import net.minecraft.block.material.Material;
007import net.minecraft.client.renderer.texture.IconRegister;
008import net.minecraft.entity.EntityLiving;
009import net.minecraft.entity.item.EntityItem;
010import net.minecraft.entity.player.EntityPlayer;
011import net.minecraft.inventory.Container;
012import net.minecraft.inventory.IInventory;
013import net.minecraft.item.ItemStack;
014import net.minecraft.nbt.NBTTagCompound;
015import net.minecraft.tileentity.TileEntity;
016import net.minecraft.tileentity.TileEntityFurnace;
017import net.minecraft.util.Icon;
018import net.minecraft.util.MathHelper;
019import net.minecraft.world.World;
020
021public class BlockFurnace extends BlockContainer
022{
023    /**
024     * Is the random generator used by furnace to drop the inventory contents in random directions.
025     */
026    private final Random furnaceRand = new Random();
027
028    /** True if this is an active furnace, false if idle */
029    private final boolean isActive;
030
031    /**
032     * This flag is used to prevent the furnace inventory to be dropped upon block removal, is used internally when the
033     * furnace block changes from idle to active and vice-versa.
034     */
035    private static boolean keepFurnaceInventory = false;
036    @SideOnly(Side.CLIENT)
037    private Icon field_94458_cO;
038    @SideOnly(Side.CLIENT)
039    private Icon field_94459_cP;
040
041    protected BlockFurnace(int par1, boolean par2)
042    {
043        super(par1, Material.rock);
044        this.isActive = par2;
045    }
046
047    /**
048     * Returns the ID of the items to drop on destruction.
049     */
050    public int idDropped(int par1, Random par2Random, int par3)
051    {
052        return Block.furnaceIdle.blockID;
053    }
054
055    /**
056     * Called whenever the block is added into the world. Args: world, x, y, z
057     */
058    public void onBlockAdded(World par1World, int par2, int par3, int par4)
059    {
060        super.onBlockAdded(par1World, par2, par3, par4);
061        this.setDefaultDirection(par1World, par2, par3, par4);
062    }
063
064    /**
065     * set a blocks direction
066     */
067    private void setDefaultDirection(World par1World, int par2, int par3, int par4)
068    {
069        if (!par1World.isRemote)
070        {
071            int l = par1World.getBlockId(par2, par3, par4 - 1);
072            int i1 = par1World.getBlockId(par2, par3, par4 + 1);
073            int j1 = par1World.getBlockId(par2 - 1, par3, par4);
074            int k1 = par1World.getBlockId(par2 + 1, par3, par4);
075            byte b0 = 3;
076
077            if (Block.opaqueCubeLookup[l] && !Block.opaqueCubeLookup[i1])
078            {
079                b0 = 3;
080            }
081
082            if (Block.opaqueCubeLookup[i1] && !Block.opaqueCubeLookup[l])
083            {
084                b0 = 2;
085            }
086
087            if (Block.opaqueCubeLookup[j1] && !Block.opaqueCubeLookup[k1])
088            {
089                b0 = 5;
090            }
091
092            if (Block.opaqueCubeLookup[k1] && !Block.opaqueCubeLookup[j1])
093            {
094                b0 = 4;
095            }
096
097            par1World.setBlockMetadataWithNotify(par2, par3, par4, b0, 2);
098        }
099    }
100
101    @SideOnly(Side.CLIENT)
102
103    /**
104     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
105     */
106    public Icon getIcon(int par1, int par2)
107    {
108        return par1 == 1 ? this.field_94458_cO : (par1 == 0 ? this.field_94458_cO : (par1 != par2 ? this.blockIcon : this.field_94459_cP));
109    }
110
111    @SideOnly(Side.CLIENT)
112
113    /**
114     * When this method is called, your block should register all the icons it needs with the given IconRegister. This
115     * is the only chance you get to register icons.
116     */
117    public void registerIcons(IconRegister par1IconRegister)
118    {
119        this.blockIcon = par1IconRegister.registerIcon("furnace_side");
120        this.field_94459_cP = par1IconRegister.registerIcon(this.isActive ? "furnace_front_lit" : "furnace_front");
121        this.field_94458_cO = par1IconRegister.registerIcon("furnace_top");
122    }
123
124    /**
125     * Called upon block activation (right click on the block.)
126     */
127    public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
128    {
129        if (par1World.isRemote)
130        {
131            return true;
132        }
133        else
134        {
135            TileEntityFurnace tileentityfurnace = (TileEntityFurnace)par1World.getBlockTileEntity(par2, par3, par4);
136
137            if (tileentityfurnace != null)
138            {
139                par5EntityPlayer.displayGUIFurnace(tileentityfurnace);
140            }
141
142            return true;
143        }
144    }
145
146    /**
147     * Update which block ID the furnace is using depending on whether or not it is burning
148     */
149    public static void updateFurnaceBlockState(boolean par0, World par1World, int par2, int par3, int par4)
150    {
151        int l = par1World.getBlockMetadata(par2, par3, par4);
152        TileEntity tileentity = par1World.getBlockTileEntity(par2, par3, par4);
153        keepFurnaceInventory = true;
154
155        if (par0)
156        {
157            par1World.setBlock(par2, par3, par4, Block.furnaceBurning.blockID);
158        }
159        else
160        {
161            par1World.setBlock(par2, par3, par4, Block.furnaceIdle.blockID);
162        }
163
164        keepFurnaceInventory = false;
165        par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2);
166
167        if (tileentity != null)
168        {
169            tileentity.validate();
170            par1World.setBlockTileEntity(par2, par3, par4, tileentity);
171        }
172    }
173
174    @SideOnly(Side.CLIENT)
175
176    /**
177     * A randomly called display update to be able to add particles or other items for display
178     */
179    public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
180    {
181        if (this.isActive)
182        {
183            int l = par1World.getBlockMetadata(par2, par3, par4);
184            float f = (float)par2 + 0.5F;
185            float f1 = (float)par3 + 0.0F + par5Random.nextFloat() * 6.0F / 16.0F;
186            float f2 = (float)par4 + 0.5F;
187            float f3 = 0.52F;
188            float f4 = par5Random.nextFloat() * 0.6F - 0.3F;
189
190            if (l == 4)
191            {
192                par1World.spawnParticle("smoke", (double)(f - f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
193                par1World.spawnParticle("flame", (double)(f - f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
194            }
195            else if (l == 5)
196            {
197                par1World.spawnParticle("smoke", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
198                par1World.spawnParticle("flame", (double)(f + f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
199            }
200            else if (l == 2)
201            {
202                par1World.spawnParticle("smoke", (double)(f + f4), (double)f1, (double)(f2 - f3), 0.0D, 0.0D, 0.0D);
203                par1World.spawnParticle("flame", (double)(f + f4), (double)f1, (double)(f2 - f3), 0.0D, 0.0D, 0.0D);
204            }
205            else if (l == 3)
206            {
207                par1World.spawnParticle("smoke", (double)(f + f4), (double)f1, (double)(f2 + f3), 0.0D, 0.0D, 0.0D);
208                par1World.spawnParticle("flame", (double)(f + f4), (double)f1, (double)(f2 + f3), 0.0D, 0.0D, 0.0D);
209            }
210        }
211    }
212
213    /**
214     * Returns a new instance of a block's tile entity class. Called on placing the block.
215     */
216    public TileEntity createNewTileEntity(World par1World)
217    {
218        return new TileEntityFurnace();
219    }
220
221    /**
222     * Called when the block is placed in the world.
223     */
224    public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving, ItemStack par6ItemStack)
225    {
226        int l = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
227
228        if (l == 0)
229        {
230            par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
231        }
232
233        if (l == 1)
234        {
235            par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
236        }
237
238        if (l == 2)
239        {
240            par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
241        }
242
243        if (l == 3)
244        {
245            par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
246        }
247
248        if (par6ItemStack.hasDisplayName())
249        {
250            ((TileEntityFurnace)par1World.getBlockTileEntity(par2, par3, par4)).func_94129_a(par6ItemStack.getDisplayName());
251        }
252    }
253
254    /**
255     * ejects contained items into the world, and notifies neighbours of an update, as appropriate
256     */
257    public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
258    {
259        if (!keepFurnaceInventory)
260        {
261            TileEntityFurnace tileentityfurnace = (TileEntityFurnace)par1World.getBlockTileEntity(par2, par3, par4);
262
263            if (tileentityfurnace != null)
264            {
265                for (int j1 = 0; j1 < tileentityfurnace.getSizeInventory(); ++j1)
266                {
267                    ItemStack itemstack = tileentityfurnace.getStackInSlot(j1);
268
269                    if (itemstack != null)
270                    {
271                        float f = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
272                        float f1 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
273                        float f2 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
274
275                        while (itemstack.stackSize > 0)
276                        {
277                            int k1 = this.furnaceRand.nextInt(21) + 10;
278
279                            if (k1 > itemstack.stackSize)
280                            {
281                                k1 = itemstack.stackSize;
282                            }
283
284                            itemstack.stackSize -= k1;
285                            EntityItem entityitem = new EntityItem(par1World, (double)((float)par2 + f), (double)((float)par3 + f1), (double)((float)par4 + f2), new ItemStack(itemstack.itemID, k1, itemstack.getItemDamage()));
286
287                            if (itemstack.hasTagCompound())
288                            {
289                                entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
290                            }
291
292                            float f3 = 0.05F;
293                            entityitem.motionX = (double)((float)this.furnaceRand.nextGaussian() * f3);
294                            entityitem.motionY = (double)((float)this.furnaceRand.nextGaussian() * f3 + 0.2F);
295                            entityitem.motionZ = (double)((float)this.furnaceRand.nextGaussian() * f3);
296                            par1World.spawnEntityInWorld(entityitem);
297                        }
298                    }
299                }
300
301                par1World.func_96440_m(par2, par3, par4, par5);
302            }
303        }
304
305        super.breakBlock(par1World, par2, par3, par4, par5, par6);
306    }
307
308    /**
309     * If this returns true, then comparators facing away from this block will use the value from
310     * getComparatorInputOverride instead of the actual redstone signal strength.
311     */
312    public boolean hasComparatorInputOverride()
313    {
314        return true;
315    }
316
317    /**
318     * If hasComparatorInputOverride returns true, the return value from this is used instead of the redstone signal
319     * strength when this block inputs to a comparator.
320     */
321    public int getComparatorInputOverride(World par1World, int par2, int par3, int par4, int par5)
322    {
323        return Container.func_94526_b((IInventory)par1World.getBlockTileEntity(par2, par3, par4));
324    }
325}