    001package net.minecraft.block;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005
    006import java.util.ArrayList;
    007import java.util.List;
    008import java.util.Random;
    009import net.minecraft.block.material.Material;
    010import net.minecraft.creativetab.CreativeTabs;
    011import net.minecraft.entity.player.EntityPlayer;
    012import net.minecraft.item.Item;
    013import net.minecraft.item.ItemStack;
    014import net.minecraft.stats.StatList;
    015import net.minecraft.world.ColorizerFoliage;
    016import net.minecraft.world.IBlockAccess;
    017import net.minecraft.world.World;
    018
    019import net.minecraftforge.common.IShearable;
    020
    021public class BlockLeaves extends BlockLeavesBase implements IShearable
    022{
    023    /**
    024     * The base index in terrain.png corresponding to the fancy version of the leaf texture. This is stored so we can
    025     * switch the displayed version between fancy and fast graphics (fast is this index + 1).
    026     */
    027    private int baseIndexInPNG;
    028    public static final String[] LEAF_TYPES = new String[] {"oak", "spruce", "birch", "jungle"};
    029    int[] adjacentTreeBlocks;
    030
    031    protected BlockLeaves(int par1, int par2)
    032    {
    033        super(par1, par2, Material.leaves, false);
    034        this.baseIndexInPNG = par2;
    035        this.setTickRandomly(true);
    036        this.setCreativeTab(CreativeTabs.tabDecorations);
    037    }
    038
    039    @SideOnly(Side.CLIENT)
    040    public int getBlockColor()
    041    {
    042        double var1 = 0.5D;
    043        double var3 = 1.0D;
    044        return ColorizerFoliage.getFoliageColor(var1, var3);
    045    }
    046
    047    @SideOnly(Side.CLIENT)
    048
    049    /**
    050     * Returns the color this block should be rendered. Used by leaves.
    051     */
    052    public int getRenderColor(int par1)
    053    {
    054        return (par1 & 3) == 1 ? ColorizerFoliage.getFoliageColorPine() : ((par1 & 3) == 2 ? ColorizerFoliage.getFoliageColorBirch() : ColorizerFoliage.getFoliageColorBasic());
    055    }
    056
    057    @SideOnly(Side.CLIENT)
    058
    059    /**
    060     * Returns a integer with hex for 0xrrggbb with this color multiplied against the blocks color. Note only called
    061     * when first determining what to render.
    062     */
    063    public int colorMultiplier(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
    064    {
    065        int var5 = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
    066
    067        if ((var5 & 3) == 1)
    068        {
    069            return ColorizerFoliage.getFoliageColorPine();
    070        }
    071        else if ((var5 & 3) == 2)
    072        {
    073            return ColorizerFoliage.getFoliageColorBirch();
    074        }
    075        else
    076        {
    077            int var6 = 0;
    078            int var7 = 0;
    079            int var8 = 0;
    080
    081            for (int var9 = -1; var9 <= 1; ++var9)
    082            {
    083                for (int var10 = -1; var10 <= 1; ++var10)
    084                {
    085                    int var11 = par1IBlockAccess.getBiomeGenForCoords(par2 + var10, par4 + var9).getBiomeFoliageColor();
    086                    var6 += (var11 & 16711680) >> 16;
    087                    var7 += (var11 & 65280) >> 8;
    088                    var8 += var11 & 255;
    089                }
    090            }
    091
    092            return (var6 / 9 & 255) << 16 | (var7 / 9 & 255) << 8 | var8 / 9 & 255;
    093        }
    094    }
    095
    096    /**
    097     * ejects contained items into the world, and notifies neighbours of an update, as appropriate
    098     */
    099    public void breakBlock(World par1World, int par2, int par3, int par4, int par5, int par6)
    100    {
    101        byte var7 = 1;
    102        int var8 = var7 + 1;
    103
    104        if (par1World.checkChunksExist(par2 - var8, par3 - var8, par4 - var8, par2 + var8, par3 + var8, par4 + var8))
    105        {
    106            for (int var9 = -var7; var9 <= var7; ++var9)
    107            {
    108                for (int var10 = -var7; var10 <= var7; ++var10)
    109                {
    110                    for (int var11 = -var7; var11 <= var7; ++var11)
    111                    {
    112                        int var12 = par1World.getBlockId(par2 + var9, par3 + var10, par4 + var11);
    113
    114                        if (Block.blocksList[var12] != null)
    115                        {
    116                            Block.blocksList[var12].beginLeavesDecay(par1World, par2 + var9, par3 + var10, par4 + var11);
    117                        }
    118                    }
    119                }
    120            }
    121        }
    122    }
    123
    124    /**
    125     * Ticks the block if it's been scheduled
    126     */
    127    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
    128    {
    129        if (!par1World.isRemote)
    130        {
    131            int var6 = par1World.getBlockMetadata(par2, par3, par4);
    132
    133            if ((var6 & 8) != 0 && (var6 & 4) == 0)
    134            {
    135                byte var7 = 4;
    136                int var8 = var7 + 1;
    137                byte var9 = 32;
    138                int var10 = var9 * var9;
    139                int var11 = var9 / 2;
    140
    141                if (this.adjacentTreeBlocks == null)
    142                {
    143                    this.adjacentTreeBlocks = new int[var9 * var9 * var9];
    144                }
    145
    146                int var12;
    147
    148                if (par1World.checkChunksExist(par2 - var8, par3 - var8, par4 - var8, par2 + var8, par3 + var8, par4 + var8))
    149                {
    150                    int var13;
    151                    int var14;
    152                    int var15;
    153
    154                    for (var12 = -var7; var12 <= var7; ++var12)
    155                    {
    156                        for (var13 = -var7; var13 <= var7; ++var13)
    157                        {
    158                            for (var14 = -var7; var14 <= var7; ++var14)
    159                            {
    160                                var15 = par1World.getBlockId(par2 + var12, par3 + var13, par4 + var14);
    161
    162                                Block block = Block.blocksList[var15];
    163
    164                                if (block != null && block.canSustainLeaves(par1World, par2 + var12, par3 + var13, par4 + var14))
    165                                {
    166                                    this.adjacentTreeBlocks[(var12 + var11) * var10 + (var13 + var11) * var9 + var14 + var11] = 0;
    167                                }
    168                                else if (block != null && block.isLeaves(par1World, par2 + var12, par3 + var13, par4 + var14))
    169                                {
    170                                    this.adjacentTreeBlocks[(var12 + var11) * var10 + (var13 + var11) * var9 + var14 + var11] = -2;
    171                                }
    172                                else
    173                                {
    174                                    this.adjacentTreeBlocks[(var12 + var11) * var10 + (var13 + var11) * var9 + var14 + var11] = -1;
    175                                }
    176                            }
    177                        }
    178                    }
    179
    180                    for (var12 = 1; var12 <= 4; ++var12)
    181                    {
    182                        for (var13 = -var7; var13 <= var7; ++var13)
    183                        {
    184                            for (var14 = -var7; var14 <= var7; ++var14)
    185                            {
    186                                for (var15 = -var7; var15 <= var7; ++var15)
    187                                {
    188                                    if (this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11) * var9 + var15 + var11] == var12 - 1)
    189                                    {
    190                                        if (this.adjacentTreeBlocks[(var13 + var11 - 1) * var10 + (var14 + var11) * var9 + var15 + var11] == -2)
    191                                        {
    192                                            this.adjacentTreeBlocks[(var13 + var11 - 1) * var10 + (var14 + var11) * var9 + var15 + var11] = var12;
    193                                        }
    194
    195                                        if (this.adjacentTreeBlocks[(var13 + var11 + 1) * var10 + (var14 + var11) * var9 + var15 + var11] == -2)
    196                                        {
    197                                            this.adjacentTreeBlocks[(var13 + var11 + 1) * var10 + (var14 + var11) * var9 + var15 + var11] = var12;
    198                                        }
    199
    200                                        if (this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11 - 1) * var9 + var15 + var11] == -2)
    201                                        {
    202                                            this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11 - 1) * var9 + var15 + var11] = var12;
    203                                        }
    204
    205                                        if (this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11 + 1) * var9 + var15 + var11] == -2)
    206                                        {
    207                                            this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11 + 1) * var9 + var15 + var11] = var12;
    208                                        }
    209
    210                                        if (this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11) * var9 + (var15 + var11 - 1)] == -2)
    211                                        {
    212                                            this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11) * var9 + (var15 + var11 - 1)] = var12;
    213                                        }
    214
    215                                        if (this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11) * var9 + var15 + var11 + 1] == -2)
    216                                        {
    217                                            this.adjacentTreeBlocks[(var13 + var11) * var10 + (var14 + var11) * var9 + var15 + var11 + 1] = var12;
    218                                        }
    219                                    }
    220                                }
    221                            }
    222                        }
    223                    }
    224                }
    225
    226                var12 = this.adjacentTreeBlocks[var11 * var10 + var11 * var9 + var11];
    227
    228                if (var12 >= 0)
    229                {
    230                    par1World.setBlockMetadata(par2, par3, par4, var6 & -9);
    231                }
    232                else
    233                {
    234                    this.removeLeaves(par1World, par2, par3, par4);
    235                }
    236            }
    237        }
    238    }
    239
    240    @SideOnly(Side.CLIENT)
    241
    242    /**
    243     * A randomly called display update to be able to add particles or other items for display
    244     */
    245    public void randomDisplayTick(World par1World, int par2, int par3, int par4, Random par5Random)
    246    {
    247        if (par1World.canLightningStrikeAt(par2, par3 + 1, par4) && !par1World.doesBlockHaveSolidTopSurface(par2, par3 - 1, par4) && par5Random.nextInt(15) == 1)
    248        {
    249            double var6 = (double)((float)par2 + par5Random.nextFloat());
    250            double var8 = (double)par3 - 0.05D;
    251            double var10 = (double)((float)par4 + par5Random.nextFloat());
    252            par1World.spawnParticle("dripWater", var6, var8, var10, 0.0D, 0.0D, 0.0D);
    253        }
    254    }
    255
    256    private void removeLeaves(World par1World, int par2, int par3, int par4)
    257    {
    258        this.dropBlockAsItem(par1World, par2, par3, par4, par1World.getBlockMetadata(par2, par3, par4), 0);
    259        par1World.setBlockWithNotify(par2, par3, par4, 0);
    260    }
    261
    262    /**
    263     * Returns the quantity of items to drop on block destruction.
    264     */
    265    public int quantityDropped(Random par1Random)
    266    {
    267        return par1Random.nextInt(20) == 0 ? 1 : 0;
    268    }
    269
    270    /**
    271     * Returns the ID of the items to drop on destruction.
    272     */
    273    public int idDropped(int par1, Random par2Random, int par3)
    274    {
    275        return Block.sapling.blockID;
    276    }
    277
    278    /**
    279     * Drops the block items with a specified chance of dropping the specified items
    280     */
    281    public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7)
    282    {
    283        if (!par1World.isRemote)
    284        {
    285            byte var8 = 20;
    286
    287            if ((par5 & 3) == 3)
    288            {
    289                var8 = 40;
    290            }
    291
    292            if (par1World.rand.nextInt(var8) == 0)
    293            {
    294                int var9 = this.idDropped(par5, par1World.rand, par7);
    295                this.dropBlockAsItem_do(par1World, par2, par3, par4, new ItemStack(var9, 1, this.damageDropped(par5)));
    296            }
    297
    298            if ((par5 & 3) == 0 && par1World.rand.nextInt(200) == 0)
    299            {
    300                this.dropBlockAsItem_do(par1World, par2, par3, par4, new ItemStack(Item.appleRed, 1, 0));
    301            }
    302        }
    303    }
    304
    305    /**
    306     * Called when the player destroys a block with an item that can harvest it. (i, j, k) are the coordinates of the
    307     * block and l is the block's subtype/damage.
    308     */
    309    public void harvestBlock(World par1World, EntityPlayer par2EntityPlayer, int par3, int par4, int par5, int par6)
    310    {
    311        super.harvestBlock(par1World, par2EntityPlayer, par3, par4, par5, par6);
    312    }
    313
    314    /**
    315     * Determines the damage on the item the block drops. Used in cloth and wood.
    316     */
    317    public int damageDropped(int par1)
    318    {
    319        return par1 & 3;
    320    }
    321
    322    /**
    323     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
    324     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
    325     */
    326    public boolean isOpaqueCube()
    327    {
    328        return !this.graphicsLevel;
    329    }
    330
    331    /**
    332     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
    333     */
    334    public int getBlockTextureFromSideAndMetadata(int par1, int par2)
    335    {
    336        return (par2 & 3) == 1 ? this.blockIndexInTexture + 80 : ((par2 & 3) == 3 ? this.blockIndexInTexture + 144 : this.blockIndexInTexture);
    337    }
    338
    339    @SideOnly(Side.CLIENT)
    340
    341    /**
    342     * Pass true to draw this block using fancy graphics, or false for fast graphics.
    343     */
    344    public void setGraphicsLevel(boolean par1)
    345    {
    346        this.graphicsLevel = par1;
    347        this.blockIndexInTexture = this.baseIndexInPNG + (par1 ? 0 : 1);
    348    }
    349
    350    @SideOnly(Side.CLIENT)
    351
    352    /**
    353     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
    354     */
    355    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
    356    {
    357        par3List.add(new ItemStack(par1, 1, 0));
    358        par3List.add(new ItemStack(par1, 1, 1));
    359        par3List.add(new ItemStack(par1, 1, 2));
    360        par3List.add(new ItemStack(par1, 1, 3));
    361    }
    362
    363    /**
    364     * Returns an item stack containing a single instance of the current block type. 'i' is the block's subtype/damage
    365     * and is ignored for blocks which do not support subtypes. Blocks which cannot be harvested should return null.
    366     */
    367    protected ItemStack createStackedBlock(int par1)
    368    {
    369        return new ItemStack(this.blockID, 1, par1 & 3);
    370    }
    371
    372    @Override
    373    public boolean isShearable(ItemStack item, World world, int x, int y, int z)
    374    {
    375        return true;
    376    }
    377
    378    @Override
    379    public ArrayList<ItemStack> onSheared(ItemStack item, World world, int x, int y, int z, int fortune)
    380    {
    381        ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
    382        ret.add(new ItemStack(this, 1, world.getBlockMetadata(x, y, z) & 3));
    383        return ret;
    384    }
    385
    386    @Override
    387    public void beginLeavesDecay(World world, int x, int y, int z)
    388    {
    389        world.setBlockMetadata(x, y, z, world.getBlockMetadata(x, y, z) | 8);
    390    }
    391
    392    @Override
    393    public boolean isLeaves(World world, int x, int y, int z)
    394    {
    395        return true;
    396    }
    397}