Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions src/main/java/tconstruct/items/tools/LumberAxe.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package tconstruct.items.tools;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Queue;
import java.util.Set;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeSet;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
Expand Down Expand Up @@ -118,41 +118,48 @@ public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPla

public static boolean detectTree(World world, int pX, int pY, int pZ) {
Stack<ChunkPosition> candidates = new Stack<>();
SortedSet<ChunkPosition> visited = new TreeSet<>(Comparator.comparingInt((ChunkPosition a) -> a.chunkPosY));
Set<ChunkPosition> wood = new HashSet<>();
Set<ChunkPosition> nonWood = new HashSet<>();

candidates.add(new ChunkPosition(pX, pY, pZ));

while (!candidates.isEmpty()) {
ChunkPosition candidate = candidates.pop();
int curX = candidate.chunkPosX, curY = candidate.chunkPosY, curZ = candidate.chunkPosZ;

Block block = world.getBlock(curX, curY, curZ);
if (!block.isWood(world, curX, curY, curZ)) {
continue;
}
if (!visited.add(candidate)) {
if (wood.contains(candidate) || nonWood.contains(candidate)) {
continue;
}

// add the current layer and above
for (int offX = 0; offX < 3; offX++) {
for (int offY = 0; offY < 2; offY++) {
for (int offZ = 0; offZ < 3; offZ++) {
ChunkPosition newCandidate = new ChunkPosition(curX - 1 + offX, curY + offY, curZ - 1 + offZ);
if (!visited.contains(newCandidate)) {
candidates.add(newCandidate);
int curX = candidate.chunkPosX, curY = candidate.chunkPosY, curZ = candidate.chunkPosZ;

Block block = world.getBlock(curX, curY, curZ);
if (block.isWood(world, curX, curY, curZ)) {
for (int offX = 0; offX < 3; offX++) {
for (int offY = 0; offY < 2; offY++) {
for (int offZ = 0; offZ < 3; offZ++) {
ChunkPosition newCandidate = new ChunkPosition(
curX - 1 + offX,
curY + offY,
curZ - 1 + offZ);
if (!(wood.contains(newCandidate) || nonWood.contains(newCandidate))) {
candidates.add(newCandidate);
}
}
}
}
wood.add(candidate);
} else {
nonWood.add(candidate);
}

}

// not even one match, so there were no logs.
if (visited.isEmpty()) {
if (wood.isEmpty()) {
return false;
}

ChunkPosition topmost = visited.last();
ChunkPosition topmost = Collections.max(wood, Comparator.comparingInt((ChunkPosition a) -> a.chunkPosY));

// check if there were enough leaves around the last position
// pos now contains the block above the topmost log
Expand Down