From 877fd25608202596336af3ead8d19d2349c47394 Mon Sep 17 00:00:00 2001 From: Brady Date: Wed, 21 Jun 2023 00:52:43 -0500 Subject: [PATCH] Distinguish unexpected exceptions in path calc --- .../exception/CommandUnhandledException.java | 7 +---- src/api/java/baritone/api/utils/Helper.java | 7 +++++ .../baritone/behavior/ElytraBehavior.java | 19 ++++++++++--- .../elytra/NetherPathfinderContext.java | 21 ++++++++------ .../elytra/PathCalculationException.java | 28 +++++++++++++++++++ 5 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 src/main/java/baritone/behavior/elytra/PathCalculationException.java diff --git a/src/api/java/baritone/api/command/exception/CommandUnhandledException.java b/src/api/java/baritone/api/command/exception/CommandUnhandledException.java index 5dce59313..a1f826262 100644 --- a/src/api/java/baritone/api/command/exception/CommandUnhandledException.java +++ b/src/api/java/baritone/api/command/exception/CommandUnhandledException.java @@ -19,7 +19,6 @@ package baritone.api.command.exception; import baritone.api.command.ICommand; import baritone.api.command.argument.ICommandArgument; -import net.minecraft.util.text.TextFormatting; import java.util.List; @@ -37,10 +36,6 @@ public class CommandUnhandledException extends RuntimeException implements IComm @Override public void handle(ICommand command, List args) { - HELPER.logDirect("An unhandled exception occurred. " + - "The error is in your game's log, please report this at https://github.com/cabaletta/baritone/issues", - TextFormatting.RED); - - this.printStackTrace(); + HELPER.logUnhandledException(this); } } diff --git a/src/api/java/baritone/api/utils/Helper.java b/src/api/java/baritone/api/utils/Helper.java index b99074ae0..d604e0006 100755 --- a/src/api/java/baritone/api/utils/Helper.java +++ b/src/api/java/baritone/api/utils/Helper.java @@ -230,4 +230,11 @@ public interface Helper { default void logDirect(String message) { logDirect(message, BaritoneAPI.getSettings().logAsToast.value); } + + default void logUnhandledException(final Throwable exception) { + HELPER.logDirect("An unhandled exception occurred. " + + "The error is in your game's log, please report this at https://github.com/cabaletta/baritone/issues", + TextFormatting.RED); + exception.printStackTrace(); + } } diff --git a/src/main/java/baritone/behavior/ElytraBehavior.java b/src/main/java/baritone/behavior/ElytraBehavior.java index 14a15706e..193c95227 100644 --- a/src/main/java/baritone/behavior/ElytraBehavior.java +++ b/src/main/java/baritone/behavior/ElytraBehavior.java @@ -23,6 +23,7 @@ import baritone.api.event.events.*; import baritone.api.utils.*; import baritone.behavior.elytra.NetherPathfinderContext; import baritone.behavior.elytra.NetherPath; +import baritone.behavior.elytra.PathCalculationException; import baritone.behavior.elytra.UnpackedSegment; import baritone.utils.BlockStateInterface; import baritone.utils.accessor.IEntityFireworkRocket; @@ -108,7 +109,7 @@ public final class ElytraBehavior extends Behavior implements IElytraBehavior, H this.attemptNextSegment(); } - public void pathToDestination(BlockPos destination) { + public void pathToDestination(final BlockPos destination) { this.destination = destination; final long start = System.nanoTime(); this.path0(ctx.playerFeet(), destination, UnaryOperator.identity()) @@ -123,7 +124,11 @@ public final class ElytraBehavior extends Behavior implements IElytraBehavior, H .whenComplete((result, ex) -> { this.recalculating = false; if (ex != null) { - logDirect("Failed to compute path to destination"); + if (ex instanceof PathCalculationException) { + logDirect("Failed to compute path to destination"); + } else { + logUnhandledException(ex); + } } }); } @@ -141,7 +146,11 @@ public final class ElytraBehavior extends Behavior implements IElytraBehavior, H .whenComplete((result, ex) -> { this.recalculating = false; if (ex != null) { - logDirect("Failed to recompute segment"); + if (ex instanceof PathCalculationException) { + logDirect("Failed to recompute segment"); + } else { + logUnhandledException(ex); + } } }); } @@ -168,8 +177,10 @@ public final class ElytraBehavior extends Behavior implements IElytraBehavior, H }) .whenComplete((result, ex) -> { this.recalculating = false; - if (ex != null) { + if (ex instanceof PathCalculationException) { logDirect("Failed to compute next segment"); + } else { + logUnhandledException(ex); } }); } diff --git a/src/main/java/baritone/behavior/elytra/NetherPathfinderContext.java b/src/main/java/baritone/behavior/elytra/NetherPathfinderContext.java index da3753bf3..d82372a99 100644 --- a/src/main/java/baritone/behavior/elytra/NetherPathfinderContext.java +++ b/src/main/java/baritone/behavior/elytra/NetherPathfinderContext.java @@ -52,14 +52,19 @@ public final class NetherPathfinderContext { } public CompletableFuture pathFindAsync(final BlockPos src, final BlockPos dst) { - return CompletableFuture.supplyAsync(() -> - NetherPathfinder.pathFind( - this.context, - src.getX(), src.getY(), src.getZ(), - dst.getX(), dst.getY(), dst.getZ(), - true, - 10000 - ), this.executor); + return CompletableFuture.supplyAsync(() -> { + final PathSegment segment = NetherPathfinder.pathFind( + this.context, + src.getX(), src.getY(), src.getZ(), + dst.getX(), dst.getY(), dst.getZ(), + true, + 10000 + ); + if (segment == null) { + throw new PathCalculationException("Path calculation failed"); + } + return segment; + }, this.executor); } /** diff --git a/src/main/java/baritone/behavior/elytra/PathCalculationException.java b/src/main/java/baritone/behavior/elytra/PathCalculationException.java new file mode 100644 index 000000000..73ce82faf --- /dev/null +++ b/src/main/java/baritone/behavior/elytra/PathCalculationException.java @@ -0,0 +1,28 @@ +/* + * 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.behavior.elytra; + +/** + * @author Brady + */ +public final class PathCalculationException extends RuntimeException { + + public PathCalculationException(final String message) { + super(message); + } +}