From 842e50adb97abfe9b5783e4b9b12dae9bde1493a Mon Sep 17 00:00:00 2001 From: Leijurv Date: Wed, 7 Nov 2018 17:16:34 -0800 Subject: [PATCH] path cleanup --- .../pathing/calc/AbstractNodeCostSearch.java | 2 +- src/main/java/baritone/pathing/calc/Path.java | 31 +---------- .../baritone/pathing/path/CutoffPath.java | 3 +- .../baritone/pathing/path/SplicedPath.java | 3 +- .../java/baritone/utils/pathing/PathBase.java | 52 +++++++++++++++++++ 5 files changed, 59 insertions(+), 32 deletions(-) create mode 100644 src/main/java/baritone/utils/pathing/PathBase.java diff --git a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java index 981ff733..708b5ab0 100644 --- a/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java +++ b/src/main/java/baritone/pathing/calc/AbstractNodeCostSearch.java @@ -90,7 +90,7 @@ public abstract class AbstractNodeCostSearch implements IPathFinder { this.cancelRequested = false; try { Optional path = calculate0(timeout); - path.ifPresent(IPath::postProcess); + path = path.map(IPath::postProcess); isFinished = true; if (cancelRequested) { return new PathCalculationResult(PathCalculationResult.Type.CANCELLATION, path); diff --git a/src/main/java/baritone/pathing/calc/Path.java b/src/main/java/baritone/pathing/calc/Path.java index de13bd34..873fef64 100644 --- a/src/main/java/baritone/pathing/calc/Path.java +++ b/src/main/java/baritone/pathing/calc/Path.java @@ -17,7 +17,6 @@ package baritone.pathing.calc; -import baritone.api.BaritoneAPI; import baritone.api.pathing.calc.IPath; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; @@ -26,9 +25,7 @@ import baritone.pathing.movement.Movement; import baritone.pathing.movement.Moves; import baritone.pathing.path.CutoffPath; import baritone.utils.Helper; -import net.minecraft.client.Minecraft; -import net.minecraft.util.math.BlockPos; -import net.minecraft.world.chunk.EmptyChunk; +import baritone.utils.pathing.PathBase; import java.util.ArrayList; import java.util.Collections; @@ -40,7 +37,7 @@ import java.util.List; * * @author leijurv */ -class Path implements IPath { +class Path extends PathBase { /** * The start position of this path @@ -188,28 +185,4 @@ class Path implements IPath { public BetterBlockPos getDest() { return end; } - - @Override - public IPath cutoffAtLoadedChunks() { - for (int i = 0; i < positions().size(); i++) { - BlockPos pos = positions().get(i); - if (Minecraft.getMinecraft().world.getChunk(pos) instanceof EmptyChunk) { - return new CutoffPath(this, i); - } - } - return this; - } - - @Override - public IPath staticCutoff(Goal destination) { - if (length() < BaritoneAPI.getSettings().pathCutoffMinimumLength.get()) { - return this; - } - if (destination == null || destination.isInGoal(getDest())) { - return this; - } - double factor = BaritoneAPI.getSettings().pathCutoffFactor.get(); - int newLength = (int) (length() * factor); - return new CutoffPath(this, newLength); - } } diff --git a/src/main/java/baritone/pathing/path/CutoffPath.java b/src/main/java/baritone/pathing/path/CutoffPath.java index 9714d8ce..30b2d4e9 100644 --- a/src/main/java/baritone/pathing/path/CutoffPath.java +++ b/src/main/java/baritone/pathing/path/CutoffPath.java @@ -21,11 +21,12 @@ import baritone.api.pathing.calc.IPath; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; import baritone.api.utils.BetterBlockPos; +import baritone.utils.pathing.PathBase; import java.util.Collections; import java.util.List; -public class CutoffPath implements IPath { +public class CutoffPath extends PathBase { private final List path; diff --git a/src/main/java/baritone/pathing/path/SplicedPath.java b/src/main/java/baritone/pathing/path/SplicedPath.java index 33b0b97f..5048a84a 100644 --- a/src/main/java/baritone/pathing/path/SplicedPath.java +++ b/src/main/java/baritone/pathing/path/SplicedPath.java @@ -21,10 +21,11 @@ import baritone.api.pathing.calc.IPath; import baritone.api.pathing.goals.Goal; import baritone.api.pathing.movement.IMovement; import baritone.api.utils.BetterBlockPos; +import baritone.utils.pathing.PathBase; import java.util.*; -public class SplicedPath implements IPath { +public class SplicedPath extends PathBase { private final List path; private final List movements; diff --git a/src/main/java/baritone/utils/pathing/PathBase.java b/src/main/java/baritone/utils/pathing/PathBase.java new file mode 100644 index 00000000..57ee941d --- /dev/null +++ b/src/main/java/baritone/utils/pathing/PathBase.java @@ -0,0 +1,52 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.utils.pathing; + +import baritone.api.BaritoneAPI; +import baritone.api.pathing.calc.IPath; +import baritone.api.pathing.goals.Goal; +import baritone.pathing.path.CutoffPath; +import net.minecraft.client.Minecraft; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.chunk.EmptyChunk; + +public abstract class PathBase implements IPath { + @Override + public IPath cutoffAtLoadedChunks() { + for (int i = 0; i < positions().size(); i++) { + BlockPos pos = positions().get(i); + if (Minecraft.getMinecraft().world.getChunk(pos) instanceof EmptyChunk) { + return new CutoffPath(this, i); + } + } + return this; + } + + @Override + public IPath staticCutoff(Goal destination) { + if (length() < BaritoneAPI.getSettings().pathCutoffMinimumLength.get()) { + return this; + } + if (destination == null || destination.isInGoal(getDest())) { + return this; + } + double factor = BaritoneAPI.getSettings().pathCutoffFactor.get(); + int newLength = (int) ((length() - 1) * factor); + return new CutoffPath(this, newLength); + } +}