-
Notifications
You must be signed in to change notification settings - Fork 81
/
WillowTreeFeature.java
302 lines (250 loc) · 12.9 KB
/
WillowTreeFeature.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.progwml6.natura.world.worldgen.trees.feature;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mojang.serialization.Codec;
import com.progwml6.natura.world.worldgen.trees.config.BaseOverworldTreeFeatureConfig;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.LeavesBlock;
import net.minecraft.block.material.Material;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tags.BlockTags;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MutableBoundingBox;
import net.minecraft.util.math.shapes.BitSetVoxelShapePart;
import net.minecraft.util.math.shapes.VoxelShapePart;
import net.minecraft.world.ISeedReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.IWorldWriter;
import net.minecraft.world.gen.ChunkGenerator;
import net.minecraft.world.gen.Heightmap;
import net.minecraft.world.gen.IWorldGenerationBaseReader;
import net.minecraft.world.gen.IWorldGenerationReader;
import net.minecraft.world.gen.feature.Feature;
import net.minecraft.world.gen.feature.template.Template;
import java.util.List;
import java.util.Random;
import java.util.Set;
public class WillowTreeFeature extends Feature<BaseOverworldTreeFeatureConfig> {
public WillowTreeFeature(Codec<BaseOverworldTreeFeatureConfig> codec) {
super(codec);
}
@Override
public final boolean generate(ISeedReader seedReader, ChunkGenerator chunkGenerator, Random random, BlockPos blockPos, BaseOverworldTreeFeatureConfig config) {
Set<BlockPos> set = Sets.newHashSet();
Set<BlockPos> set1 = Sets.newHashSet();
Set<BlockPos> set2 = Sets.newHashSet();
MutableBoundingBox mutableboundingbox = MutableBoundingBox.getNewBoundingBox();
boolean flag = this.place(seedReader, random, blockPos, set, set1, mutableboundingbox, config);
if (mutableboundingbox.minX <= mutableboundingbox.maxX && flag && !set.isEmpty()) {
VoxelShapePart voxelshapepart = this.func_236403_a_(seedReader, mutableboundingbox, set, set2);
Template.func_222857_a(seedReader, 3, voxelshapepart, mutableboundingbox.minX, mutableboundingbox.minY, mutableboundingbox.minZ);
return true;
}
else {
return false;
}
}
private boolean place(IWorldGenerationReader generationReader, Random rand, BlockPos positionIn, Set<BlockPos> trunkBlockPosSet, Set<BlockPos> leavesBlockPosSet, MutableBoundingBox boundingBoxIn, BaseOverworldTreeFeatureConfig configIn) {
int height = rand.nextInt(configIn.randomHeight) + configIn.baseHeight;
if (!(generationReader instanceof IWorld)) {
return false;
}
BlockPos blockpos;
if (!configIn.forcePlacement) {
int oceanFloorHeight = generationReader.getHeight(Heightmap.Type.OCEAN_FLOOR, positionIn).getY();
blockpos = new BlockPos(positionIn.getX(), oceanFloorHeight, positionIn.getZ());
}
else {
blockpos = positionIn;
}
if (blockpos.getY() >= 1 && blockpos.getY() + height + 1 <= 256) {
if (!isDirtOrFarmlandAt(generationReader, blockpos.down())) {
return false;
}
else {
this.setDirtAt(generationReader, blockpos.down(), blockpos);
this.placeTrunk(generationReader, rand, height, blockpos, trunkBlockPosSet, boundingBoxIn, configIn);
this.placeCanopy(generationReader, rand, height, blockpos, trunkBlockPosSet, boundingBoxIn, configIn);
return true;
}
}
else {
return false;
}
}
protected void setDirtAt(IWorldGenerationReader reader, BlockPos pos, BlockPos origin) {
if (!(reader instanceof IWorld)) {
return;
}
((IWorld) reader).getBlockState(pos).getBlock().onPlantGrow(((IWorld) reader).getBlockState(pos), (IWorld) reader, pos, origin);
}
protected void placeTrunk(IWorldGenerationReader worldIn, Random randomIn, int treeHeight, BlockPos blockPos, Set<BlockPos> blockPosSet, MutableBoundingBox mutableBoundingBoxIn, BaseOverworldTreeFeatureConfig treeFeatureConfigIn) {
while (treeHeight > 0) {
this.setLog(worldIn, randomIn, blockPos, blockPosSet, mutableBoundingBoxIn, treeFeatureConfigIn);
blockPos = blockPos.up();
treeHeight--;
}
}
protected void placeCanopy(IWorldGenerationReader worldIn, Random randomIn, int treeHeight, BlockPos blockPos, Set<BlockPos> blockPosSet, MutableBoundingBox mutableBoundingBoxIn, BaseOverworldTreeFeatureConfig treeFeatureConfigIn) {
for (int k1 = blockPos.getY() - 3 + treeHeight; k1 <= blockPos.getY() + treeHeight; ++k1) {
int j2 = k1 - (blockPos.getY() + treeHeight);
int l2 = 2 - j2 / 2;
for (int j3 = blockPos.getX() - l2; j3 <= blockPos.getX() + l2; ++j3) {
int k3 = j3 - blockPos.getX();
for (int i4 = blockPos.getZ() - l2; i4 <= blockPos.getZ() + l2; ++i4) {
int j1 = i4 - blockPos.getZ();
if (Math.abs(k3) != l2 || Math.abs(j1) != l2 || randomIn.nextInt(2) != 0 && j2 != 0) {
this.setLeaf(worldIn, randomIn, new BlockPos(j3, k1, i4), blockPosSet, mutableBoundingBoxIn, treeFeatureConfigIn);
}
}
}
}
for (int i2 = blockPos.getY() - 3 + treeHeight; i2 <= blockPos.getY() + treeHeight; ++i2) {
int k2 = i2 - (blockPos.getY() + treeHeight);
int i3 = 2 - k2 / 2;
BlockPos.Mutable mutableBlockPos = new BlockPos.Mutable();
for (int l3 = blockPos.getX() - i3; l3 <= blockPos.getX() + i3; ++l3) {
for (int j4 = blockPos.getZ() - i3; j4 <= blockPos.getZ() + i3; ++j4) {
mutableBlockPos.setPos(l3, i2, j4);
if (worldIn.hasBlockState(mutableBlockPos, (state -> state.getMaterial() == Material.LEAVES))) {
BlockPos west = mutableBlockPos.west();
BlockPos east = mutableBlockPos.east();
BlockPos north = mutableBlockPos.north();
BlockPos south = mutableBlockPos.south();
if (randomIn.nextInt(4) == 0 && isAirAt(worldIn, west)) {
this.placeDownLeaves(worldIn, randomIn, west, blockPosSet, mutableBoundingBoxIn, treeFeatureConfigIn);
}
if (randomIn.nextInt(4) == 0 && isAirAt(worldIn, east)) {
this.placeDownLeaves(worldIn, randomIn, east, blockPosSet, mutableBoundingBoxIn, treeFeatureConfigIn);
}
if (randomIn.nextInt(4) == 0 && isAirAt(worldIn, north)) {
this.placeDownLeaves(worldIn, randomIn, north, blockPosSet, mutableBoundingBoxIn, treeFeatureConfigIn);
}
if (randomIn.nextInt(4) == 0 && isAirAt(worldIn, south)) {
this.placeDownLeaves(worldIn, randomIn, south, blockPosSet, mutableBoundingBoxIn, treeFeatureConfigIn);
}
}
}
}
}
}
private void placeDownLeaves(IWorldGenerationReader worldIn, Random randomIn, BlockPos blockPos, Set<BlockPos> blockPosSet, MutableBoundingBox mutableBoundingBoxIn, BaseOverworldTreeFeatureConfig treeFeatureConfigIn) {
this.setLeaf(worldIn, randomIn, blockPos, blockPosSet, mutableBoundingBoxIn, treeFeatureConfigIn);
int i = 4;
for (blockPos = blockPos.down(); isAirAt(worldIn, blockPos) && i > 0; --i) {
this.setLeaf(worldIn, randomIn, blockPos, blockPosSet, mutableBoundingBoxIn, treeFeatureConfigIn);
blockPos = blockPos.down();
}
}
protected boolean setLog(IWorldGenerationReader worldIn, Random randomIn, BlockPos blockPos, Set<BlockPos> blockPosSet, MutableBoundingBox mutableBoundingBoxIn, BaseOverworldTreeFeatureConfig treeFeatureConfigIn) {
if (!isAirOrLeavesAt(worldIn, blockPos)) {
return false;
}
else {
this.setBlockState(worldIn, blockPos, treeFeatureConfigIn.trunkProvider.getBlockState(randomIn, blockPos));
mutableBoundingBoxIn.expandTo(new MutableBoundingBox(blockPos, blockPos));
blockPosSet.add(blockPos.toImmutable());
return true;
}
}
protected boolean setLeaf(IWorldGenerationReader worldIn, Random random, BlockPos blockPos, Set<BlockPos> blockPosSet, MutableBoundingBox mutableBoundingBoxIn, BaseOverworldTreeFeatureConfig treeFeatureConfigIn) {
if (!isAirOrLeavesAt(worldIn, blockPos)) {
return false;
}
else {
this.setBlockState(worldIn, blockPos, treeFeatureConfigIn.leavesProvider.getBlockState(random, blockPos));
mutableBoundingBoxIn.expandTo(new MutableBoundingBox(blockPos, blockPos));
blockPosSet.add(blockPos.toImmutable());
return true;
}
}
private static boolean isWaterAt(IWorldGenerationBaseReader reader, BlockPos blockPos) {
return reader.hasBlockState(blockPos, (p_236413_0_) -> p_236413_0_.isIn(Blocks.WATER));
}
public static boolean isAirOrLeavesAt(IWorldGenerationBaseReader reader, BlockPos blockPos) {
return reader.hasBlockState(blockPos, (p_236411_0_) -> p_236411_0_.isAir() || p_236411_0_.isIn(BlockTags.LEAVES));
}
public static boolean isAirAt(IWorldGenerationBaseReader reader, BlockPos blockPos) {
return reader.hasBlockState(blockPos, AbstractBlock.AbstractBlockState::isAir);
}
private static boolean isDirtOrFarmlandAt(IWorldGenerationBaseReader reader, BlockPos blockPos) {
return reader.hasBlockState(blockPos, (state) -> {
Block block = state.getBlock();
return isDirt(block) || block == Blocks.FARMLAND;
});
}
private static boolean isTallPlantAt(IWorldGenerationBaseReader reader, BlockPos blockPos) {
return reader.hasBlockState(blockPos, (p_236406_0_) -> {
Material material = p_236406_0_.getMaterial();
return material == Material.TALL_PLANTS;
});
}
public static boolean isReplaceableAt(IWorldGenerationBaseReader reader, BlockPos blockPos) {
return isAirOrLeavesAt(reader, blockPos) || isTallPlantAt(reader, blockPos) || isWaterAt(reader, blockPos);
}
public static void setBlockStateAt(IWorldWriter writer, BlockPos blockPos, BlockState state) {
writer.setBlockState(blockPos, state, 19);
}
private VoxelShapePart func_236403_a_(IWorld world, MutableBoundingBox boundingBox, Set<BlockPos> logs, Set<BlockPos> leaves) {
List<Set<BlockPos>> list = Lists.newArrayList();
VoxelShapePart voxelshapepart = new BitSetVoxelShapePart(boundingBox.getXSize(), boundingBox.getYSize(), boundingBox.getZSize());
for (int j = 0; j < 6; ++j) {
list.add(Sets.newHashSet());
}
BlockPos.Mutable mutable = new BlockPos.Mutable();
for (BlockPos blockpos : Lists.newArrayList(leaves)) {
if (boundingBox.isVecInside(blockpos)) {
voxelshapepart.setFilled(blockpos.getX() - boundingBox.minX, blockpos.getY() - boundingBox.minY, blockpos.getZ() - boundingBox.minZ, true, true);
}
}
for (BlockPos blockpos1 : Lists.newArrayList(logs)) {
if (boundingBox.isVecInside(blockpos1)) {
voxelshapepart.setFilled(blockpos1.getX() - boundingBox.minX, blockpos1.getY() - boundingBox.minY, blockpos1.getZ() - boundingBox.minZ, true, true);
}
for (Direction direction : Direction.values()) {
mutable.setAndMove(blockpos1, direction);
if (!logs.contains(mutable)) {
BlockState blockstate = world.getBlockState(mutable);
if (blockstate.hasProperty(BlockStateProperties.DISTANCE_1_7)) {
list.get(0).add(mutable.toImmutable());
setBlockStateAt(world, mutable, blockstate.with(BlockStateProperties.DISTANCE_1_7, Integer.valueOf(1)));
if (boundingBox.isVecInside(mutable)) {
voxelshapepart.setFilled(mutable.getX() - boundingBox.minX, mutable.getY() - boundingBox.minY, mutable.getZ() - boundingBox.minZ, true, true);
}
}
}
}
}
for (int l = 1; l < 6; ++l) {
Set<BlockPos> set = list.get(l - 1);
Set<BlockPos> set1 = list.get(l);
for (BlockPos blockpos2 : set) {
if (boundingBox.isVecInside(blockpos2)) {
voxelshapepart.setFilled(blockpos2.getX() - boundingBox.minX, blockpos2.getY() - boundingBox.minY, blockpos2.getZ() - boundingBox.minZ, true, true);
}
for (Direction direction1 : Direction.values()) {
mutable.setAndMove(blockpos2, direction1);
if (!set.contains(mutable) && !set1.contains(mutable)) {
BlockState blockstate1 = world.getBlockState(mutable);
if (blockstate1.hasProperty(BlockStateProperties.DISTANCE_1_7)) {
int k = blockstate1.get(BlockStateProperties.DISTANCE_1_7);
if (k > l + 1) {
BlockState blockstate2 = blockstate1.with(BlockStateProperties.DISTANCE_1_7, Integer.valueOf(l + 1));
setBlockStateAt(world, mutable, blockstate2);
if (boundingBox.isVecInside(mutable)) {
voxelshapepart.setFilled(mutable.getX() - boundingBox.minX, mutable.getY() - boundingBox.minY, mutable.getZ() - boundingBox.minZ, true, true);
}
set1.add(mutable.toImmutable());
}
}
}
}
}
}
return voxelshapepart;
}
}