From b75a78f3d3623588e4c48c036c6afdbaba8d9cc0 Mon Sep 17 00:00:00 2001 From: Brady Date: Thu, 26 Sep 2019 23:33:37 -0500 Subject: [PATCH] Fix the scuff --- src/api/java/baritone/api/utils/Helper.java | 7 ++++-- .../command/argparser/ArgParserManager.java | 6 ++--- .../CommandNoParserForTypeException.java | 5 +--- .../exception/CommandUnhandledException.java | 24 +++++++++++++++++-- .../command/execution/CommandExecution.java | 10 +++++--- 5 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/api/java/baritone/api/utils/Helper.java b/src/api/java/baritone/api/utils/Helper.java index 376dcf7f..91d7890f 100755 --- a/src/api/java/baritone/api/utils/Helper.java +++ b/src/api/java/baritone/api/utils/Helper.java @@ -37,6 +37,11 @@ public interface Helper { */ Helper HELPER = new Helper() {}; + /** + * Instance of the game + */ + Minecraft mc = Minecraft.getMinecraft(); + static ITextComponent getPrefix() { // Inner text component ITextComponent baritone = new TextComponentString(BaritoneAPI.getSettings().shortBaritonePrefix.value ? "B" : "Baritone"); @@ -52,8 +57,6 @@ public interface Helper { return prefix; } - Minecraft mc = Minecraft.getMinecraft(); - /** * Send a message to chat only if chatDebug is on * 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 b9b7bcc0..1b0730c6 100644 --- a/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java +++ b/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java @@ -72,8 +72,7 @@ public class ArgParserManager { public static T parseStateless(Class type, CommandArgument arg) throws CommandInvalidTypeException { IArgParser.Stateless parser = getParserStateless(type); if (parser == null) { - // TODO: Fix this scuff lol - throw new CommandUnhandledException(new CommandNoParserForTypeException(type)); + throw new CommandNoParserForTypeException(type); } try { return parser.parseArg(arg); @@ -95,8 +94,7 @@ public class ArgParserManager { public static T parseStated(Class type, Class stateKlass, CommandArgument arg, S state) throws CommandInvalidTypeException { IArgParser.Stated parser = getParserStated(type, stateKlass); if (parser == null) { - // TODO: Fix this scuff lol - throw new CommandUnhandledException(new CommandNoParserForTypeException(type)); + throw new CommandNoParserForTypeException(type); } try { return parser.parseArg(arg, state); diff --git a/src/api/java/baritone/api/utils/command/exception/CommandNoParserForTypeException.java b/src/api/java/baritone/api/utils/command/exception/CommandNoParserForTypeException.java index 5a016cd4..f4f2d818 100644 --- a/src/api/java/baritone/api/utils/command/exception/CommandNoParserForTypeException.java +++ b/src/api/java/baritone/api/utils/command/exception/CommandNoParserForTypeException.java @@ -17,12 +17,9 @@ package baritone.api.utils.command.exception; -public class CommandNoParserForTypeException extends CommandErrorMessageException { - - public final Class klass; +public class CommandNoParserForTypeException extends CommandUnhandledException { public CommandNoParserForTypeException(Class klass) { super(String.format("Could not find a handler for type %s", klass.getSimpleName())); - this.klass = klass; } } 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 5967eeb6..6d647a20 100644 --- a/src/api/java/baritone/api/utils/command/exception/CommandUnhandledException.java +++ b/src/api/java/baritone/api/utils/command/exception/CommandUnhandledException.java @@ -17,10 +17,30 @@ package baritone.api.utils.command.exception; +import baritone.api.utils.command.Command; +import baritone.api.utils.command.argument.CommandArgument; +import net.minecraft.util.text.TextFormatting; + +import java.util.List; + +import static baritone.api.utils.Helper.HELPER; + public class CommandUnhandledException extends RuntimeException implements ICommandException { + public CommandUnhandledException(String message) { + super(message); + } + public CommandUnhandledException(Throwable cause) { - 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(); + super(cause); + } + + @Override + public void handle(Command 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(); } } 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 eb69a609..181c0e6b 100644 --- a/src/api/java/baritone/api/utils/command/execution/CommandExecution.java +++ b/src/api/java/baritone/api/utils/command/execution/CommandExecution.java @@ -21,6 +21,7 @@ import baritone.api.utils.command.Command; import baritone.api.utils.command.argument.CommandArgument; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandUnhandledException; +import baritone.api.utils.command.exception.ICommandException; import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.manager.ICommandManager; import com.mojang.realmsclient.util.Pair; @@ -68,10 +69,13 @@ public class CommandExecution { public void execute() { try { command.execute(this); - } catch (CommandException e) { - e.handle(command, args.args); } catch (Throwable t) { - new CommandUnhandledException(t).handle(command, args.args); + // Create a handleable exception, wrap if needed + ICommandException exception = t instanceof ICommandException + ? (ICommandException) t + : new CommandUnhandledException(t); + + exception.handle(command, args.args); } }