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.client.renderer.texture.IconRegister;
011import net.minecraft.creativetab.CreativeTabs;
012import net.minecraft.entity.player.EntityPlayer;
013import net.minecraft.item.Item;
014import net.minecraft.item.ItemStack;
015import net.minecraft.stats.StatList;
016import net.minecraft.util.Icon;
017import net.minecraft.world.ColorizerFoliage;
018import net.minecraft.world.IBlockAccess;
019import net.minecraft.world.World;
020
021import net.minecraftforge.common.IShearable;
022
023public class BlockLeaves extends BlockLeavesBase implements IShearable
024{
025    public static final String[] LEAF_TYPES = new String[] {"oak", "spruce", "birch", "jungle"};
026    public static final String[][] field_94396_b = new String[][] {{"leaves", "leaves_spruce", "leaves", "leaves_jungle"}, {"leaves_opaque", "leaves_spruce_opaque", "leaves_opaque", "leaves_jungle_opaque"}};
027    @SideOnly(Side.CLIENT)
028    private int field_94394_cP;
029    private Icon[][] field_94395_cQ = new Icon[2][];
030    int[] adjacentTreeBlocks;
031
032    protected BlockLeaves(int par1)
033    {
034        super(par1, Material.leaves, false);
035        this.setTickRandomly(true);
036        this.setCreativeTab(CreativeTabs.tabDecorations);
037    }
038
039    @SideOnly(Side.CLIENT)
040    public int getBlockColor()
041    {
042        double d0 = 0.5D;
043        double d1 = 1.0D;
044        return ColorizerFoliage.getFoliageColor(d0, d1);
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 l = par1IBlockAccess.getBlockMetadata(par2, par3, par4);
066
067        if ((l & 3) == 1)
068        {
069            return ColorizerFoliage.getFoliageColorPine();
070        }
071        else if ((l & 3) == 2)
072        {
073            return ColorizerFoliage.getFoliageColorBirch();
074        }
075        else
076        {
077            int i1 = 0;
078            int j1 = 0;
079            int k1 = 0;
080
081            for (int l1 = -1; l1 <= 1; ++l1)
082            {
083                for (int i2 = -1; i2 <= 1; ++i2)
084                {
085                    int j2 = par1IBlockAccess.getBiomeGenForCoords(par2 + i2, par4 + l1).getBiomeFoliageColor();
086                    i1 += (j2 & 16711680) >> 16;
087                    j1 += (j2 & 65280) >> 8;
088                    k1 += j2 & 255;
089                }
090            }
091
092            return (i1 / 9 & 255) << 16 | (j1 / 9 & 255) << 8 | k1 / 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 b0 = 1;
102        int j1 = b0 + 1;
103
104        if (par1World.checkChunksExist(par2 - j1, par3 - j1, par4 - j1, par2 + j1, par3 + j1, par4 + j1))
105        {
106            for (int k1 = -b0; k1 <= b0; ++k1)
107            {
108                for (int l1 = -b0; l1 <= b0; ++l1)
109                {
110                    for (int i2 = -b0; i2 <= b0; ++i2)
111                    {
112                        int j2 = par1World.getBlockId(par2 + k1, par3 + l1, par4 + i2);
113
114                        if (Block.blocksList[j2] != null)
115                        {
116                            Block.blocksList[j2].beginLeavesDecay(par1World, par2 + k1, par3 + l1, par4 + i2);
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 l = par1World.getBlockMetadata(par2, par3, par4);
132
133            if ((l & 8) != 0 && (l & 4) == 0)
134            {
135                byte b0 = 4;
136                int i1 = b0 + 1;
137                byte b1 = 32;
138                int j1 = b1 * b1;
139                int k1 = b1 / 2;
140
141                if (this.adjacentTreeBlocks == null)
142                {
143                    this.adjacentTreeBlocks = new int[b1 * b1 * b1];
144                }
145
146                int l1;
147
148                if (par1World.checkChunksExist(par2 - i1, par3 - i1, par4 - i1, par2 + i1, par3 + i1, par4 + i1))
149                {
150                    int i2;
151                    int j2;
152                    int k2;
153
154                    for (l1 = -b0; l1 <= b0; ++l1)
155                    {
156                        for (i2 = -b0; i2 <= b0; ++i2)
157                        {
158                            for (j2 = -b0; j2 <= b0; ++j2)
159                            {
160                                k2 = par1World.getBlockId(par2 + l1, par3 + i2, par4 + j2);
161
162                                Block block = Block.blocksList[k2];
163
164                                if (block != null && block.canSustainLeaves(par1World, par2 + l1, par3 + i2, par4 + j2))
165                                {
166                                    this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = 0;
167                                }
168                                else if (block != null && block.isLeaves(par1World, par2 + l1, par3 + i2, par4 + j2))
169                                {
170                                    this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -2;
171                                }
172                                else
173                                {
174                                    this.adjacentTreeBlocks[(l1 + k1) * j1 + (i2 + k1) * b1 + j2 + k1] = -1;
175                                }
176                            }
177                        }
178                    }
179
180                    for (l1 = 1; l1 <= 4; ++l1)
181                    {
182                        for (i2 = -b0; i2 <= b0; ++i2)
183                        {
184                            for (j2 = -b0; j2 <= b0; ++j2)
185                            {
186                                for (k2 = -b0; k2 <= b0; ++k2)
187                                {
188                                    if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1] == l1 - 1)
189                                    {
190                                        if (this.adjacentTreeBlocks[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2)
191                                        {
192                                            this.adjacentTreeBlocks[(i2 + k1 - 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;
193                                        }
194
195                                        if (this.adjacentTreeBlocks[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] == -2)
196                                        {
197                                            this.adjacentTreeBlocks[(i2 + k1 + 1) * j1 + (j2 + k1) * b1 + k2 + k1] = l1;
198                                        }
199
200                                        if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] == -2)
201                                        {
202                                            this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 - 1) * b1 + k2 + k1] = l1;
203                                        }
204
205                                        if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] == -2)
206                                        {
207                                            this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1 + 1) * b1 + k2 + k1] = l1;
208                                        }
209
210                                        if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] == -2)
211                                        {
212                                            this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + (k2 + k1 - 1)] = l1;
213                                        }
214
215                                        if (this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] == -2)
216                                        {
217                                            this.adjacentTreeBlocks[(i2 + k1) * j1 + (j2 + k1) * b1 + k2 + k1 + 1] = l1;
218                                        }
219                                    }
220                                }
221                            }
222                        }
223                    }
224                }
225
226                l1 = this.adjacentTreeBlocks[k1 * j1 + k1 * b1 + k1];
227
228                if (l1 >= 0)
229                {
230                    par1World.setBlockMetadataWithNotify(par2, par3, par4, l & -9, 4);
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 d0 = (double)((float)par2 + par5Random.nextFloat());
250            double d1 = (double)par3 - 0.05D;
251            double d2 = (double)((float)par4 + par5Random.nextFloat());
252            par1World.spawnParticle("dripWater", d0, d1, d2, 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.func_94571_i(par2, par3, par4);
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            int j1 = 20;
286
287            if ((par5 & 3) == 3)
288            {
289                j1 = 40;
290            }
291
292            if (par7 > 0)
293            {
294                j1 -= 2 << par7;
295
296                if (j1 < 10)
297                {
298                    j1 = 10;
299                }
300            }
301
302            if (par1World.rand.nextInt(j1) == 0)
303            {
304                int k1 = this.idDropped(par5, par1World.rand, par7);
305                this.dropBlockAsItem_do(par1World, par2, par3, par4, new ItemStack(k1, 1, this.damageDropped(par5)));
306            }
307
308            j1 = 200;
309
310            if (par7 > 0)
311            {
312                j1 -= 10 << par7;
313
314                if (j1 < 40)
315                {
316                    j1 = 40;
317                }
318            }
319
320            if ((par5 & 3) == 0 && par1World.rand.nextInt(j1) == 0)
321            {
322                this.dropBlockAsItem_do(par1World, par2, par3, par4, new ItemStack(Item.appleRed, 1, 0));
323            }
324        }
325    }
326
327    /**
328     * Called when the player destroys a block with an item that can harvest it. (i, j, k) are the coordinates of the
329     * block and l is the block's subtype/damage.
330     */
331    public void harvestBlock(World par1World, EntityPlayer par2EntityPlayer, int par3, int par4, int par5, int par6)
332    {
333        super.harvestBlock(par1World, par2EntityPlayer, par3, par4, par5, par6);
334    }
335
336    /**
337     * Determines the damage on the item the block drops. Used in cloth and wood.
338     */
339    public int damageDropped(int par1)
340    {
341        return par1 & 3;
342    }
343
344    /**
345     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
346     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
347     */
348    public boolean isOpaqueCube()
349    {
350        return !this.graphicsLevel;
351    }
352
353    @SideOnly(Side.CLIENT)
354
355    /**
356     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
357     */
358    public Icon getBlockTextureFromSideAndMetadata(int par1, int par2)
359    {
360        return (par2 & 3) == 1 ? this.field_94395_cQ[this.field_94394_cP][1] : ((par2 & 3) == 3 ? this.field_94395_cQ[this.field_94394_cP][3] : this.field_94395_cQ[this.field_94394_cP][0]);
361    }
362
363    @SideOnly(Side.CLIENT)
364
365    /**
366     * Pass true to draw this block using fancy graphics, or false for fast graphics.
367     */
368    public void setGraphicsLevel(boolean par1)
369    {
370        this.graphicsLevel = par1;
371        this.field_94394_cP = par1 ? 0 : 1;
372    }
373
374    @SideOnly(Side.CLIENT)
375
376    /**
377     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
378     */
379    public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
380    {
381        par3List.add(new ItemStack(par1, 1, 0));
382        par3List.add(new ItemStack(par1, 1, 1));
383        par3List.add(new ItemStack(par1, 1, 2));
384        par3List.add(new ItemStack(par1, 1, 3));
385    }
386
387    /**
388     * Returns an item stack containing a single instance of the current block type. 'i' is the block's subtype/damage
389     * and is ignored for blocks which do not support subtypes. Blocks which cannot be harvested should return null.
390     */
391    protected ItemStack createStackedBlock(int par1)
392    {
393        return new ItemStack(this.blockID, 1, par1 & 3);
394    }
395
396    @SideOnly(Side.CLIENT)
397    public void func_94332_a(IconRegister par1IconRegister)
398    {
399        for (int i = 0; i < field_94396_b.length; ++i)
400        {
401            this.field_94395_cQ[i] = new Icon[field_94396_b[i].length];
402
403            for (int j = 0; j < field_94396_b[i].length; ++j)
404            {
405                this.field_94395_cQ[i][j] = par1IconRegister.func_94245_a(field_94396_b[i][j]);
406            }
407        }
408    }
409
410    @Override
411    public boolean isShearable(ItemStack item, World world, int x, int y, int z)
412    {
413        return true;
414    }
415
416    @Override
417    public ArrayList<ItemStack> onSheared(ItemStack item, World world, int x, int y, int z, int fortune)
418    {
419        ArrayList<ItemStack> ret = new ArrayList<ItemStack>();
420        ret.add(new ItemStack(this, 1, world.getBlockMetadata(x, y, z) & 3));
421        return ret;
422    }
423
424    @Override
425    public void beginLeavesDecay(World world, int x, int y, int z)
426    {
427        world.setBlockMetadataWithNotify(x, y, z, world.getBlockMetadata(x, y, z) | 8, 4);
428    }
429
430    @Override
431    public boolean isLeaves(World world, int x, int y, int z)
432    {
433        return true;
434    }
435}