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 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<Integer> 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<Integer> 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);
}

View File

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

View File

@ -411,7 +411,7 @@ public final class PathingBehavior extends Behavior implements IPathingBehavior,
}
PathCalculationResult calcResult = pathfinder.calculate(timeout);
Optional<IPath> path = calcResult.path;
Optional<IPath> 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);
}

View File

@ -89,16 +89,15 @@ public abstract class AbstractNodeCostSearch implements IPathFinder {
}
this.cancelRequested = false;
try {
Optional<IPath> 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;

View File

@ -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;