From 4fe9c180d5e0a063d625a8496dab470607c4b299 Mon Sep 17 00:00:00 2001 From: babbaj Date: Thu, 14 Feb 2019 07:03:22 -0500 Subject: [PATCH] Fix potential problems --- .../java/baritone/process/GetToBlockProcess.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/baritone/process/GetToBlockProcess.java b/src/main/java/baritone/process/GetToBlockProcess.java index 6a0a44f7..89f60da3 100644 --- a/src/main/java/baritone/process/GetToBlockProcess.java +++ b/src/main/java/baritone/process/GetToBlockProcess.java @@ -33,7 +33,6 @@ import net.minecraft.inventory.ContainerPlayer; import net.minecraft.util.math.BlockPos; import java.util.*; -import java.util.stream.Collectors; public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBlockProcess { @@ -117,13 +116,15 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl return new PathingCommand(goal, PathingCommandType.REVALIDATE_GOAL_AND_PATH); } + // blacklist the closest block and its adjacent blocks public synchronized void blacklistClosest() { - List newBlacklist = knownLocations.stream().sorted(Comparator.comparingDouble(ctx.player()::getDistanceSq)).collect(Collectors.toList()).subList(0, 1); + List newBlacklist = new ArrayList<>(); + knownLocations.stream().min(Comparator.comparingDouble(ctx.player()::getDistanceSq)).ifPresent(newBlacklist::add); outer: while (true) { for (BlockPos known : knownLocations) { for (BlockPos blacklist : newBlacklist) { - if (known.distanceSq(blacklist) == 1) { // directly adjacent + if (areAdjacent(known, blacklist)) { // directly adjacent newBlacklist.add(known); knownLocations.remove(known); continue outer; @@ -138,6 +139,14 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl blacklist.addAll(newBlacklist); } + // safer than direct double comparison from distanceSq + private boolean areAdjacent(BlockPos posA, BlockPos posB) { + int diffX = Math.abs(posA.getX() - posB.getX()); + int diffY = Math.abs(posA.getY() - posB.getY()); + int diffZ = Math.abs(posA.getZ() - posB.getZ()); + return (diffX + diffY + diffZ) == 1; + } + @Override public synchronized void onLostControl() { gettingTo = null;