    001package net.minecraft.world.gen.feature;
    002
    003import java.util.Random;
    004import net.minecraft.block.Block;
    005import net.minecraft.block.BlockSapling;
    006import net.minecraft.world.World;
    007import net.minecraftforge.common.ForgeDirection;
    008
    009public class WorldGenForest extends WorldGenerator
    010{
    011    public WorldGenForest(boolean par1)
    012    {
    013        super(par1);
    014    }
    015
    016    public boolean generate(World par1World, Random par2Random, int par3, int par4, int par5)
    017    {
    018        int l = par2Random.nextInt(3) + 5;
    019        boolean flag = true;
    020
    021        if (par4 >= 1 && par4 + l + 1 <= 256)
    022        {
    023            int i1;
    024            int j1;
    025            int k1;
    026            int l1;
    027
    028            for (i1 = par4; i1 <= par4 + 1 + l; ++i1)
    029            {
    030                byte b0 = 1;
    031
    032                if (i1 == par4)
    033                {
    034                    b0 = 0;
    035                }
    036
    037                if (i1 >= par4 + 1 + l - 2)
    038                {
    039                    b0 = 2;
    040                }
    041
    042                for (j1 = par3 - b0; j1 <= par3 + b0 && flag; ++j1)
    043                {
    044                    for (k1 = par5 - b0; k1 <= par5 + b0 && flag; ++k1)
    045                    {
    046                        if (i1 >= 0 && i1 < 256)
    047                        {
    048                            l1 = par1World.getBlockId(j1, i1, k1);
    049
    050                            Block block = Block.blocksList[l1];
    051
    052                            if (l1 != 0 && (block != null && !block.isLeaves(par1World, j1,  i1, k1)))
    053                            {
    054                                flag = false;
    055                            }
    056                        }
    057                        else
    058                        {
    059                            flag = false;
    060                        }
    061                    }
    062                }
    063            }
    064
    065            if (!flag)
    066            {
    067                return false;
    068            }
    069            else
    070            {
    071                i1 = par1World.getBlockId(par3, par4 - 1, par5);
    072                Block soil = Block.blocksList[i1];
    073                boolean isValidSoil = soil != null && soil.canSustainPlant(par1World, par3, par4 - 1, par5, ForgeDirection.UP, (BlockSapling)Block.sapling);
    074
    075                if (isValidSoil && par4 < 256 - l - 1)
    076                {
    077                    soil.onPlantGrow(par1World, par3, par4 - 1, par5, par3, par4, par5);
    078                    int i2;
    079
    080                    for (i2 = par4 - 3 + l; i2 <= par4 + l; ++i2)
    081                    {
    082                        j1 = i2 - (par4 + l);
    083                        k1 = 1 - j1 / 2;
    084
    085                        for (l1 = par3 - k1; l1 <= par3 + k1; ++l1)
    086                        {
    087                            int j2 = l1 - par3;
    088
    089                            for (int k2 = par5 - k1; k2 <= par5 + k1; ++k2)
    090                            {
    091                                int l2 = k2 - par5;
    092
    093                                if (Math.abs(j2) != k1 || Math.abs(l2) != k1 || par2Random.nextInt(2) != 0 && j1 != 0)
    094                                {
    095                                    int i3 = par1World.getBlockId(l1, i2, k2);
    096                                    Block block = Block.blocksList[i3];
    097
    098                                    if (block == null || block.canBeReplacedByLeaves(par1World, l1, i2, k2))
    099                                    {
    100                                        this.setBlockAndMetadata(par1World, l1, i2, k2, Block.leaves.blockID, 2);
    101                                    }
    102                                }
    103                            }
    104                        }
    105                    }
    106
    107                    for (i2 = 0; i2 < l; ++i2)
    108                    {
    109                        j1 = par1World.getBlockId(par3, par4 + i2, par5);
    110
    111                        Block block = Block.blocksList[j1];
    112
    113                        if (j1 == 0 || block == null || block.isLeaves(par1World, par3, par4 + i2, par5))
    114                        {
    115                            this.setBlockAndMetadata(par1World, par3, par4 + i2, par5, Block.wood.blockID, 2);
    116                        }
    117                    }
    118
    119                    return true;
    120                }
    121                else
    122                {
    123                    return false;
    124                }
    125            }
    126        }
    127        else
    128        {
    129            return false;
    130        }
    131    }
    132}