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