    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 getBlockTextureFromSideAndMetadata(int par1, int par2)
    107    {
    108        return par1 == 1 ? this.field_94458_cO : (par1 == 0 ? this.field_94458_cO : (par1 != par2 ? this.field_94336_cN : this.field_94459_cP));
    109    }
    110
    111    @SideOnly(Side.CLIENT)
    112    public void func_94332_a(IconRegister par1IconRegister)
    113    {
    114        this.field_94336_cN = par1IconRegister.func_94245_a("furnace_side");
    115        this.field_94459_cP = par1IconRegister.func_94245_a(this.isActive ? "furnace_front_lit" : "furnace_front");
    116        this.field_94458_cO = par1IconRegister.func_94245_a("furnace_top");
    117    }
    118
    119    /**
    120     * Called upon block activation (right click on the block.)
    121     */
    122    public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    123    {
    124        if (par1World.isRemote)
    125        {
    126            return true;
    127        }
    128        else
    129        {
    130            TileEntityFurnace tileentityfurnace = (TileEntityFurnace)par1World.getBlockTileEntity(par2, par3, par4);
    131
    132            if (tileentityfurnace != null)
    133            {
    134                par5EntityPlayer.displayGUIFurnace(tileentityfurnace);
    135            }
    136
    137            return true;
    138        }
    139    }
    140
    141    /**
    142     * Update which block ID the furnace is using depending on whether or not it is burning
    143     */
    144    public static void updateFurnaceBlockState(boolean par0, World par1World, int par2, int par3, int par4)
    145    {
    146        int l = par1World.getBlockMetadata(par2, par3, par4);
    147        TileEntity tileentity = par1World.getBlockTileEntity(par2, par3, par4);
    148        keepFurnaceInventory = true;
    149
    150        if (par0)
    151        {
    152            par1World.func_94575_c(par2, par3, par4, Block.furnaceBurning.blockID);
    153        }
    154        else
    155        {
    156            par1World.func_94575_c(par2, par3, par4, Block.furnaceIdle.blockID);
    157        }
    158
    159        keepFurnaceInventory = false;
    160        par1World.setBlockMetadataWithNotify(par2, par3, par4, l, 2);
    161
    162        if (tileentity != null)
    163        {
    164            tileentity.validate();
    165            par1World.setBlockTileEntity(par2, par3, par4, tileentity);
    166        }
    167    }
    168
    169    @SideOnly(Side.CLIENT)
    170
    171    /**
    172     * A randomly called display update to be able to add particles or other items for display
    173     */
    174    public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    175    {
    176        if (this.isActive)
    177        {
    178            int l = par1World.getBlockMetadata(par2, par3, par4);
    179            float f = (float)par2 + 0.5F;
    180            float f1 = (float)par3 + 0.0F + par5Random.nextFloat() * 6.0F / 16.0F;
    181            float f2 = (float)par4 + 0.5F;
    182            float f3 = 0.52F;
    183            float f4 = par5Random.nextFloat() * 0.6F - 0.3F;
    184
    185            if (l == 4)
    186            {
    187                par1World.spawnParticle("smoke", (double)(f - f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
    188                par1World.spawnParticle("flame", (double)(f - f3), (double)f1, (double)(f2 + f4), 0.0D, 0.0D, 0.0D);
    189            }
    190            else if (l == 5)
    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 == 2)
    196            {
    197                par1World.spawnParticle("smoke", (double)(f + f4), (double)f1, (double)(f2 - f3), 0.0D, 0.0D, 0.0D);
    198                par1World.spawnParticle("flame", (double)(f + f4), (double)f1, (double)(f2 - f3), 0.0D, 0.0D, 0.0D);
    199            }
    200            else if (l == 3)
    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        }
    206    }
    207
    208    /**
    209     * Returns a new instance of a block's tile entity class. Called on placing the block.
    210     */
    211    public TileEntity createNewTileEntity(World par1World)
    212    {
    213        return new TileEntityFurnace();
    214    }
    215
    216    /**
    217     * Called when the block is placed in the world.
    218     */
    219    public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving, ItemStack par6ItemStack)
    220    {
    221        int l = MathHelper.floor_double((double)(par5EntityLiving.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;
    222
    223        if (l == 0)
    224        {
    225            par1World.setBlockMetadataWithNotify(par2, par3, par4, 2, 2);
    226        }
    227
    228        if (l == 1)
    229        {
    230            par1World.setBlockMetadataWithNotify(par2, par3, par4, 5, 2);
    231        }
    232
    233        if (l == 2)
    234        {
    235            par1World.setBlockMetadataWithNotify(par2, par3, par4, 3, 2);
    236        }
    237
    238        if (l == 3)
    239        {
    240            par1World.setBlockMetadataWithNotify(par2, par3, par4, 4, 2);
    241        }
    242
    243        if (par6ItemStack.hasDisplayName())
    244        {
    245            ((TileEntityFurnace)par1World.getBlockTileEntity(par2, par3, par4)).func_94129_a(par6ItemStack.getDisplayName());
    246        }
    247    }
    248
    249    /**
    250     * ejects contained items into the world, and notifies neighbours of an update, as appropriate
    251     */
    252    public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
    253    {
    254        if (!keepFurnaceInventory)
    255        {
    256            TileEntityFurnace tileentityfurnace = (TileEntityFurnace)par1World.getBlockTileEntity(par2, par3, par4);
    257
    258            if (tileentityfurnace != null)
    259            {
    260                for (int j1 = 0; j1 < tileentityfurnace.getSizeInventory(); ++j1)
    261                {
    262                    ItemStack itemstack = tileentityfurnace.getStackInSlot(j1);
    263
    264                    if (itemstack != null)
    265                    {
    266                        float f = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
    267                        float f1 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
    268                        float f2 = this.furnaceRand.nextFloat() * 0.8F + 0.1F;
    269
    270                        while (itemstack.stackSize > 0)
    271                        {
    272                            int k1 = this.furnaceRand.nextInt(21) + 10;
    273
    274                            if (k1 > itemstack.stackSize)
    275                            {
    276                                k1 = itemstack.stackSize;
    277                            }
    278
    279                            itemstack.stackSize -= k1;
    280                            EntityItem entityitem = new EntityItem(par1World, (double)((float)par2 + f), (double)((float)par3 + f1), (double)((float)par4 + f2), new ItemStack(itemstack.itemID, k1, itemstack.getItemDamage()));
    281
    282                            if (itemstack.hasTagCompound())
    283                            {
    284                                entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
    285                            }
    286
    287                            float f3 = 0.05F;
    288                            entityitem.motionX = (double)((float)this.furnaceRand.nextGaussian() * f3);
    289                            entityitem.motionY = (double)((float)this.furnaceRand.nextGaussian() * f3 + 0.2F);
    290                            entityitem.motionZ = (double)((float)this.furnaceRand.nextGaussian() * f3);
    291                            par1World.spawnEntityInWorld(entityitem);
    292                        }
    293                    }
    294                }
    295            }
    296        }
    297
    298        super.breakBlock(par1World, par2, par3, par4, par5, par6);
    299    }
    300
    301    public boolean func_96468_q_()
    302    {
    303        return true;
    304    }
    305
    306    public int func_94328_b_(World par1World, int par2, int par3, int par4, int par5)
    307    {
    308        return Container.func_94526_b((IInventory)par1World.getBlockTileEntity(par2, par3, par4));
    309    }
    310}