From ce0e8b4cd1caf77dada7a69197992983c5bfabfe Mon Sep 17 00:00:00 2001 From: Brady Date: Sat, 17 Nov 2018 11:18:55 -0600 Subject: [PATCH] Clean up some bad Optional practices --- .../api/pathing/goals/GoalRunAway.java | 17 ++++++++--------- .../api/utils/PathCalculationResult.java | 18 +++++++++++++++--- .../baritone/behavior/PathingBehavior.java | 4 ++-- .../pathing/calc/AbstractNodeCostSearch.java | 11 +++++------ .../java/baritone/process/MineProcess.java | 2 +- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java index 44c5fa80..3f4a02de 100644 --- a/src/api/java/baritone/api/pathing/goals/GoalRunAway.java +++ b/src/api/java/baritone/api/pathing/goals/GoalRunAway.java @@ -20,7 +20,6 @@ package baritone.api.pathing.goals; import net.minecraft.util.math.BlockPos; import java.util.Arrays; -import java.util.Optional; /** * Useful for automated combat (retreating specifically) @@ -33,13 +32,13 @@ public class GoalRunAway implements Goal { private final double distanceSq; - private final Optional maintainY; + private final Integer maintainY; public GoalRunAway(double distance, BlockPos... from) { - this(distance, Optional.empty(), from); + this(distance, null, from); } - public GoalRunAway(double distance, Optional maintainY, BlockPos... from) { + public GoalRunAway(double distance, Integer maintainY, BlockPos... from) { if (from.length == 0) { throw new IllegalArgumentException(); } @@ -50,7 +49,7 @@ public class GoalRunAway implements Goal { @Override public boolean isInGoal(int x, int y, int z) { - if (maintainY.isPresent() && maintainY.get() != y) { + if (maintainY != null && maintainY != y) { return false; } for (BlockPos p : from) { @@ -74,16 +73,16 @@ public class GoalRunAway implements Goal { } } min = -min; - if (maintainY.isPresent()) { - min = min * 0.6 + GoalYLevel.calculate(maintainY.get(), y) * 1.5; + if (maintainY != null) { + min = min * 0.6 + GoalYLevel.calculate(maintainY, y) * 1.5; } return min; } @Override public String toString() { - if (maintainY.isPresent()) { - return "GoalRunAwayFromMaintainY y=" + maintainY.get() + ", " + Arrays.asList(from); + if (maintainY != null) { + return "GoalRunAwayFromMaintainY y=" + maintainY + ", " + Arrays.asList(from); } else { return "GoalRunAwayFrom" + Arrays.asList(from); } diff --git a/src/api/java/baritone/api/utils/PathCalculationResult.java b/src/api/java/baritone/api/utils/PathCalculationResult.java index 69d2a9b3..df009952 100644 --- a/src/api/java/baritone/api/utils/PathCalculationResult.java +++ b/src/api/java/baritone/api/utils/PathCalculationResult.java @@ -23,14 +23,26 @@ import java.util.Optional; public class PathCalculationResult { - public final Optional path; - public final Type type; + private final IPath path; + private final Type type; - public PathCalculationResult(Type type, Optional path) { + public PathCalculationResult(Type type) { + this(type, null); + } + + public PathCalculationResult(Type type, IPath path) { this.path = path; this.type = type; } + public final Optional getPath() { + return Optional.ofNullable(this.path); + } + + public final Type getType() { + return this.type; + } + public enum Type { SUCCESS_TO_GOAL, SUCCESS_SEGMENT, diff --git a/src/main/java/baritone/behavior/PathingBehavior.java b/src/main/java/baritone/behavior/PathingBehavior.java index 78cd0913..31f6721f 100644 --- a/src/main/java/baritone/behavior/PathingBehavior.java +++ b/src/main/java/baritone/behavior/PathingBehavior.java @@ -411,7 +411,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, } PathCalculationResult calcResult = pathfinder.calculate(timeout); - Optional path = calcResult.path; + Optional path = calcResult.getPath(); if (Baritone.settings().cutoffAtLoadBoundary.get()) { path = path.map(p -> { IPath result = p.cutoffAtLoadedChunks(context.world()); @@ -443,7 +443,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior, queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING); current = executor.get(); } else { - if (calcResult.type != PathCalculationResult.Type.CANCELLATION && calcResult.type != PathCalculationResult.Type.EXCEPTION) { + if (calcResult.getType() != PathCalculationResult.Type.CANCELLATION && calcResult.getType() != PathCalculationResult.Type.EXCEPTION) { // don't dispatch CALC_FAILED on cancellation queuePathEvent(PathEvent.CALC_FAILED); } diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index c2ce36e2..ac4efa4d 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -89,16 +89,15 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { } this.cancelRequested = false; try { - Optional path = calculate0(timeout); - path = path.map(IPath::postProcess); + IPath path = calculate0(timeout).map(IPath::postProcess).orElse(null); isFinished = true; if (cancelRequested) { return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION, path); } - if (!path.isPresent()) { - return new PathCalculationResult(PathCalculationResult.Type.FAILURE, path); + if (path == null) { + return new PathCalculationResult(PathCalculationResult.Type.FAILURE); } - if (goal.isInGoal(path.get().getDest())) { + if (goal.isInGoal(path.getDest())) { return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_TO_GOAL, path); } else { return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_SEGMENT, path); @@ -106,7 +105,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { } catch (Exception e) { Helper.HELPER.logDebug("Pathing exception: " + e); e.printStackTrace(); - return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION, Optional.empty()); + return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION); } finally { // this is run regardless of what exception may or may not be raised by calculate0 isFinished = true; diff --git a/src/main/java/baritone/process/MineProcess.java b/src/main/java/baritone/process/MineProcess.java index 60b36ea1..204e11c5 100644 --- a/src/main/java/baritone/process/MineProcess.java +++ b/src/main/java/baritone/process/MineProcess.java @@ -141,7 +141,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro // TODO shaft mode, mine 1x1 shafts to either side // TODO also, see if the GoalRunAway with maintain Y at 11 works even from the surface if (branchPointRunaway == null) { - branchPointRunaway = new GoalRunAway(1, Optional.of(y), branchPoint) { + branchPointRunaway = new GoalRunAway(1, y, branchPoint) { @Override public boolean isInGoal(int x, int y, int z) { return false;