Merge pull request #336 from babbaj/master

Fix potential problems
This commit is contained in:
Leijurv 2019-02-14 14:48:40 -08:00
commit 38127011fb
No known key found for this signature in database
GPG Key ID: 44A3EA646EADAC6A

View File

@ -33,7 +33,6 @@ import net.minecraft.inventory.ContainerPlayer;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBlockProcess { 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); return new PathingCommand(goal, PathingCommandType.REVALIDATE_GOAL_AND_PATH);
} }
// blacklist the closest block and its adjacent blocks
public synchronized void blacklistClosest() { public synchronized void blacklistClosest() {
List<BlockPos> newBlacklist = knownLocations.stream().sorted(Comparator.comparingDouble(ctx.player()::getDistanceSq)).collect(Collectors.toList()).subList(0, 1); List<BlockPos> newBlacklist = new ArrayList<>();
knownLocations.stream().min(Comparator.comparingDouble(ctx.player()::getDistanceSq)).ifPresent(newBlacklist::add);
outer: outer:
while (true) { while (true) {
for (BlockPos known : knownLocations) { for (BlockPos known : knownLocations) {
for (BlockPos blacklist : newBlacklist) { for (BlockPos blacklist : newBlacklist) {
if (known.distanceSq(blacklist) == 1) { // directly adjacent if (areAdjacent(known, blacklist)) { // directly adjacent
newBlacklist.add(known); newBlacklist.add(known);
knownLocations.remove(known); knownLocations.remove(known);
continue outer; continue outer;
@ -138,6 +139,14 @@ public class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBl
blacklist.addAll(newBlacklist); 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 @Override
public synchronized void onLostControl() { public synchronized void onLostControl() {
gettingTo = null; gettingTo = null;