001package net.minecraft.block;
002
003import cpw.mods.fml.relauncher.Side;
004import cpw.mods.fml.relauncher.SideOnly;
005import java.util.Random;
006import net.minecraft.client.renderer.texture.IconRegister;
007import net.minecraft.world.World;
008import net.minecraft.world.gen.feature.WorldGenBigMushroom;
009
010import net.minecraftforge.common.ForgeDirection;
011
012public class BlockMushroom extends BlockFlower
013{
014    private final String field_94374_a;
015
016    protected BlockMushroom(int par1, String par2Str)
017    {
018        super(par1);
019        this.field_94374_a = par2Str;
020        float f = 0.2F;
021        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);
022        this.setTickRandomly(true);
023    }
024
025    /**
026     * Ticks the block if it's been scheduled
027     */
028    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
029    {
030        if (par5Random.nextInt(25) == 0)
031        {
032            byte b0 = 4;
033            int l = 5;
034            int i1;
035            int j1;
036            int k1;
037
038            for (i1 = par2 - b0; i1 <= par2 + b0; ++i1)
039            {
040                for (j1 = par4 - b0; j1 <= par4 + b0; ++j1)
041                {
042                    for (k1 = par3 - 1; k1 <= par3 + 1; ++k1)
043                    {
044                        if (par1World.getBlockId(i1, k1, j1) == this.blockID)
045                        {
046                            --l;
047
048                            if (l <= 0)
049                            {
050                                return;
051                            }
052                        }
053                    }
054                }
055            }
056
057            i1 = par2 + par5Random.nextInt(3) - 1;
058            j1 = par3 + par5Random.nextInt(2) - par5Random.nextInt(2);
059            k1 = par4 + par5Random.nextInt(3) - 1;
060
061            for (int l1 = 0; l1 < 4; ++l1)
062            {
063                if (par1World.isAirBlock(i1, j1, k1) && this.canBlockStay(par1World, i1, j1, k1))
064                {
065                    par2 = i1;
066                    par3 = j1;
067                    par4 = k1;
068                }
069
070                i1 = par2 + par5Random.nextInt(3) - 1;
071                j1 = par3 + par5Random.nextInt(2) - par5Random.nextInt(2);
072                k1 = par4 + par5Random.nextInt(3) - 1;
073            }
074
075            if (par1World.isAirBlock(i1, j1, k1) && this.canBlockStay(par1World, i1, j1, k1))
076            {
077                par1World.setBlock(i1, j1, k1, this.blockID);
078            }
079        }
080    }
081
082    /**
083     * Checks to see if its valid to put this block at the specified coordinates. Args: world, x, y, z
084     */
085    public boolean canPlaceBlockAt(World par1World, int par2, int par3, int par4)
086    {
087        return super.canPlaceBlockAt(par1World, par2, par3, par4) && this.canBlockStay(par1World, par2, par3, par4);
088    }
089
090    /**
091     * Gets passed in the blockID of the block below and supposed to return true if its allowed to grow on the type of
092     * blockID passed in. Args: blockID
093     */
094    protected boolean canThisPlantGrowOnThisBlockID(int par1)
095    {
096        return Block.opaqueCubeLookup[par1];
097    }
098
099    /**
100     * Can this block stay at this position.  Similar to canPlaceBlockAt except gets checked often with plants.
101     */
102    public boolean canBlockStay(World par1World, int par2, int par3, int par4)
103    {
104        if (par3 >= 0 && par3 < 256)
105        {
106            int l = par1World.getBlockId(par2, par3 - 1, par4);
107            Block soil = Block.blocksList[l];
108            return (l == Block.mycelium.blockID || par1World.getFullBlockLightValue(par2, par3, par4) < 13) &&
109                   (soil != null && soil.canSustainPlant(par1World, par2, par3 - 1, par4, ForgeDirection.UP, this));
110        }
111        else
112        {
113            return false;
114        }
115    }
116
117    /**
118     * Fertilize the mushroom.
119     */
120    public boolean fertilizeMushroom(World par1World, int par2, int par3, int par4, Random par5Random)
121    {
122        int l = par1World.getBlockMetadata(par2, par3, par4);
123        par1World.setBlockToAir(par2, par3, par4);
124        WorldGenBigMushroom worldgenbigmushroom = null;
125
126        if (this.blockID == Block.mushroomBrown.blockID)
127        {
128            worldgenbigmushroom = new WorldGenBigMushroom(0);
129        }
130        else if (this.blockID == Block.mushroomRed.blockID)
131        {
132            worldgenbigmushroom = new WorldGenBigMushroom(1);
133        }
134
135        if (worldgenbigmushroom != null && worldgenbigmushroom.generate(par1World, par5Random, par2, par3, par4))
136        {
137            return true;
138        }
139        else
140        {
141            par1World.setBlock(par2, par3, par4, this.blockID, l, 3);
142            return false;
143        }
144    }
145
146    @SideOnly(Side.CLIENT)
147
148    /**
149     * When this method is called, your block should register all the icons it needs with the given IconRegister. This
150     * is the only chance you get to register icons.
151     */
152    public void registerIcons(IconRegister par1IconRegister)
153    {
154        this.blockIcon = par1IconRegister.registerIcon(this.field_94374_a);
155    }
156}