-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathTreeHelper.java
More file actions
435 lines (368 loc) · 17.6 KB
/
TreeHelper.java
File metadata and controls
435 lines (368 loc) · 17.6 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package com.ferreusveritas.dynamictrees.api;
import com.ferreusveritas.dynamictrees.api.network.MapSignal;
import com.ferreusveritas.dynamictrees.api.treedata.TreePart;
import com.ferreusveritas.dynamictrees.block.NullTreePart;
import com.ferreusveritas.dynamictrees.block.branch.BranchBlock;
import com.ferreusveritas.dynamictrees.block.branch.TrunkShellBlock;
import com.ferreusveritas.dynamictrees.block.leaves.DynamicLeavesBlock;
import com.ferreusveritas.dynamictrees.block.rooty.RootyBlock;
import com.ferreusveritas.dynamictrees.init.DTClient;
import com.ferreusveritas.dynamictrees.systems.nodemapper.TwinkleNode;
import com.ferreusveritas.dynamictrees.tree.species.Species;
import com.ferreusveritas.dynamictrees.util.BranchDestructionData;
import com.ferreusveritas.dynamictrees.util.SafeChunkBounds;
import com.ferreusveritas.dynamictrees.util.SimpleVoxmap;
import com.ferreusveritas.dynamictrees.worldgen.JoCode;
import com.ferreusveritas.dynamictrees.worldgen.RootsJoCode;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.particles.SimpleParticleType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import javax.annotation.Nullable;
import java.util.Optional;
import java.util.function.BiConsumer;
public class TreeHelper {
public static final TreePart NULL_TREE_PART = new NullTreePart();
///////////////////////////////////////////
//CONVENIENCE METHODS
///////////////////////////////////////////
/**
* Convenience method to pulse a single growth cycle and age the cuboid volume. Used by growth potions, fertilizers
* and the dendrocoil.
*/
public static void growPulse(Level level, BlockPos rootPos) {
BlockState rootyState = level.getBlockState(rootPos);
RootyBlock dirt = TreeHelper.getRooty(rootyState);
if (dirt != null) {
dirt.updateTree(rootyState, level, rootPos, level.random, false);
ageVolume(level, rootPos, 8, 32, 1, SafeChunkBounds.ANY);//blindly age a cuboid volume
}
}
/**
* Pulses an entire leafMap volume of blocks each with an age signal. Warning: CPU intensive and should be used
* sparingly.
*
* @param level The {@link LevelAccessor} instance.
* @param leafMap The voxel map of hydro values to use as an iterator.
* @param iterations The number of times to age the volume.
*/
public static void ageVolume(LevelAccessor level, SimpleVoxmap leafMap, int iterations, SafeChunkBounds safeBounds) {
//The iterMap is the voxmap we will use as a discardable. The leafMap must survive for snow
SimpleVoxmap iterMap = leafMap != null ? new SimpleVoxmap(leafMap) : null;
for (int i = 0; i < iterations; i++) {
for (BlockPos.MutableBlockPos iPos : iterMap.getAllNonZero()) {
BlockState blockState = level.getBlockState(iPos);
Block block = blockState.getBlock();
if (block instanceof DynamicLeavesBlock) {//Special case for leaves
int prevHydro = leafMap.getVoxel(iPos);//The leafMap should contain accurate hydro data
int newHydro = ((Ageable) block).age(level, iPos, blockState, level.getRandom(), safeBounds);//Get new values from neighbors
if (newHydro == -1) {
//Leaf block died. Take it out of the leafMap and iterMap
leafMap.setVoxel(iPos, (byte) 0);
iterMap.setVoxel(iPos, (byte) 0);
} else {
//Leaf did not die so the block is still leaves
if (prevHydro == newHydro) { //But it didn't change
iterMap.setVoxel(iPos, (byte) 0); //Stop iterating over it if it's not changing
} else {//Oh, wait... it did change
//Update both maps with this new hydro value
leafMap.setVoxel(iPos, (byte) newHydro);
iterMap.setVoxel(iPos, (byte) newHydro);
//Copy all the surrounding values from the leafMap to the iterMap since they now also have potential to change
for (Direction dir : Direction.values()) {
BlockPos dPos = iPos.relative(dir);
iterMap.setVoxel(dPos, leafMap.getVoxel(dPos));
}
}
}
} else if (block instanceof Ageable) {//Treat as just a regular ageable block
((Ageable) block).age(level, iPos, blockState, level.getRandom(), safeBounds);
} else {//You're not supposed to be here
leafMap.setVoxel(iPos, (byte) 0);
iterMap.setVoxel(iPos, (byte) 0);
}
}
}
}
/**
* Pulses an entire cuboid volume of blocks each with an age signal. Warning: CPU intensive and should be used
* sparingly
*
* @param level The level
* @param treePos The position of the bottom most block of a trees trunk
* @param halfWidth The "radius" of the cuboid volume
* @param height The height of the cuboid volume
* @param iterations The number of times to age the volume
*/
public static void ageVolume(LevelAccessor level, BlockPos treePos, int halfWidth, int height, int iterations, SafeChunkBounds safeBounds) {
//Slow and dirty iteration over a cuboid volume. Try to avoid this by using a voxmap if you can
Iterable<BlockPos> iterable = BlockPos.betweenClosed(treePos.offset(new BlockPos(-halfWidth, 0, -halfWidth)), treePos.offset(new BlockPos(halfWidth, height, halfWidth)));
for (int i = 0; i < iterations; i++) {
for (BlockPos iPos : iterable) {
BlockState blockState = level.getBlockState(iPos);
Block block = blockState.getBlock();
if (block instanceof Ageable) {
((Ageable) block).age(level, iPos, blockState, level.getRandom(), safeBounds);//Treat as just a regular ageable block
}
}
}
}
public static Optional<JoCode> getRootsJoCode(Level level, BlockPos pos) {
return getJoCode(level, pos, Direction.SOUTH, true);
}
public static Optional<JoCode> getJoCode(Level level, BlockPos pos) {
return getJoCode(level, pos, Direction.SOUTH, false);
}
public static Optional<JoCode> getJoCode(Level level, BlockPos pos, Direction direction, boolean roots) {
if (pos == null) {
return Optional.empty();
}
pos = dereferenceTrunkShell(level, pos);
BlockPos rootPos = TreeHelper.findRootNode(level, pos);
return rootPos != BlockPos.ZERO ? Optional.of (
roots ? new RootsJoCode(level, rootPos, direction)
: new JoCode(level, rootPos, direction)
) : Optional.empty();
}
public static BlockPos dereferenceTrunkShell(Level level, BlockPos pos) {
BlockState blockState = level.getBlockState(pos);
if (blockState.getBlock() instanceof TrunkShellBlock) {
TrunkShellBlock.ShellMuse muse = ((TrunkShellBlock) blockState.getBlock()).getMuse(level, blockState, pos);
if (muse != null) {
return muse.pos();
}
}
return pos;
}
public static Species getCommonSpecies(Level level, BlockPos pos) {
pos = dereferenceTrunkShell(level, pos);
BlockState state = level.getBlockState(pos);
if (state.getBlock() instanceof BranchBlock) {
BranchBlock branch = (BranchBlock) state.getBlock();
return branch.getFamily().getCommonSpecies();
}
return Species.NULL_SPECIES;
}
/**
* This is resource intensive. Use only for interaction code. Only the root node can determine the exact species
* and it has to be found by mapping the branch network.
*
* @param level The {@link Level} instance.
* @param pos The {@link BlockPos} to find the {@link Species} at.
* @return The {@link Species}, or {@link Species#NULL_SPECIES} if one couldn't be found.
*/
public static Species getExactSpecies(Level level, BlockPos pos) {
BlockPos rootPos = findRootNode(level, pos);
if (rootPos != BlockPos.ZERO) {
BlockState rootyState = level.getBlockState(rootPos);
return TreeHelper.getRooty(rootyState).getSpecies(rootyState, level, rootPos);
}
return Species.NULL_SPECIES;
}
/**
* This is resource intensive. Use only for interaction code. Only the root node can determine the exact species
* and it has to be found by mapping the branch network. Tries to find the exact species and if that fails tries to
* find the common species.
*
* @param level
* @param pos
* @return
*/
public static Species getBestGuessSpecies(Level level, BlockPos pos) {
Species species = getExactSpecies(level, pos);
return species == Species.NULL_SPECIES ? getCommonSpecies(level, pos) : species;
}
/**
* Find the root node of a tree.
*
* @param level The level
* @param pos The position being analyzed
* @return The position of the root node of the tree or BlockPos.ZERO if nothing was found.
*/
public static BlockPos findRootNode(Level level, BlockPos pos) {
pos = dereferenceTrunkShell(level, pos);
BlockState state = level.getBlockState(pos);
TreePart treePart = TreeHelper.getTreePart(level.getBlockState(pos));
switch (treePart.getTreePartType()) {
case BRANCH:
MapSignal signal = treePart.analyse(state, level, pos, null, new MapSignal());// Analyze entire tree network to find root node
if (signal.foundRoot) {
return signal.root;
}
break;
case ROOT:
return pos;
default:
return BlockPos.ZERO;
}
return BlockPos.ZERO;
}
/**
* Sets a custom rooty block decay (what dirt it becomes when the tree is gone) algorithm for mods that have special
* requirements.
*
* @param decay The {@link RootyBlockDecayer} implementation.
*/
public static void setCustomRootBlockDecay(RootyBlockDecayer decay) {
RootyBlock.rootyBlockDecayer = decay;
}
/**
* Provided as a means for an implementation to chain the handlers.
*
* @return The currently defined {@link RootyBlockDecayer} handler.
*/
public static RootyBlockDecayer getCustomRootBlockDecay() {
return RootyBlock.rootyBlockDecayer;
}
/**
* Convenience function that spawns particles all over the tree branches
*/
public static void treeParticles(Level level, BlockPos rootPos, SimpleParticleType type, int num) {
if (level.isClientSide) {
startAnalysisFromRoot(level, rootPos, new MapSignal(new TwinkleNode(type, num)));
}
}
public static void rootParticles(Level level, BlockPos rootPos, Direction offset, SimpleParticleType type, int num) {
if (level.isClientSide) {
if (level.isClientSide() && level.getBlockState(rootPos).getBlock() instanceof RootyBlock) {
final BlockPos particlePos = rootPos.offset(offset.getNormal());
DTClient.spawnParticles(level, type, particlePos.getX(), particlePos.getY(), particlePos.getZ(), num, level.getRandom());
}
}
}
/**
* Convenience function that verifies an analysis is starting from the root node before commencing.
*
* @param level The level
* @param rootPos The position of the rootyBlock
* @param signal The signal carrying the inspectors
* @return true if a root block was found.
*/
public static boolean startAnalysisFromRoot(LevelAccessor level, BlockPos rootPos, MapSignal signal) {
RootyBlock dirt = TreeHelper.getRooty(level.getBlockState(rootPos));
if (dirt != null) {
dirt.startAnalysis(level, rootPos, signal);
return true;
}
return false;
}
/**
* Destroys a part of the tree from the specified {@code cutPos}. Destruction is handled fully by this method,
* except for drops which are returned to be handled by the caller using the {@code dropConsumer} parameter.
* <p>
* This may be used for mod compatibility with machines that cut down trees automatically, for example the Create
* saw. This method was based on how the compatibility with Create worked, making it easier for them and others
* to provide integration.
*
* @param dropConsumer called for all drops with the block position at which they should drop and the stack.
* For logs and sticks this will be the cut position, while for leaves this will be the
* position of the respective leave block that created this drop.
*/
public static void destroyTree(Level level, BlockPos cutPos, @Nullable Player player, BiConsumer<BlockPos, ItemStack> dropConsumer) {
BlockPos startPos = dereferenceTrunkShell(level, cutPos);
BranchBlock cutBlock = getBranch(level.getBlockState(startPos));
// Fire event for break sound and particles
level.levelEvent(null, 2001, cutPos, Block.getId(level.getBlockState(cutPos)));
BranchDestructionData destructionData = cutBlock.destroyBranchFromNode(level, cutPos, Direction.DOWN, false, player);
// Allow drop consumer callback to handle drops
destructionData.leavesDrops.forEach(stackData -> dropConsumer.accept(stackData.pos, stackData.stack));
destructionData.species.getBranchesDrops(level, destructionData.woodVolume).forEach(stack -> dropConsumer.accept(startPos, stack));
}
//Treeparts
public static boolean isTreePart(Block block) {
return block instanceof TreePart;
}
public static boolean isTreePart(BlockState state) {
return isTreePart(state.getBlock());
}
public static boolean isTreePart(LevelAccessor level, BlockPos pos) {
return isTreePart(level.getBlockState(pos).getBlock());
}
public static TreePart getTreePart(Block block) {
return isTreePart(block) ? (TreePart) block : NULL_TREE_PART;
}
public static TreePart getTreePart(BlockState state) {
return getTreePart(state.getBlock());
}
//Branches
public static boolean isBranch(Block block) {
return block instanceof BranchBlock;//Oh shuddap you java purists.. this is minecraft!
}
public static boolean isBranch(@Nullable final BlockState state) {
return state != null && isBranch(state.getBlock());
}
@Nullable
public static BranchBlock getBranch(Block block) {
return isBranch(block) ? (BranchBlock) block : null;
}
@Nullable
public static BranchBlock getBranch(TreePart treePart) {
return treePart instanceof BranchBlock ? (BranchBlock) treePart : null;
}
@Nullable
public static BranchBlock getBranch(BlockState state) {
return getBranch(state.getBlock());
}
public static int getRadius(BlockGetter level, BlockPos pos) {
BlockState state = level.getBlockState(pos);
return getTreePart(state).getRadius(state);
}
public static Optional<BranchBlock> getBranchOpt(Block block) {
return isBranch(block) ? Optional.of((BranchBlock) block) : Optional.empty();
}
public static Optional<BranchBlock> getBranchOpt(BlockState state) {
final Block block = state.getBlock();
return isBranch(block) ? Optional.of((BranchBlock) block) : Optional.empty();
}
public static Optional<BranchBlock> getBranchOpt(TreePart treePart) {
return treePart instanceof BranchBlock ? Optional.of((BranchBlock) treePart) : Optional.empty();
}
public static Optional<RootyBlock> getRootyOpt(BlockState state) {
Block block = state.getBlock();
return isRooty(block) ? Optional.of((RootyBlock) block) : Optional.empty();
}
//Leaves
public static boolean isLeaves(Block block) {
return block instanceof DynamicLeavesBlock;
}
public static boolean isLeaves(BlockState state) {
return isLeaves(state.getBlock());
}
@Nullable
public static DynamicLeavesBlock getLeaves(Block block) {
return isLeaves(block) ? (DynamicLeavesBlock) block : null;
}
@Nullable
public static DynamicLeavesBlock getLeaves(TreePart treePart) {
return treePart instanceof DynamicLeavesBlock ? (DynamicLeavesBlock) treePart : null;
}
@Nullable
public static DynamicLeavesBlock getLeaves(BlockState state) {
return getLeaves(state.getBlock());
}
//Rooty
public static boolean isRooty(Block block) {
return block instanceof RootyBlock;
}
public static boolean isRooty(BlockState state) {
return isRooty(state.getBlock());
}
@Nullable
public static RootyBlock getRooty(Block block) {
return isRooty(block) ? (RootyBlock) block : null;
}
@Nullable
public static RootyBlock getRooty(TreePart treePart) {
return treePart instanceof RootyBlock ? (RootyBlock) treePart : null;
}
@Nullable
public static RootyBlock getRooty(BlockState state) {
return getRooty(state.getBlock());
}
}