-
Notifications
You must be signed in to change notification settings - Fork 81
/
OverworldTreeGenerator.java
220 lines (179 loc) · 7.13 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package com.progwml6.natura.world.worldgen.trees.overworld;
import java.util.Random;
import com.progwml6.natura.common.config.Config;
import com.progwml6.natura.overworld.NaturaOverworld;
import com.progwml6.natura.world.worldgen.trees.BaseTreeGenerator;
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.WorldType;
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 final boolean isSapling;
public OverworldTreeGenerator(int treeHeight, int treeRange, IBlockState log, IBlockState leaves, boolean seekHeight, boolean isSapling)
{
this.minTreeHeight = treeHeight;
this.treeHeightRange = treeRange;
this.log = log;
this.leaves = leaves;
this.seekHeight = seekHeight;
this.isSapling = isSapling;
}
public OverworldTreeGenerator(int treeHeight, int treeRange, IBlockState log, IBlockState leaves)
{
this(treeHeight, treeRange, log, leaves, true, false);
}
@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)
{
if (!this.checkClear(world, pos.getX(), yPos, pos.getY(), height))
{
return;
}
soil.onPlantGrow(state, world, pos.down(), pos);
this.placeCanopy(world, random, pos, height);
this.placeTrunk(world, pos, height);
}
}
}
boolean checkClear(World world, int x, int y, int z, int treeHeight)
{
for (int yPos = 0; yPos < treeHeight + 1; yPos++)
{
int range = 1;
if (yPos == 0)
{
range = 0;
}
else if (yPos >= treeHeight - 1)
{
range = 2;
}
for (int xPos = range; xPos <= range; xPos++)
{
for (int zPos = range; zPos <= range; zPos++)
{
BlockPos blockpos = new BlockPos(x + xPos, y + yPos, z + zPos);
IBlockState state = world.getBlockState(blockpos);
if (state.getBlock() != null && state.getBlock() != NaturaOverworld.overworldSapling || !state.getBlock().isLeaves(state, world, blockpos))
{
return true;
}
}
}
}
return true;
}
BlockPos findGround(World world, BlockPos pos)
{
int returnHeight = 0;
int height = pos.getY();
if (world.getWorldType() == WorldType.FLAT && this.isSapling)
{
do
{
BlockPos position = new BlockPos(pos.getX(), height, pos.getZ());
Block block = world.getBlockState(position).getBlock();
if ((block == Blocks.DIRT || block == Blocks.GRASS) && !world.getBlockState(position.up()).isFullBlock())
{
returnHeight = height + 1;
break;
}
height--;
}
while (height > Config.flatSeaLevel);
return new BlockPos(pos.getX(), returnHeight, pos.getZ());
}
else
{
do
{
BlockPos position = new BlockPos(pos.getX(), height, pos.getZ());
Block block = world.getBlockState(position).getBlock();
if ((block == Blocks.DIRT || block == Blocks.GRASS) && !world.getBlockState(position.up()).isFullBlock())
{
returnHeight = height + 1;
break;
}
height--;
}
while (height > Config.seaLevel);
return new BlockPos(pos.getX(), returnHeight, pos.getZ());
}
}
protected 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().canBeReplacedByLeaves(state, world, blockpos))
{
world.setBlockState(blockpos, this.leaves, 2);
}
}
}
}
}
}
protected boolean canGrowInto(Block blockType)
{
Material material = blockType.getDefaultState().getMaterial();
return material == Material.AIR || material == Material.LEAVES || blockType == Blocks.GRASS || blockType == Blocks.DIRT || blockType == Blocks.LOG || blockType == Blocks.LOG2 || blockType == Blocks.SAPLING || blockType == Blocks.VINE;
}
protected void placeTrunk(World world, BlockPos pos, int height)
{
for (int localHeight = 0; localHeight < height; ++localHeight)
{
BlockPos blockpos = new BlockPos(pos.getX(), pos.getY() + localHeight, pos.getZ());
IBlockState state = world.getBlockState(blockpos);
Block block = state.getBlock();
if (block.isAir(state, world, blockpos) || block == null || block.isLeaves(state, world, blockpos))
{
world.setBlockState(blockpos, this.log, 2);
}
}
}
}