Fixed exception when breaking trees (whilst not looking at one) Closes #33

This commit is contained in:
Sollace 2021-06-16 00:46:04 +02:00
parent 0a06dee124
commit c59848544a
2 changed files with 9 additions and 8 deletions

View file

@ -55,7 +55,9 @@ public final class TreeType {
return;
}
traverseInner(logs, leaves, w, findBase(w, start), recurseLevel, maxRecurse, logConsumer, leavesConsumer);
findBase(w, start).ifPresent(base -> {
traverseInner(logs, leaves, w, base, recurseLevel, maxRecurse, logConsumer, leavesConsumer);
});
}
private void traverseInner(Set<BlockPos> logs, Set<BlockPos> leaves, World w, BlockPos pos, int recurseLevel, int maxRecurse, Reactor logConsumer, Reactor leavesConsumer) {
@ -89,8 +91,8 @@ public final class TreeType {
/**
* Recursively locates the base of the tree.
*/
public BlockPos findBase(World w, BlockPos pos) {
return findBase(new HashSet<BlockPos>(), w, new BlockPos.Mutable(pos.getX(), pos.getY(), pos.getZ())).get();
public Optional<BlockPos> findBase(World w, BlockPos pos) {
return findBase(new HashSet<BlockPos>(), w, new BlockPos.Mutable(pos.getX(), pos.getY(), pos.getZ()));
}
private Optional<BlockPos> findBase(Set<BlockPos> done, World w, BlockPos.Mutable pos) {
@ -124,7 +126,7 @@ public final class TreeType {
Set<BlockPos> logs = new HashSet<>();
Set<BlockPos> leaves = new HashSet<>();
traverseInner(logs, leaves, w, findBase(w, pos), 0, 50, null, null);
findBase(w, pos).ifPresent(base -> traverseInner(logs, leaves, w, base, 0, 50, null, null));
int logCount = logs.size();

View file

@ -67,10 +67,9 @@ public class EarthPonyKickAbility implements Ability<Pos> {
TreeType tree = TreeType.get(player.getWorld().getBlockState(pos));
if (tree != TreeType.NONE) {
pos = tree.findBase(player.getWorld(), pos);
if (tree.countBlocks(player.getWorld(), pos) > 0) {
return new Pos(pos);
}
return tree.findBase(player.getWorld(), pos).map(base -> {
return tree.countBlocks(player.getWorld(), pos) > 0 ? new Pos(base) : null;
}).orElse(null);
}
}