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.creativetab.CreativeTabs;
009import net.minecraft.util.Icon;
010import net.minecraft.world.ColorizerGrass;
011import net.minecraft.world.IBlockAccess;
012import net.minecraft.world.World;
013
014public class BlockGrass extends Block
015{
016    @SideOnly(Side.CLIENT)
017    private Icon iconGrassTop;
018    @SideOnly(Side.CLIENT)
019    private Icon iconSnowSide;
020    @SideOnly(Side.CLIENT)
021    private Icon iconGrassSideOverlay;
022
023    protected BlockGrass(int par1)
024    {
025        super(par1, Material.grass);
026        this.setTickRandomly(true);
027        this.setCreativeTab(CreativeTabs.tabBlock);
028    }
029
030    @SideOnly(Side.CLIENT)
031
032    /**
033     * From the specified side and block metadata retrieves the blocks texture. Args: side, metadata
034     */
035    public Icon getBlockTextureFromSideAndMetadata(int par1, int par2)
036    {
037        return par1 == 1 ? this.iconGrassTop : (par1 == 0 ? Block.dirt.getBlockTextureFromSide(par1) : this.blockIcon);
038    }
039
040    /**
041     * Ticks the block if it's been scheduled
042     */
043    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
044    {
045        if (!par1World.isRemote)
046        {
047            if (par1World.getBlockLightValue(par2, par3 + 1, par4) < 4 && par1World.getBlockLightOpacity(par2, par3 + 1, par4) > 2)
048            {
049                par1World.setBlock(par2, par3, par4, Block.dirt.blockID);
050            }
051            else if (par1World.getBlockLightValue(par2, par3 + 1, par4) >= 9)
052            {
053                for (int l = 0; l < 4; ++l)
054                {
055                    int i1 = par2 + par5Random.nextInt(3) - 1;
056                    int j1 = par3 + par5Random.nextInt(5) - 3;
057                    int k1 = par4 + par5Random.nextInt(3) - 1;
058                    int l1 = par1World.getBlockId(i1, j1 + 1, k1);
059
060                    if (par1World.getBlockId(i1, j1, k1) == Block.dirt.blockID && par1World.getBlockLightValue(i1, j1 + 1, k1) >= 4 && par1World.getBlockLightOpacity(i1, j1 + 1, k1) <= 2)
061                    {
062                        par1World.setBlock(i1, j1, k1, Block.grass.blockID);
063                    }
064                }
065            }
066        }
067    }
068
069    /**
070     * Returns the ID of the items to drop on destruction.
071     */
072    public int idDropped(int par1, Random par2Random, int par3)
073    {
074        return Block.dirt.idDropped(0, par2Random, par3);
075    }
076
077    @SideOnly(Side.CLIENT)
078
079    /**
080     * Retrieves the block texture to use based on the display side. Args: iBlockAccess, x, y, z, side
081     */
082    public Icon getBlockTexture(IBlockAccess par1IBlockAccess, int par2, int par3, int par4, int par5)
083    {
084        if (par5 == 1)
085        {
086            return this.iconGrassTop;
087        }
088        else if (par5 == 0)
089        {
090            return Block.dirt.getBlockTextureFromSide(par5);
091        }
092        else
093        {
094            Material material = par1IBlockAccess.getBlockMaterial(par2, par3 + 1, par4);
095            return material != Material.snow && material != Material.craftedSnow ? this.blockIcon : this.iconSnowSide;
096        }
097    }
098
099    @SideOnly(Side.CLIENT)
100
101    /**
102     * When this method is called, your block should register all the icons it needs with the given IconRegister. This
103     * is the only chance you get to register icons.
104     */
105    public void registerIcons(IconRegister par1IconRegister)
106    {
107        this.blockIcon = par1IconRegister.registerIcon("grass_side");
108        this.iconGrassTop = par1IconRegister.registerIcon("grass_top");
109        this.iconSnowSide = par1IconRegister.registerIcon("snow_side");
110        this.iconGrassSideOverlay = par1IconRegister.registerIcon("grass_side_overlay");
111    }
112
113    @SideOnly(Side.CLIENT)
114    public int getBlockColor()
115    {
116        double d0 = 0.5D;
117        double d1 = 1.0D;
118        return ColorizerGrass.getGrassColor(d0, d1);
119    }
120
121    @SideOnly(Side.CLIENT)
122
123    /**
124     * Returns the color this block should be rendered. Used by leaves.
125     */
126    public int getRenderColor(int par1)
127    {
128        return this.getBlockColor();
129    }
130
131    @SideOnly(Side.CLIENT)
132
133    /**
134     * Returns a integer with hex for 0xrrggbb with this color multiplied against the blocks color. Note only called
135     * when first determining what to render.
136     */
137    public int colorMultiplier(IBlockAccess par1IBlockAccess, int par2, int par3, int par4)
138    {
139        int l = 0;
140        int i1 = 0;
141        int j1 = 0;
142
143        for (int k1 = -1; k1 <= 1; ++k1)
144        {
145            for (int l1 = -1; l1 <= 1; ++l1)
146            {
147                int i2 = par1IBlockAccess.getBiomeGenForCoords(par2 + l1, par4 + k1).getBiomeGrassColor();
148                l += (i2 & 16711680) >> 16;
149                i1 += (i2 & 65280) >> 8;
150                j1 += i2 & 255;
151            }
152        }
153
154        return (l / 9 & 255) << 16 | (i1 / 9 & 255) << 8 | j1 / 9 & 255;
155    }
156
157    @SideOnly(Side.CLIENT)
158    public static Icon getIconSideOverlay()
159    {
160        return Block.grass.iconGrassSideOverlay;
161    }
162}