/
OverworldTreeGenerator.java
151 lines (126 loc) · 4.86 KB
/
OverworldTreeGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.progwml6.natura.world.worldgen;
import java.util.Random;
import com.progwml6.natura.overworld.NaturaOverworld;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkGenerator;
import net.minecraft.world.chunk.IChunkProvider;
public class OverworldTreeGenerator extends BaseTreeGenerator
{
public final int minTreeHeight;
public final int treeHeightRange;
public final IBlockState log;
public final IBlockState leaves;
public final boolean seekHeight;
public OverworldTreeGenerator(int treeHeight, int treeRange, IBlockState log, IBlockState leaves, boolean seekHeight)
{
this.minTreeHeight = treeHeight;
this.treeHeightRange = treeRange;
this.log = log;
this.leaves = leaves;
this.seekHeight = seekHeight;
}
public OverworldTreeGenerator(int treeHeight, int treeRange, IBlockState log, IBlockState leaves)
{
this(treeHeight, treeRange, log, leaves, true);
}
@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider)
{
}
@Override
public void generateTree(Random random, World world, BlockPos pos)
{
int height = random.nextInt(this.treeHeightRange) + this.minTreeHeight;
if (this.seekHeight)
{
pos = this.findGround(world, pos);
if (pos.getY() < 0)
{
return;
}
}
int yPos = pos.getY();
if (yPos >= 1 && yPos + height + 1 <= 256)
{
IBlockState state = world.getBlockState(pos.down());
Block soil = state.getBlock();
boolean isSoil = (soil != null && soil.canSustainPlant(state, world, pos.down(), EnumFacing.UP, NaturaOverworld.overworldSapling));
if (isSoil)
{
soil.onPlantGrow(state, world, pos.down(), pos);
this.placeCanopy(world, random, pos, height);
this.placeTrunk(world, pos, height);
}
}
}
@SuppressWarnings("deprecation")
BlockPos findGround(World world, BlockPos pos)
{
do
{
IBlockState state = world.getBlockState(pos);
Block heightID = state.getBlock();
if ((heightID == Blocks.DIRT || heightID == Blocks.GRASS) && !world.getBlockState(pos.up()).getBlock().isOpaqueCube(state))
{
return pos.up();
}
pos = pos.down();
}
while (pos.getY() > 0);
return pos;
}
void placeCanopy(World world, Random random, BlockPos pos, int height)
{
for (int y = pos.getY() - 3 + height; y <= pos.getY() + height; ++y)
{
int subract = y - (pos.getY() + height);
int subract2 = 1 - subract / 2;
for (int x = pos.getX() - subract2; x <= pos.getX() + subract2; ++x)
{
int mathX = x - pos.getX();
for (int z = pos.getZ() - subract2; z <= pos.getZ() + subract2; ++z)
{
int mathZ = z - pos.getZ();
if (Math.abs(mathX) != subract2 || Math.abs(mathZ) != subract2 || random.nextInt(2) != 0 && subract != 0)
{
BlockPos blockpos = new BlockPos(x, y, z);
IBlockState state = world.getBlockState(blockpos);
if (state.getBlock().isAir(state, world, blockpos) || state.getBlock().isLeaves(state, world, blockpos) || state.getMaterial() == Material.VINE)
{
this.setBlockAndMetadata(world, blockpos, this.leaves);
}
}
}
}
}
}
protected void placeTrunk(World world, BlockPos pos, int height)
{
while (height >= 0)
{
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block.isAir(state, world, pos) || block.isReplaceable(world, pos) || block.isLeaves(state, world, pos))
{
this.setBlockAndMetadata(world, pos, this.log);
}
pos = pos.up();
height--;
}
}
protected void setBlockAndMetadata(World world, BlockPos pos, IBlockState stateNew)
{
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block.isAir(state, world, pos) || block.canPlaceBlockAt(world, pos) || world.getBlockState(pos) == this.leaves)
{
world.setBlockState(pos, stateNew, 2);
}
}
}