    001package net.minecraft.world.gen.feature;
    002
    003import java.util.Random;
    004import net.minecraft.block.Block;
    005import net.minecraft.block.BlockSapling;
    006import net.minecraft.util.Direction;
    007import net.minecraft.world.World;
    008import net.minecraftforge.common.ForgeDirection;
    009
    010public class WorldGenTrees extends WorldGenerator
    011{
    012    /** The minimum height of a generated tree. */
    013    private final int minTreeHeight;
    014
    015    /** True if this tree should grow Vines. */
    016    private final boolean vinesGrow;
    017
    018    /** The metadata value of the wood to use in tree generation. */
    019    private final int metaWood;
    020
    021    /** The metadata value of the leaves to use in tree generation. */
    022    private final int metaLeaves;
    023
    024    public WorldGenTrees(boolean par1)
    025    {
    026        this(par1, 4, 0, 0, false);
    027    }
    028
    029    public WorldGenTrees(boolean par1, int par2, int par3, int par4, boolean par5)
    030    {
    031        super(par1);
    032        this.minTreeHeight = par2;
    033        this.metaWood = par3;
    034        this.metaLeaves = par4;
    035        this.vinesGrow = par5;
    036    }
    037
    038    public boolean generate(World par1World, Random par2Random, int par3, int par4, int par5)
    039    {
    040        int var6 = par2Random.nextInt(3) + this.minTreeHeight;
    041        boolean var7 = true;
    042
    043        if (par4 >= 1 && par4 + var6 + 1 <= 256)
    044        {
    045            int var8;
    046            byte var9;
    047            int var11;
    048            int var12;
    049
    050            for (var8 = par4; var8 <= par4 + 1 + var6; ++var8)
    051            {
    052                var9 = 1;
    053
    054                if (var8 == par4)
    055                {
    056                    var9 = 0;
    057                }
    058
    059                if (var8 >= par4 + 1 + var6 - 2)
    060                {
    061                    var9 = 2;
    062                }
    063
    064                for (int var10 = par3 - var9; var10 <= par3 + var9 && var7; ++var10)
    065                {
    066                    for (var11 = par5 - var9; var11 <= par5 + var9 && var7; ++var11)
    067                    {
    068                        if (var8 >= 0 && var8 < 256)
    069                        {
    070                            var12 = par1World.getBlockId(var10, var8, var11);
    071
    072                            Block block = Block.blocksList[var12];
    073
    074                            if (block != null &&
    075                               !block.isLeaves(par1World, var10, var8, var11) &&
    076                               !block.canSustainPlant(par1World, var10, var8, var11, ForgeDirection.UP, (BlockSapling)Block.sapling) &&
    077                               !block.isWood(par1World, var10, var8, var11))
    078                            {
    079                                var7 = false;
    080                            }
    081                        }
    082                        else
    083                        {
    084                            var7 = false;
    085                        }
    086                    }
    087                }
    088            }
    089
    090            if (!var7)
    091            {
    092                return false;
    093            }
    094            else
    095            {
    096                var8 = par1World.getBlockId(par3, par4 - 1, par5);
    097                Block soil = Block.blocksList[var8];
    098                boolean isSoil = (soil != null && soil.canSustainPlant(par1World, par3, par4 - 1, par5, ForgeDirection.UP, (BlockSapling)Block.sapling));
    099
    100                if (isSoil && par4 < 256 - var6 - 1)
    101                {
    102                    soil.onPlantGrow(par1World, par3, par4 - 1, par5, par3, par4, par5);
    103                    var9 = 3;
    104                    byte var18 = 0;
    105                    int var13;
    106                    int var14;
    107                    int var15;
    108
    109                    for (var11 = par4 - var9 + var6; var11 <= par4 + var6; ++var11)
    110                    {
    111                        var12 = var11 - (par4 + var6);
    112                        var13 = var18 + 1 - var12 / 2;
    113
    114                        for (var14 = par3 - var13; var14 <= par3 + var13; ++var14)
    115                        {
    116                            var15 = var14 - par3;
    117
    118                            for (int var16 = par5 - var13; var16 <= par5 + var13; ++var16)
    119                            {
    120                                int var17 = var16 - par5;
    121
    122                                Block block = Block.blocksList[par1World.getBlockId(var14, var11, var16)];
    123
    124                                if ((Math.abs(var15) != var13 || Math.abs(var17) != var13 || par2Random.nextInt(2) != 0 && var12 != 0) &&
    125                                    (block == null || block.canBeReplacedByLeaves(par1World, var14, var11, var16)))
    126                                {
    127                                    this.setBlockAndMetadata(par1World, var14, var11, var16, Block.leaves.blockID, this.metaLeaves);
    128                                }
    129                            }
    130                        }
    131                    }
    132
    133                    for (var11 = 0; var11 < var6; ++var11)
    134                    {
    135                        var12 = par1World.getBlockId(par3, par4 + var11, par5);
    136
    137                        Block block = Block.blocksList[var12];
    138
    139                        if (var12 == 0 || block == null || block.isLeaves(par1World, par3, par4 + var11, par5))
    140                        {
    141                            this.setBlockAndMetadata(par1World, par3, par4 + var11, par5, Block.wood.blockID, this.metaWood);
    142
    143                            if (this.vinesGrow && var11 > 0)
    144                            {
    145                                if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3 - 1, par4 + var11, par5))
    146                                {
    147                                    this.setBlockAndMetadata(par1World, par3 - 1, par4 + var11, par5, Block.vine.blockID, 8);
    148                                }
    149
    150                                if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3 + 1, par4 + var11, par5))
    151                                {
    152                                    this.setBlockAndMetadata(par1World, par3 + 1, par4 + var11, par5, Block.vine.blockID, 2);
    153                                }
    154
    155                                if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3, par4 + var11, par5 - 1))
    156                                {
    157                                    this.setBlockAndMetadata(par1World, par3, par4 + var11, par5 - 1, Block.vine.blockID, 1);
    158                                }
    159
    160                                if (par2Random.nextInt(3) > 0 && par1World.isAirBlock(par3, par4 + var11, par5 + 1))
    161                                {
    162                                    this.setBlockAndMetadata(par1World, par3, par4 + var11, par5 + 1, Block.vine.blockID, 4);
    163                                }
    164                            }
    165                        }
    166                    }
    167
    168                    if (this.vinesGrow)
    169                    {
    170                        for (var11 = par4 - 3 + var6; var11 <= par4 + var6; ++var11)
    171                        {
    172                            var12 = var11 - (par4 + var6);
    173                            var13 = 2 - var12 / 2;
    174
    175                            for (var14 = par3 - var13; var14 <= par3 + var13; ++var14)
    176                            {
    177                                for (var15 = par5 - var13; var15 <= par5 + var13; ++var15)
    178                                {
    179                                    Block block = Block.blocksList[par1World.getBlockId(var14, var11, var15)];
    180                                    if (block != null && block.isLeaves(par1World, var14, var11, var15))
    181                                    {
    182                                        if (par2Random.nextInt(4) == 0 && par1World.getBlockId(var14 - 1, var11, var15) == 0)
    183                                        {
    184                                            this.growVines(par1World, var14 - 1, var11, var15, 8);
    185                                        }
    186
    187                                        if (par2Random.nextInt(4) == 0 && par1World.getBlockId(var14 + 1, var11, var15) == 0)
    188                                        {
    189                                            this.growVines(par1World, var14 + 1, var11, var15, 2);
    190                                        }
    191
    192                                        if (par2Random.nextInt(4) == 0 && par1World.getBlockId(var14, var11, var15 - 1) == 0)
    193                                        {
    194                                            this.growVines(par1World, var14, var11, var15 - 1, 1);
    195                                        }
    196
    197                                        if (par2Random.nextInt(4) == 0 && par1World.getBlockId(var14, var11, var15 + 1) == 0)
    198                                        {
    199                                            this.growVines(par1World, var14, var11, var15 + 1, 4);
    200                                        }
    201                                    }
    202                                }
    203                            }
    204                        }
    205
    206                        if (par2Random.nextInt(5) == 0 && var6 > 5)
    207                        {
    208                            for (var11 = 0; var11 < 2; ++var11)
    209                            {
    210                                for (var12 = 0; var12 < 4; ++var12)
    211                                {
    212                                    if (par2Random.nextInt(4 - var11) == 0)
    213                                    {
    214                                        var13 = par2Random.nextInt(3);
    215                                        this.setBlockAndMetadata(par1World, par3 + Direction.offsetX[Direction.footInvisibleFaceRemap[var12]], par4 + var6 - 5 + var11, par5 + Direction.offsetZ[Direction.footInvisibleFaceRemap[var12]], Block.cocoaPlant.blockID, var13 << 2 | var12);
    216                                    }
    217                                }
    218                            }
    219                        }
    220                    }
    221
    222                    return true;
    223                }
    224                else
    225                {
    226                    return false;
    227                }
    228            }
    229        }
    230        else
    231        {
    232            return false;
    233        }
    234    }
    235
    236    /**
    237     * Grows vines downward from the given block for a given length. Args: World, x, starty, z, vine-length
    238     */
    239    private void growVines(World par1World, int par2, int par3, int par4, int par5)
    240    {
    241        this.setBlockAndMetadata(par1World, par2, par3, par4, Block.vine.blockID, par5);
    242        int var6 = 4;
    243
    244        while (true)
    245        {
    246            --par3;
    247
    248            if (par1World.getBlockId(par2, par3, par4) != 0 || var6 <= 0)
    249            {
    250                return;
    251            }
    252
    253            this.setBlockAndMetadata(par1World, par2, par3, par4, Block.vine.blockID, par5);
    254            --var6;
    255        }
    256    }
    257}