From 4c4bc8058b26add8fbc0c264a283077997a8ccbc Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 6 Jun 2019 04:15:43 -0500 Subject: [PATCH] Some clean ups --- .../java/baritone/pathing/calc/AStarPathFinder.java | 3 +-- src/main/java/baritone/pathing/calc/Path.java | 2 +- src/main/java/baritone/utils/pathing/Avoidance.java | 7 +++++-- .../baritone/utils/pathing/SegmentedCalculator.java | 12 ++++++------ .../baritone/pathing/calc/openset/OpenSetsTest.java | 10 +++++----- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main/java/baritone/pathing/calc/AStarPathFinder.java b/src/main/java/baritone/pathing/calc/AStarPathFinder.java index 57e9200d..fc964d0f 100644 --- a/src/main/java/baritone/pathing/calc/AStarPathFinder.java +++ b/src/main/java/baritone/pathing/calc/AStarPathFinder.java @@ -86,8 +86,7 @@ public final class AStarPathFinder extends AbstractNodeCostSearch { if (slowPath) { try { Thread.sleep(Baritone.settings().slowPathTimeDelayMS.value); - } catch (InterruptedException ex) { - } + } catch (InterruptedException ignored) {} } PathNode currentNode = openSet.removeLowest(); mostRecentConsidered = currentNode; diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index a3f9dd22..76cd8396 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -96,7 +96,7 @@ class Path extends PathBase { } PathNode current = end; LinkedList tempPath = new LinkedList<>(); - LinkedList tempNodes = new LinkedList(); + LinkedList tempNodes = new LinkedList<>(); // Repeatedly inserting to the beginning of an arraylist is O(n^2) // Instead, do it into a linked list, then convert at the end while (current != null) { diff --git a/src/main/java/baritone/utils/pathing/Avoidance.java b/src/main/java/baritone/utils/pathing/Avoidance.java index 9b32b1df..c4b12336 100644 --- a/src/main/java/baritone/utils/pathing/Avoidance.java +++ b/src/main/java/baritone/utils/pathing/Avoidance.java @@ -64,10 +64,13 @@ public class Avoidance { double mobSpawnerCoeff = Baritone.settings().mobSpawnerAvoidanceCoefficient.value; double mobCoeff = Baritone.settings().mobAvoidanceCoefficient.value; if (mobSpawnerCoeff != 1.0D) { - ctx.worldData().getCachedWorld().getLocationsOf("mob_spawner", 1, ctx.playerFeet().x, ctx.playerFeet().z, 2).forEach(mobspawner -> res.add(new Avoidance(mobspawner, mobSpawnerCoeff, Baritone.settings().mobSpawnerAvoidanceRadius.value))); + ctx.worldData().getCachedWorld().getLocationsOf("mob_spawner", 1, ctx.playerFeet().x, ctx.playerFeet().z, 2) + .forEach(mobspawner -> res.add(new Avoidance(mobspawner, mobSpawnerCoeff, Baritone.settings().mobSpawnerAvoidanceRadius.value))); } if (mobCoeff != 1.0D) { - ctx.world().loadedEntityList.stream().filter(entity -> entity instanceof EntityMob).forEach(entity -> res.add(new Avoidance(new BlockPos(entity), mobCoeff, Baritone.settings().mobAvoidanceRadius.value))); + ctx.world().loadedEntityList.stream() + .filter(entity -> entity instanceof EntityMob) + .forEach(entity -> res.add(new Avoidance(new BlockPos(entity), mobCoeff, Baritone.settings().mobAvoidanceRadius.value))); } return res; } diff --git a/src/main/java/baritone/utils/pathing/SegmentedCalculator.java b/src/main/java/baritone/utils/pathing/SegmentedCalculator.java index 757ea015..c1753a8b 100644 --- a/src/main/java/baritone/utils/pathing/SegmentedCalculator.java +++ b/src/main/java/baritone/utils/pathing/SegmentedCalculator.java @@ -51,7 +51,7 @@ public class SegmentedCalculator { private Optional doCalc() { Optional soFar = Optional.empty(); while (true) { - PathCalculationResult result = segment(soFar); + PathCalculationResult result = segment(soFar.orElse(null)); switch (result.getType()) { case SUCCESS_SEGMENT: case SUCCESS_TO_GOAL: @@ -62,8 +62,8 @@ public class SegmentedCalculator { default: // CANCELLATION and null should not be possible, nothing else has access to this, so it can't have been canceled throw new IllegalStateException(); } - IPath segment = result.getPath().get(); // path calculation result type is SUCCESS_SEGMENT, so the path must be present - IPath combined = soFar.map(previous -> (IPath) SplicedPath.trySplice(previous, segment, true).get()).orElse(segment); + IPath segment = result.getPath().orElseThrow(IllegalStateException::new); // path calculation result type is SUCCESS_SEGMENT, so the path must be present + IPath combined = soFar.map(previous -> (IPath) SplicedPath.trySplice(previous, segment, true).orElseThrow(IllegalStateException::new)).orElse(segment); loadAdjacent(combined.getDest().getX(), combined.getDest().getZ()); soFar = Optional.of(combined); if (result.getType() == PathCalculationResult.Type.SUCCESS_TO_GOAL) { @@ -85,9 +85,9 @@ public class SegmentedCalculator { } } - private PathCalculationResult segment(Optional previous) { - BetterBlockPos segmentStart = previous.map(IPath::getDest).orElse(start); // <-- e p i c - AbstractNodeCostSearch search = new AStarPathFinder(segmentStart.x, segmentStart.y, segmentStart.z, goal, new Favoring(previous.orElse(null), context), context); // this is on another thread, so cannot include mob avoidances. + private PathCalculationResult segment(IPath previous) { + BetterBlockPos segmentStart = previous != null ? previous.getDest() : start; + AbstractNodeCostSearch search = new AStarPathFinder(segmentStart.x, segmentStart.y, segmentStart.z, goal, new Favoring(previous, context), context); // this is on another thread, so cannot include mob avoidances. return search.calculate(Baritone.settings().primaryTimeoutMS.value, Baritone.settings().failureTimeoutMS.value); // use normal time settings, not the plan ahead settings, so as to not overwhelm the computer } diff --git a/src/test/java/baritone/pathing/calc/openset/OpenSetsTest.java b/src/test/java/baritone/pathing/calc/openset/OpenSetsTest.java index a12e0b7a..33a1f4dc 100644 --- a/src/test/java/baritone/pathing/calc/openset/OpenSetsTest.java +++ b/src/test/java/baritone/pathing/calc/openset/OpenSetsTest.java @@ -50,14 +50,14 @@ public class OpenSetsTest { return testSizes; } - private static void removeAndTest(int amount, IOpenSet[] test, Optional> mustContain) { + private static void removeAndTest(int amount, IOpenSet[] test, Collection mustContain) { double[][] results = new double[test.length][amount]; for (int i = 0; i < test.length; i++) { long before = System.nanoTime() / 1000000L; for (int j = 0; j < amount; j++) { PathNode pn = test[i].removeLowest(); - if (mustContain.isPresent() && !mustContain.get().contains(pn)) { - throw new IllegalStateException(mustContain.get() + " " + pn); + if (mustContain != null && !mustContain.contains(pn)) { + throw new IllegalStateException(mustContain + " " + pn); } results[i][j] = pn.combinedCost; } @@ -131,7 +131,7 @@ public class OpenSetsTest { System.out.println("Removal round 1"); // remove a quarter of the nodes and verify that they are indeed the size/4 lowest ones - removeAndTest(size / 4, test, Optional.of(lowestQuarter)); + removeAndTest(size / 4, test, lowestQuarter); // none of them should be empty (sanity check) for (IOpenSet set : test) { @@ -160,7 +160,7 @@ public class OpenSetsTest { System.out.println("Removal round 2"); // remove the remaining 3/4 - removeAndTest(size - size / 4, test, Optional.empty()); + removeAndTest(size - size / 4, test, null); // every set should now be empty for (IOpenSet set : test) {