forked from Creators-of-Create/Create
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeCutter.java
More file actions
344 lines (293 loc) · 10.7 KB
/
TreeCutter.java
File metadata and controls
344 lines (293 loc) · 10.7 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
package com.simibubi.create.content.kinetics.saw;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.simibubi.create.AllTags;
import com.simibubi.create.AllTags.AllBlockTags;
import com.simibubi.create.compat.Mods;
import com.simibubi.create.compat.dynamictrees.DynamicTree;
import com.simibubi.create.foundation.utility.AbstractBlockBreakQueue;
import net.createmod.catnip.data.Iterate;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.tags.BlockTags;
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.block.BambooStalkBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.CactusBlock;
import net.minecraft.world.level.block.ChorusFlowerBlock;
import net.minecraft.world.level.block.ChorusPlantBlock;
import net.minecraft.world.level.block.KelpBlock;
import net.minecraft.world.level.block.KelpPlantBlock;
import net.minecraft.world.level.block.LeavesBlock;
import net.minecraft.world.level.block.SugarCaneBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.Property;
public class TreeCutter {
public static final Tree NO_TREE =
new Tree(Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
public static boolean canDynamicTreeCutFrom(Block startBlock) {
return Mods.DYNAMICTREES.runIfInstalled(() -> () -> DynamicTree.isDynamicBranch(startBlock))
.orElse(false);
}
@Nonnull
public static Optional<AbstractBlockBreakQueue> findDynamicTree(Block startBlock, BlockPos pos) {
if (canDynamicTreeCutFrom(startBlock))
return Mods.DYNAMICTREES.runIfInstalled(() -> () -> new DynamicTree(pos));
return Optional.empty();
}
/**
* Finds a tree at the given pos. Block at the position should be air
*
* @param reader the level that will be searched for a tree
* @param pos position that the saw cut at
* @param brokenState block state what was broken by the saw
*/
@Nonnull
public static Tree findTree(@Nullable BlockGetter reader, BlockPos pos, BlockState brokenState) {
if (reader == null)
return NO_TREE;
List<BlockPos> logs = new ArrayList<>();
List<BlockPos> leaves = new ArrayList<>();
List<BlockPos> attachments = new ArrayList<>();
Set<BlockPos> visited = new HashSet<>();
List<BlockPos> frontier = new LinkedList<>();
BlockState stateAbove = reader.getBlockState(pos.above());
// Bamboo, Sugar Cane, Cactus
if (isVerticalPlant(brokenState)) {
if (!isVerticalPlant(stateAbove))
return NO_TREE;
logs.add(pos.above());
for (int i = 1; i < reader.getHeight(); i++) {
BlockPos current = pos.above(i);
if (!isVerticalPlant(reader.getBlockState(current)))
break;
logs.add(current);
}
Collections.reverse(logs);
return new Tree(logs, leaves, attachments);
}
// Chorus
if (isChorus(brokenState)) {
if (!isChorus(stateAbove))
return NO_TREE;
frontier.add(pos.above());
while (!frontier.isEmpty()) {
BlockPos current = frontier.remove(0);
visited.add(current);
logs.add(current);
for (Direction direction : Iterate.directions) {
BlockPos offset = current.relative(direction);
if (visited.contains(offset))
continue;
if (!isChorus(reader.getBlockState(offset)))
continue;
frontier.add(offset);
}
}
Collections.reverse(logs);
return new Tree(logs, leaves, attachments);
}
// Regular Tree
if (!validateCut(reader, pos))
return NO_TREE;
visited.add(pos);
BlockPos.betweenClosedStream(pos.offset(-1, 0, -1), pos.offset(1, 1, 1))
.forEach(p -> frontier.add(new BlockPos(p)));
// Find all logs & roots
boolean hasRoots = false;
while (!frontier.isEmpty()) {
BlockPos currentPos = frontier.remove(0);
if (!visited.add(currentPos))
continue;
BlockState currentState = reader.getBlockState(currentPos);
if (isRoot(currentState))
hasRoots = true;
else if (!isLog(currentState))
continue;
logs.add(currentPos);
forNeighbours(currentPos, visited, SearchDirection.UP, p -> frontier.add(new BlockPos(p)));
}
visited.clear();
visited.addAll(logs);
frontier.addAll(logs);
if (hasRoots) {
Set<BlockPos> oldLogs = new HashSet<>(logs);
while (!frontier.isEmpty()) {
BlockPos currentPos = frontier.remove(0);
BlockState currentState = reader.getBlockState(currentPos);
if (!isRoot(currentState))
continue;
if (!oldLogs.contains(currentPos))
logs.add(currentPos);
forNeighbours(currentPos, visited, SearchDirection.DOWN, p -> {
BlockPos neighbourPos = p.immutable();
if (visited.add(neighbourPos))
frontier.add(neighbourPos);
});
}
visited.clear();
visited.addAll(logs);
frontier.addAll(logs);
}
// Find all leaves
while (!frontier.isEmpty()) {
BlockPos prevPos = frontier.remove(0);
BlockState prevState = reader.getBlockState(prevPos);
int prevLeafDistance = isLeaf(prevState) ? getLeafDistance(prevState) : 0;
forNeighbours(prevPos, visited, SearchDirection.BOTH, currentPos -> {
BlockState state = reader.getBlockState(currentPos);
BlockPos subtract = currentPos.subtract(pos);
BlockPos currentPosImmutable = currentPos.immutable();
if (AllBlockTags.TREE_ATTACHMENTS.matches(state)) {
attachments.add(currentPosImmutable);
visited.add(currentPosImmutable);
return;
}
int horizontalDistance = Math.max(Math.abs(subtract.getX()), Math.abs(subtract.getZ()));
if (horizontalDistance <= nonDecayingLeafDistance(state) && visited.add(currentPosImmutable)) {
leaves.add(currentPosImmutable);
frontier.add(currentPosImmutable);
return;
}
if (isLeaf(state) && getLeafDistance(state) > prevLeafDistance && visited.add(currentPosImmutable)) {
leaves.add(currentPosImmutable);
frontier.add(currentPosImmutable);
return;
}
});
}
return new Tree(logs, leaves, attachments);
}
private static int getLeafDistance(BlockState state) {
IntegerProperty distanceProperty = LeavesBlock.DISTANCE;
for (Property<?> property : state.getValues()
.keySet())
if (property instanceof IntegerProperty ip && property.getName()
.equals("distance"))
distanceProperty = ip;
return state.getValue(distanceProperty);
}
public static boolean isChorus(BlockState stateAbove) {
return stateAbove.getBlock() instanceof ChorusPlantBlock || stateAbove.getBlock() instanceof ChorusFlowerBlock;
}
public static boolean isVerticalPlant(BlockState stateAbove) {
Block block = stateAbove.getBlock();
if (block instanceof BambooStalkBlock)
return true;
if (block instanceof CactusBlock)
return true;
if (block instanceof SugarCaneBlock)
return true;
if (block instanceof KelpPlantBlock)
return true;
return block instanceof KelpBlock;
}
/**
* Checks whether a tree was fully cut by seeing whether the layer above the cut
* is not supported by any more logs.
*
* @param reader
* @param pos
* @return
*/
private static boolean validateCut(BlockGetter reader, BlockPos pos) {
Set<BlockPos> visited = new HashSet<>();
List<BlockPos> frontier = new LinkedList<>();
frontier.add(pos);
frontier.add(pos.above());
int posY = pos.getY();
while (!frontier.isEmpty()) {
BlockPos currentPos = frontier.remove(0);
BlockPos belowPos = currentPos.below();
visited.add(currentPos);
boolean lowerLayer = currentPos.getY() == posY;
BlockState currentState = reader.getBlockState(currentPos);
BlockState belowState = reader.getBlockState(belowPos);
if (!isLog(currentState) && !isRoot(currentState))
continue;
if (!lowerLayer && !pos.equals(belowPos) && (isLog(belowState) || isRoot(belowState)))
return false;
for (Direction direction : Iterate.directions) {
if (direction == Direction.DOWN)
continue;
if (direction == Direction.UP && !lowerLayer)
continue;
BlockPos offset = currentPos.relative(direction);
if (visited.contains(offset))
continue;
frontier.add(offset);
}
}
return true;
}
private enum SearchDirection {
UP(0, 1), DOWN(-1, 0), BOTH(-1, 1);
int minY;
int maxY;
private SearchDirection(int minY, int maxY) {
this.minY = minY;
this.maxY = maxY;
}
}
private static void forNeighbours(BlockPos pos, Set<BlockPos> visited, SearchDirection direction,
Consumer<BlockPos> acceptor) {
BlockPos.betweenClosedStream(pos.offset(-1, direction.minY, -1), pos.offset(1, direction.maxY, 1))
.filter(((Predicate<BlockPos>) visited::contains).negate())
.forEach(acceptor);
}
public static boolean isRoot(BlockState state) {
return AllBlockTags.ROOTS.matches(state);
}
public static boolean isLog(BlockState state) {
return state.is(BlockTags.LOGS) || AllTags.AllBlockTags.SLIMY_LOGS.matches(state)
|| state.is(Blocks.MUSHROOM_STEM);
}
private static int nonDecayingLeafDistance(BlockState state) {
if (state.is(Blocks.RED_MUSHROOM_BLOCK))
return 2;
if (state.is(Blocks.BROWN_MUSHROOM_BLOCK))
return 3;
if (state.is(BlockTags.WART_BLOCKS) || state.is(Blocks.WEEPING_VINES) || state.is(Blocks.WEEPING_VINES_PLANT))
return 3;
return -1;
}
private static boolean isLeaf(BlockState state) {
for (Property<?> property : state.getValues().keySet())
if (property instanceof IntegerProperty && property.getName().equals("distance") && property != BlockStateProperties.STABILITY_DISTANCE)
return true;
return false;
}
public static class Tree extends AbstractBlockBreakQueue {
private final List<BlockPos> logs;
private final List<BlockPos> leaves;
private final List<BlockPos> attachments;
public Tree(List<BlockPos> logs, List<BlockPos> leaves, List<BlockPos> attachments) {
this.logs = logs;
this.leaves = leaves;
this.attachments = attachments;
}
@Override
public void destroyBlocks(Level world, ItemStack toDamage, @Nullable Player playerEntity,
BiConsumer<BlockPos, ItemStack> drop) {
attachments.forEach(makeCallbackFor(world, 1 / 32f, toDamage, playerEntity, drop));
logs.forEach(makeCallbackFor(world, 1 / 2f, toDamage, playerEntity, drop));
leaves.forEach(makeCallbackFor(world, 1 / 8f, toDamage, playerEntity, drop));
}
}
}