/
WillowTreeGenerator.java
254 lines (212 loc) · 9.65 KB
/
WillowTreeGenerator.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package com.progwml6.natura.world.worldgen.trees.overworld;
import java.util.Random;
import com.progwml6.natura.world.worldgen.trees.BaseTreeGenerator;
import net.minecraft.block.Block;
import net.minecraft.block.BlockVine;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
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 WillowTreeGenerator extends BaseTreeGenerator
{
public final int minTreeHeight;
public final int treeHeightRange;
public final IBlockState log;
public final IBlockState leaves;
public final boolean seekHeight;
public WillowTreeGenerator(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 WillowTreeGenerator(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 worldIn, BlockPos position)
{
int height;
if (this.seekHeight)
{
position = this.findGround(worldIn, position);
if (position.getY() < 0)
{
return;
}
}
for (height = random.nextInt(this.treeHeightRange) + this.minTreeHeight; worldIn.getBlockState(position.down()).getMaterial() == Material.WATER; position = position.down())
{
;
}
boolean flag = true;
if (position.getY() >= 1 && position.getY() + height + 1 <= 256)
{
for (int j = position.getY(); j <= position.getY() + 1 + height; ++j)
{
int k = 1;
if (j == position.getY())
{
k = 0;
}
if (j >= position.getY() + 1 + height - 2)
{
k = 3;
}
BlockPos.MutableBlockPos blockpos$mutableblockpos = new BlockPos.MutableBlockPos();
for (int l = position.getX() - k; l <= position.getX() + k && flag; ++l)
{
for (int i1 = position.getZ() - k; i1 <= position.getZ() + k && flag; ++i1)
{
if (j >= 0 && j < 256)
{
IBlockState iblockstate = worldIn.getBlockState(blockpos$mutableblockpos.setPos(l, j, i1));
Block block = iblockstate.getBlock();
if (!iblockstate.getBlock().isAir(iblockstate, worldIn, blockpos$mutableblockpos.setPos(l, j, i1)) && !iblockstate.getBlock().isLeaves(iblockstate, worldIn, blockpos$mutableblockpos.setPos(l, j, i1)))
{
if (block != Blocks.WATER && block != Blocks.FLOWING_WATER)
{
flag = false;
}
else if (j > position.getY())
{
flag = false;
}
}
}
else
{
flag = false;
}
}
}
}
BlockPos down = position.down();
IBlockState state = worldIn.getBlockState(down);
boolean isSoil = state.getBlock().canSustainPlant(state, worldIn, down, net.minecraft.util.EnumFacing.UP, ((net.minecraft.block.BlockSapling) Blocks.SAPLING));
if (isSoil && position.getY() < worldIn.getHeight() - height - 1)
{
state.getBlock().onPlantGrow(state, worldIn, position.down(), position);
for (int k1 = position.getY() - 3 + height; k1 <= position.getY() + height; ++k1)
{
int j2 = k1 - (position.getY() + height);
int l2 = 2 - j2 / 2;
for (int j3 = position.getX() - l2; j3 <= position.getX() + l2; ++j3)
{
int k3 = j3 - position.getX();
for (int i4 = position.getZ() - l2; i4 <= position.getZ() + l2; ++i4)
{
int j1 = i4 - position.getZ();
if (Math.abs(k3) != l2 || Math.abs(j1) != l2 || random.nextInt(2) != 0 && j2 != 0)
{
BlockPos blockpos = new BlockPos(j3, k1, i4);
state = worldIn.getBlockState(blockpos);
if (state.getBlock().canBeReplacedByLeaves(state, worldIn, blockpos))
{
this.setBlockAndMetadata(worldIn, blockpos, this.leaves);
}
}
}
}
}
for (int l1 = 0; l1 < height; ++l1)
{
BlockPos upN = position.up(l1);
IBlockState iblockstate1 = worldIn.getBlockState(upN);
Block block2 = iblockstate1.getBlock();
if (block2.isAir(iblockstate1, worldIn, upN) || block2.isLeaves(iblockstate1, worldIn, upN) || block2 == Blocks.FLOWING_WATER || block2 == Blocks.WATER)
{
this.setBlockAndMetadata(worldIn, position.up(l1), this.log);
}
}
for (int i2 = position.getY() - 3 + height; i2 <= position.getY() + height; ++i2)
{
int k2 = i2 - (position.getY() + height);
int i3 = 2 - k2 / 2;
BlockPos.MutableBlockPos blockpos$mutableblockpos1 = new BlockPos.MutableBlockPos();
for (int l3 = position.getX() - i3; l3 <= position.getX() + i3; ++l3)
{
for (int j4 = position.getZ() - i3; j4 <= position.getZ() + i3; ++j4)
{
blockpos$mutableblockpos1.setPos(l3, i2, j4);
if (worldIn.getBlockState(blockpos$mutableblockpos1).getMaterial() == Material.LEAVES)
{
BlockPos blockpos3 = blockpos$mutableblockpos1.west();
BlockPos blockpos4 = blockpos$mutableblockpos1.east();
BlockPos blockpos1 = blockpos$mutableblockpos1.north();
BlockPos blockpos2 = blockpos$mutableblockpos1.south();
if (random.nextInt(4) == 0 && isAir(worldIn, blockpos3))
{
this.addVine(worldIn, blockpos3, BlockVine.EAST);
}
if (random.nextInt(4) == 0 && isAir(worldIn, blockpos4))
{
this.addVine(worldIn, blockpos4, BlockVine.WEST);
}
if (random.nextInt(4) == 0 && isAir(worldIn, blockpos1))
{
this.addVine(worldIn, blockpos1, BlockVine.SOUTH);
}
if (random.nextInt(4) == 0 && isAir(worldIn, blockpos2))
{
this.addVine(worldIn, blockpos2, BlockVine.NORTH);
}
}
}
}
}
}
}
}
@SuppressWarnings("deprecation")
BlockPos findGround(World world, BlockPos pos)
{
do
{
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if ((block == Blocks.DIRT || block == Blocks.GRASS || block == Blocks.SAND) && !world.getBlockState(pos.up()).getBlock().isOpaqueCube(state))
{
return pos.up();
}
pos = pos.down();
}
while (pos.getY() > 0);
return pos;
}
private void addVine(World worldIn, BlockPos pos, PropertyBool prop)
{
this.setBlockAndMetadata(worldIn, pos, this.leaves);
int i = 4;
for (pos = pos.down(); isAir(worldIn, pos) && i > 0; --i)
{
this.setBlockAndMetadata(worldIn, pos, this.leaves);
pos = pos.down();
}
}
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);
}
}
private boolean isAir(World world, BlockPos pos)
{
IBlockState state = world.getBlockState(pos);
return state.getBlock().isAir(state, world, pos);
}
}