    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005    import java.util.Random;
    006
    007    public class BlockFurnace extends BlockContainer
    008    {
    009        /**
    010         * Is the random generator used by furnace to drop the inventory contents in random directions.
    011         */
    012        private Random furnaceRand = new Random();
    013
    014        /** True if this is an active furnace, false if idle */
    015        private final boolean isActive;
    016
    017        /**
    018         * This flag is used to prevent the furnace inventory to be dropped upon block removal, is used internally when the
    019         * furnace block changes from idle to active and vice-versa.
    020         */
    021        private static boolean keepFurnaceInventory = false;
    022
    023        protected BlockFurnace(int par1, boolean par2)
    024        {
    025            super(par1, Material.rock);
    026            this.isActive = par2;
    027            this.blockIndexInTexture = 45;
    028        }
    029
    030        /**
    031         * Returns the ID of the items to drop on destruction.
    032         */
    033        public int idDropped(int par1, Random par2Random, int par3)
    034        {
    035            return Block.stoneOvenIdle.blockID;
    036        }
    037
    038        /**
    039         * Called whenever the block is added into the world. Args: world, x, y, z
    040         */
    041        public void onBlockAdded(World par1World, int par2, int par3, int par4)
    042        {
    043            super.onBlockAdded(par1World, par2, par3, par4);
    044            this.setDefaultDirection(par1World, par2, par3, par4);
    045        }
    046
    047        /**
    048         * set a blocks direction
    049         */
    050        private void setDefaultDirection(World par1World, int par2, int par3, int par4)
    051        {
    052            if (!par1World.isRemote)
    053            {
    054                int var5 = par1World.getBlockId(par2, par3, par4 - 1);
    055                int var6 = par1World.getBlockId(par2, par3, par4 + 1);
    056                int var7 = par1World.getBlockId(par2 - 1, par3, par4);
    057                int var8 = par1World.getBlockId(par2 + 1, par3, par4);
    058                byte var9 = 3;
    059
    060                if (Block.opaqueCubeLookup[var5] && !Block.opaqueCubeLookup[var6])
    061                {
    062                    var9 = 3;
    063                }
    064
    065                if (Block.opaqueCubeLookup[var6] && !Block.opaqueCubeLookup[var5])
    066                {
    067                    var9 = 2;
    068                }
    069
    070                if (Block.opaqueCubeLookup[var7] && !Block.opaqueCubeLookup[var8])
    071                {
    072                    var9 = 5;
    073                }
    074
    075                if (Block.opaqueCubeLookup[var8] && !Block.opaqueCubeLookup[var7])
    076                {
    077                    var9 = 4;
    078                }
    079
    080                par1World.setBlockMetadataWithNotify(par2, par3, par4, var9);
    081            }
    082        }
    083
    084        @SideOnly(Side.CLIENT)
    085
    086        /**
    087         * Retrieves the block texture to use based on the display side. Args: iBlockAccess, x, y, z, side
    088         */
    089        public int getBlockTexture(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
    090        {
    091            if (par5 == 1)
    092            {
    093                return this.blockIndexInTexture + 17;
    094            }
    095            else if (par5 == 0)
    096            {
    097                return this.blockIndexInTexture + 17;
    098            }
    099            else
    100            {
    101                int var6 = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
    102                return par5 != var6 ? this.blockIndexInTexture : (this.isActive ? this.blockIndexInTexture + 16 : this.blockIndexInTexture - 1);
    103            }
    104        }
    105
    106        @SideOnly(Side.CLIENT)
    107
    108        /**
    109         * A randomly called display update to be able to add particles or other items for display
    110         */
    111        public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    112        {
    113            if (this.isActive)
    114            {
    115                int var6 = par1World.getBlockMetadata(par2, par3, par4);
    116                float var7 = (float)par2 + 0.5F;
    117                float var8 = (float)par3 + 0.0F + par5Random.nextFloat() * 6.0F / 16.0F;
    118                float var9 = (float)par4 + 0.5F;
    119                float var10 = 0.52F;
    120                float var11 = par5Random.nextFloat() * 0.6F - 0.3F;
    121
    122                if (var6 == 4)
    123                {
    124                    par1World.spawnParticle("smoke", (double)(var7 - var10), (double)var8, (double)(var9 + var11), 0.0D, 0.0D, 0.0D);
    125                    par1World.spawnParticle("flame", (double)(var7 - var10), (double)var8, (double)(var9 + var11), 0.0D, 0.0D, 0.0D);
    126                }
    127                else if (var6 == 5)
    128                {
    129                    par1World.spawnParticle("smoke", (double)(var7 + var10), (double)var8, (double)(var9 + var11), 0.0D, 0.0D, 0.0D);
    130                    par1World.spawnParticle("flame", (double)(var7 + var10), (double)var8, (double)(var9 + var11), 0.0D, 0.0D, 0.0D);
    131                }
    132                else if (var6 == 2)
    133                {
    134                    par1World.spawnParticle("smoke", (double)(var7 + var11), (double)var8, (double)(var9 - var10), 0.0D, 0.0D, 0.0D);
    135                    par1World.spawnParticle("flame", (double)(var7 + var11), (double)var8, (double)(var9 - var10), 0.0D, 0.0D, 0.0D);
    136                }
    137                else if (var6 == 3)
    138                {
    139                    par1World.spawnParticle("smoke", (double)(var7 + var11), (double)var8, (double)(var9 + var10), 0.0D, 0.0D, 0.0D);
    140                    par1World.spawnParticle("flame", (double)(var7 + var11), (double)var8, (double)(var9 + var10), 0.0D, 0.0D, 0.0D);
    141                }
    142            }
    143        }
    144
    145        /**
    146         * Returns the block texture based on the side being looked at.  Args: side
    147         */
    148        public int getBlockTextureFromSide(int par1)
    149        {
    150            return par1 == 1 ? this.blockIndexInTexture + 17 : (par1 == 0 ? this.blockIndexInTexture + 17 : (par1 == 3 ? this.blockIndexInTexture - 1 : this.blockIndexInTexture));
    151        }
    152
    153        /**
    154         * Called upon block activation (right click on the block.)
    155         */
    156        public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    157        {
    158            if (par1World.isRemote)
    159            {
    160                return true;
    161            }
    162            else
    163            {
    164                TileEntityFurnace var10 = (TileEntityFurnace)par1World.getBlockTileEntity(par2, par3, par4);
    165
    166                if (var10 != null)
    167                {
    168                    par5EntityPlayer.displayGUIFurnace(var10);
    169                }
    170
    171                return true;
    172            }
    173        }
    174
    175        /**
    176         * Update which block ID the furnace is using depending on whether or not it is burning
    177         */
    178        public static void updateFurnaceBlockState(boolean par0, World par1World, int par2, int par3, int par4)
    179        {
    180            int var5 = par1World.getBlockMetadata(par2, par3, par4);
    181            TileEntity var6 = par1World.getBlockTileEntity(par2, par3, par4);
    182            keepFurnaceInventory = true;
    183
    184            if (par0)
    185            {
    186                par1World.setBlockWithNotify(par2, par3, par4, Block.stoneOvenActive.blockID);
    187            }
    188            else
    189            {
    190                par1World.setBlockWithNotify(par2, par3, par4, Block.stoneOvenIdle.blockID);
    191            }
    192
    193            keepFurnaceInventory = false;
    194            par1World.setBlockMetadataWithNotify(par2, par3, par4, var5);
    195
    196            if (var6 != null)
    197            {
    198                var6.validate();
    199                par1World.setBlockTileEntity(par2, par3, par4, var6);
    200            }
    201        }
    202
    203        /**
    204         * Returns a new instance of a block's tile entity class. Called on placing the block.
    205         */
    206        public TileEntity createNewTileEntity(World par1World)
    207        {
    208            return new TileEntityFurnace();
    209        }
    210
    211        /**
    212         * Called when the block is placed in the world.
    213         */
    214        public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)
    215        {
    216            int var6 = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
    217
    218            if (var6 == 0)
    219            {
    220                par1World.setBlockMetadataWithNotify(par2, par3, par4, 2);
    221            }
    222
    223            if (var6 == 1)
    224            {
    225                par1World.setBlockMetadataWithNotify(par2, par3, par4, 5);
    226            }
    227
    228            if (var6 == 2)
    229            {
    230                par1World.setBlockMetadataWithNotify(par2, par3, par4, 3);
    231            }
    232
    233            if (var6 == 3)
    234            {
    235                par1World.setBlockMetadataWithNotify(par2, par3, par4, 4);
    236            }
    237        }
    238
    239        /**
    240         * ejects contained items into the world, and notifies neighbours of an update, as appropriate
    241         */
    242        public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
    243        {
    244            if (!keepFurnaceInventory)
    245            {
    246                TileEntityFurnace var7 = (TileEntityFurnace)par1World.getBlockTileEntity(par2, par3, par4);
    247
    248                if (var7 != null)
    249                {
    250                    for (int var8 = 0; var8 < var7.getSizeInventory(); ++var8)
    251                    {
    252                        ItemStack var9 = var7.getStackInSlot(var8);
    253
    254                        if (var9 != null)
    255                        {
    256                            float var10 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
    257                            float var11 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
    258                            float var12 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
    259
    260                            while (var9.stackSize > 0)
    261                            {
    262                                int var13 = this.furnaceRand.nextInt(21) + 10;
    263
    264                                if (var13 > var9.stackSize)
    265                                {
    266                                    var13 = var9.stackSize;
    267                                }
    268
    269                                var9.stackSize -= var13;
    270                                EntityItem var14 = new EntityItem(par1World, (double)((float)par2 + var10), (double)((float)par3 + var11), (double)((float)par4 + var12), new ItemStack(var9.itemID, var13, var9.getItemDamage()));
    271
    272                                if (var9.hasTagCompound())
    273                                {
    274                                    var14.item.setTagCompound((NBTTagCompound)var9.getTagCompound().copy());
    275                                }
    276
    277                                float var15 = 0.05F;
    278                                var14.motionX = (double)((float)this.furnaceRand.nextGaussian() * var15);
    279                                var14.motionY = (double)((float)this.furnaceRand.nextGaussian() * var15 + 0.2F);
    280                                var14.motionZ = (double)((float)this.furnaceRand.nextGaussian() * var15);
    281                                par1World.spawnEntityInWorld(var14);
    282                            }
    283                        }
    284                    }
    285                }
    286            }
    287
    288            super.breakBlock(par1World, par2, par3, par4, par5, par6);
    289        }
    290    }