Clean up some bad Optional practices

This commit is contained in:
Brady 2018-11-17 11:18:55 -06:00
parent 737c632227
commit ce0e8b4cd1
No known key found for this signature in database
GPG Key ID: 73A788379A197567
5 changed files with 31 additions and 21 deletions

View File

@ -20,7 +20,6 @@ package baritone.api.pathing.goals;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional;
/** /**
* Useful for automated combat (retreating specifically) * Useful for automated combat (retreating specifically)
@ -33,13 +32,13 @@ public class GoalRunAway implements Goal {
private final double distanceSq; private final double distanceSq;
private final Optional<Integer> maintainY; private final Integer maintainY;
public GoalRunAway(double distance, BlockPos... from) { public GoalRunAway(double distance, BlockPos... from) {
this(distance, Optional.empty(), from); this(distance, null, from);
} }
public GoalRunAway(double distance, Optional<Integer> maintainY, BlockPos... from) { public GoalRunAway(double distance, Integer maintainY, BlockPos... from) {
if (from.length == 0) { if (from.length == 0) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
@ -50,7 +49,7 @@ public class GoalRunAway implements Goal {
@Override @Override
public boolean isInGoal(int x, int y, int z) { public boolean isInGoal(int x, int y, int z) {
if (maintainY.isPresent() && maintainY.get() != y) { if (maintainY != null && maintainY != y) {
return false; return false;
} }
for (BlockPos p : from) { for (BlockPos p : from) {
@ -74,16 +73,16 @@ public class GoalRunAway implements Goal {
} }
} }
min = -min; min = -min;
if (maintainY.isPresent()) { if (maintainY != null) {
min = min * 0.6 + GoalYLevel.calculate(maintainY.get(), y) * 1.5; min = min * 0.6 + GoalYLevel.calculate(maintainY, y) * 1.5;
} }
return min; return min;
} }
@Override @Override
public String toString() { public String toString() {
if (maintainY.isPresent()) { if (maintainY != null) {
return "GoalRunAwayFromMaintainY y=" + maintainY.get() + ", " + Arrays.asList(from); return "GoalRunAwayFromMaintainY y=" + maintainY + ", " + Arrays.asList(from);
} else { } else {
return "GoalRunAwayFrom" + Arrays.asList(from); return "GoalRunAwayFrom" + Arrays.asList(from);
} }

View File

@ -23,14 +23,26 @@ import java.util.Optional;
public class PathCalculationResult { public class PathCalculationResult {
public final Optional<IPath> path; private final IPath path;
public final Type type; private final Type type;
public PathCalculationResult(Type type, Optional<IPath> path) { public PathCalculationResult(Type type) {
this(type, null);
}
public PathCalculationResult(Type type, IPath path) {
this.path = path; this.path = path;
this.type = type; this.type = type;
} }
public final Optional<IPath> getPath() {
return Optional.ofNullable(this.path);
}
public final Type getType() {
return this.type;
}
public enum Type { public enum Type {
SUCCESS_TO_GOAL, SUCCESS_TO_GOAL,
SUCCESS_SEGMENT, SUCCESS_SEGMENT,

View File

@ -411,7 +411,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
} }
PathCalculationResult calcResult = pathfinder.calculate(timeout); PathCalculationResult calcResult = pathfinder.calculate(timeout);
Optional<IPath> path = calcResult.path; Optional<IPath> path = calcResult.getPath();
if (Baritone.settings().cutoffAtLoadBoundary.get()) { if (Baritone.settings().cutoffAtLoadBoundary.get()) {
path = path.map(p -> { path = path.map(p -> {
IPath result = p.cutoffAtLoadedChunks(context.world()); IPath result = p.cutoffAtLoadedChunks(context.world());
@ -443,7 +443,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING); queuePathEvent(PathEvent.CALC_FINISHED_NOW_EXECUTING);
current = executor.get(); current = executor.get();
} else { } 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 // don't dispatch CALC_FAILED on cancellation
queuePathEvent(PathEvent.CALC_FAILED); queuePathEvent(PathEvent.CALC_FAILED);
} }

View File

@ -89,16 +89,15 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
} }
this.cancelRequested = false; this.cancelRequested = false;
try { try {
Optional<IPath> path = calculate0(timeout); IPath path = calculate0(timeout).map(IPath::postProcess).orElse(null);
path = path.map(IPath::postProcess);
isFinished = true; isFinished = true;
if (cancelRequested) { if (cancelRequested) {
return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION, path); return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION, path);
} }
if (!path.isPresent()) { if (path == null) {
return new PathCalculationResult(PathCalculationResult.Type.FAILURE, path); 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); return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_TO_GOAL, path);
} else { } else {
return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_SEGMENT, path); return new PathCalculationResult(PathCalculationResult.Type.SUCCESS_SEGMENT, path);
@ -106,7 +105,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
} catch (Exception e) { } catch (Exception e) {
Helper.HELPER.logDebug("Pathing exception: " + e); Helper.HELPER.logDebug("Pathing exception: " + e);
e.printStackTrace(); e.printStackTrace();
return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION, Optional.empty()); return new PathCalculationResult(PathCalculationResult.Type.EXCEPTION);
} finally { } finally {
// this is run regardless of what exception may or may not be raised by calculate0 // this is run regardless of what exception may or may not be raised by calculate0
isFinished = true; isFinished = true;

View File

@ -141,7 +141,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
// TODO shaft mode, mine 1x1 shafts to either side // 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 // TODO also, see if the GoalRunAway with maintain Y at 11 works even from the surface
if (branchPointRunaway == null) { if (branchPointRunaway == null) {
branchPointRunaway = new GoalRunAway(1, Optional.of(y), branchPoint) { branchPointRunaway = new GoalRunAway(1, y, branchPoint) {
@Override @Override
public boolean isInGoal(int x, int y, int z) { public boolean isInGoal(int x, int y, int z) {
return false; return false;