From 5ba1e67ea02d5cbc374989389390784efd697896 Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 26 Sep 2019 23:18:05 -0500 Subject: [PATCH] Don't print stack-traces to chat, just inform the user of the error --- src/api/java/baritone/api/utils/Helper.java | 3 + .../command/argparser/ArgParserManager.java | 4 +- .../exception/CommandUnhandledException.java | 56 +------------------ .../command/execution/CommandExecution.java | 1 - 4 files changed, 7 insertions(+), 57 deletions(-) diff --git a/src/api/java/baritone/api/utils/Helper.java b/src/api/java/baritone/api/utils/Helper.java index 7d962501..ae91957a 100755 --- a/src/api/java/baritone/api/utils/Helper.java +++ b/src/api/java/baritone/api/utils/Helper.java @@ -37,8 +37,11 @@ public interface Helper { Helper HELPER = new Helper() {}; static ITextComponent getPrefix() { + // Inner text component ITextComponent baritone = new TextComponentString(BaritoneAPI.getSettings().shortBaritonePrefix.value ? "B" : "Baritone"); baritone.getStyle().setColor(TextFormatting.LIGHT_PURPLE); + + // Outer brackets ITextComponent prefix = new TextComponentString(""); prefix.getStyle().setColor(TextFormatting.DARK_PURPLE); prefix.appendText("["); diff --git a/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java b/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java index 24f5fec0..b9b7bcc0 100644 --- a/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java +++ b/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java @@ -67,7 +67,7 @@ public class ArgParserManager { * @param type The type to try and parse the argument into. * @param arg The argument to parse. * @return An instance of the specified class. - * @throws CommandInvalidTypeException If the parsing failed + * @throws CommandInvalidTypeException If the parsing failed */ public static T parseStateless(Class type, CommandArgument arg) throws CommandInvalidTypeException { IArgParser.Stateless parser = getParserStateless(type); @@ -89,7 +89,7 @@ public class ArgParserManager { * @param arg The argument to parse. * @param state The state to pass to the {@link IArgParser.Stated}. * @return An instance of the specified class. - * @throws CommandInvalidTypeException If the parsing failed + * @throws CommandInvalidTypeException If the parsing failed * @see IArgParser.Stated */ public static T parseStated(Class type, Class stateKlass, CommandArgument arg, S state) throws CommandInvalidTypeException { diff --git a/src/api/java/baritone/api/utils/command/exception/CommandUnhandledException.java b/src/api/java/baritone/api/utils/command/exception/CommandUnhandledException.java index 08f39ecd..5967eeb6 100644 --- a/src/api/java/baritone/api/utils/command/exception/CommandUnhandledException.java +++ b/src/api/java/baritone/api/utils/command/exception/CommandUnhandledException.java @@ -17,62 +17,10 @@ package baritone.api.utils.command.exception; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - public class CommandUnhandledException extends RuntimeException implements ICommandException { public CommandUnhandledException(Throwable cause) { - super(String.format( - "An unhandled exception has occurred:\n\n%s", - getFriendlierStackTrace(cause) - )); - } - - private static String getStackTrace(Throwable throwable) { - StringWriter sw = new StringWriter(); - throwable.printStackTrace(new PrintWriter(sw)); - return sw.toString(); - } - - private static String getBaritoneStackTrace(String stackTrace) { - List lines = Stream.of(stackTrace.split("\n")) - .collect(Collectors.toList()); - int lastBaritoneLine = 0; - for (int i = 0; i < lines.size(); i++) { - if (lines.get(i).startsWith("\tat baritone.") && lines.get(i).contains("BaritoneChatControl")) { - lastBaritoneLine = i; - } - } - return String.join("\n", lines.subList(0, lastBaritoneLine + 1)); - } - - private static String getBaritoneStackTrace(Throwable throwable) { - return getBaritoneStackTrace(getStackTrace(throwable)); - } - - private static String getFriendlierStackTrace(String stackTrace) { - List lines = Arrays.asList(stackTrace.split("\n")); - for (int i = 0; i < lines.size(); i++) { - String line = lines.get(i); - if (line.startsWith("\tat ")) { - if (line.startsWith("\tat baritone.")) { - line = line.replaceFirst("^\tat [a-z.]+?([A-Z])", "\tat $1"); - } - // line = line.replaceFirst("\\(([^)]+)\\)$", "\n\t . $1"); - line = line.replaceFirst("\\([^:]+:(\\d+)\\)$", ":$1"); - line = line.replaceFirst("\\(Unknown Source\\)$", ""); - lines.set(i, line); - } - } - return String.join("\n", lines); - } - - private static String getFriendlierStackTrace(Throwable throwable) { - return getFriendlierStackTrace(getBaritoneStackTrace(throwable)); + super("An unhandled exception occurred. The error is in your game's log, please report this at https://github.com/cabaletta/baritone/issues"); + cause.printStackTrace(); } } diff --git a/src/api/java/baritone/api/utils/command/execution/CommandExecution.java b/src/api/java/baritone/api/utils/command/execution/CommandExecution.java index 8a23757d..eb69a609 100644 --- a/src/api/java/baritone/api/utils/command/execution/CommandExecution.java +++ b/src/api/java/baritone/api/utils/command/execution/CommandExecution.java @@ -71,7 +71,6 @@ public class CommandExecution { } catch (CommandException e) { e.handle(command, args.args); } catch (Throwable t) { - t.printStackTrace(); new CommandUnhandledException(t).handle(command, args.args); } }