From 3a3d880d814f70cee2fb0eb8aa7c4853502f9e7c Mon Sep 17 00:00:00 2001 From: Brady Date: Fri, 4 Oct 2019 08:55:02 -0500 Subject: [PATCH] ArgConsumer API separation --- .../baritone/api/utils/command/Command.java | 6 +- .../command/argparser/ArgParserManager.java | 7 +- .../command/argparser/DefaultArgParsers.java | 22 +- .../utils/command/argparser/IArgParser.java | 10 +- .../command/argument/CommandArgument.java | 170 ------- .../command/argument/ICommandArgument.java | 102 ++++ .../utils/command/datatypes/IDatatype.java | 6 +- .../command/datatypes/IDatatypeContext.java | 8 +- .../command/datatypes/RelativeBlockPos.java | 6 +- .../command/datatypes/RelativeCoordinate.java | 4 +- .../utils/command/datatypes/RelativeFile.java | 4 +- .../utils/command/datatypes/RelativeGoal.java | 6 +- .../command/datatypes/RelativeGoalBlock.java | 6 +- .../command/datatypes/RelativeGoalXZ.java | 6 +- .../command/datatypes/RelativeGoalYLevel.java | 4 +- .../CommandInvalidArgumentException.java | 8 +- .../CommandInvalidTypeException.java | 10 +- .../exception/CommandNotFoundException.java | 4 +- .../exception/CommandUnhandledException.java | 4 +- .../command/exception/ICommandException.java | 4 +- .../{ArgConsumer.java => IArgConsumer.java} | 425 ++++------------- .../command/helpers/pagination/Paginator.java | 28 +- .../tabcomplete/TabCompleteHelper.java | 5 +- .../command/manager/ICommandManager.java | 6 +- .../utils/command/BaritoneChatControl.java | 11 +- .../command/argument/CommandArgument.java | 97 ++++ .../command/argument/CommandArguments.java | 79 ++++ .../utils/command/defaults/AxisCommand.java | 6 +- .../command/defaults/BlacklistCommand.java | 6 +- .../utils/command/defaults/BuildCommand.java | 6 +- .../utils/command/defaults/CancelCommand.java | 6 +- .../utils/command/defaults/ChestsCommand.java | 6 +- .../utils/command/defaults/ClickCommand.java | 6 +- .../utils/command/defaults/ComeCommand.java | 6 +- .../utils/command/defaults/CommandAlias.java | 6 +- .../command/defaults/ExploreCommand.java | 6 +- .../defaults/ExploreFilterCommand.java | 6 +- .../utils/command/defaults/FarmCommand.java | 6 +- .../utils/command/defaults/FindCommand.java | 6 +- .../utils/command/defaults/FollowCommand.java | 6 +- .../command/defaults/ForceCancelCommand.java | 6 +- .../utils/command/defaults/GcCommand.java | 6 +- .../utils/command/defaults/GoalCommand.java | 6 +- .../utils/command/defaults/HelpCommand.java | 6 +- .../utils/command/defaults/InvertCommand.java | 6 +- .../utils/command/defaults/MineCommand.java | 6 +- .../utils/command/defaults/PathCommand.java | 6 +- .../command/defaults/PauseResumeCommands.java | 14 +- .../utils/command/defaults/ProcCommand.java | 6 +- .../command/defaults/ReloadAllCommand.java | 6 +- .../utils/command/defaults/RenderCommand.java | 6 +- .../utils/command/defaults/RepackCommand.java | 6 +- .../command/defaults/SaveAllCommand.java | 6 +- .../command/defaults/SchematicaCommand.java | 6 +- .../utils/command/defaults/SelCommand.java | 6 +- .../utils/command/defaults/SetCommand.java | 6 +- .../command/defaults/ThisWayCommand.java | 6 +- .../utils/command/defaults/TunnelCommand.java | 6 +- .../command/defaults/VersionCommand.java | 6 +- .../command/defaults/WaypointsCommand.java | 6 +- .../helpers/arguments/ArgConsumer.java | 444 ++++++++++++++++++ .../utils/command/manager/CommandManager.java | 23 +- 62 files changed, 1025 insertions(+), 696 deletions(-) delete mode 100644 src/api/java/baritone/api/utils/command/argument/CommandArgument.java create mode 100644 src/api/java/baritone/api/utils/command/argument/ICommandArgument.java rename src/api/java/baritone/api/utils/command/helpers/arguments/{ArgConsumer.java => IArgConsumer.java} (58%) create mode 100644 src/main/java/baritone/utils/command/argument/CommandArgument.java create mode 100644 src/main/java/baritone/utils/command/argument/CommandArguments.java create mode 100644 src/main/java/baritone/utils/command/helpers/arguments/ArgConsumer.java diff --git a/src/api/java/baritone/api/utils/command/Command.java b/src/api/java/baritone/api/utils/command/Command.java index 280c5f03..ba4ed938 100644 --- a/src/api/java/baritone/api/utils/command/Command.java +++ b/src/api/java/baritone/api/utils/command/Command.java @@ -21,7 +21,7 @@ import baritone.api.IBaritone; import baritone.api.utils.Helper; import baritone.api.utils.IPlayerContext; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Collections; import java.util.List; @@ -59,13 +59,13 @@ public abstract class Command implements Helper { /** * Called when this command is executed. */ - public abstract void execute(String label, ArgConsumer args) throws CommandException; + public abstract void execute(String label, IArgConsumer args) throws CommandException; /** * Called when the command needs to tab complete. Return a Stream representing the entries to put in the completions * list. */ - public abstract Stream tabComplete(String label, ArgConsumer args) throws CommandException; + public abstract Stream tabComplete(String label, IArgConsumer args) throws CommandException; /** * @return A single-line string containing a short description of this command's purpose. 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 1b0730c6..2b864f42 100644 --- a/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java +++ b/src/api/java/baritone/api/utils/command/argparser/ArgParserManager.java @@ -17,10 +17,9 @@ package baritone.api.utils.command.argparser; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import baritone.api.utils.command.exception.CommandInvalidTypeException; import baritone.api.utils.command.exception.CommandNoParserForTypeException; -import baritone.api.utils.command.exception.CommandUnhandledException; import baritone.api.utils.command.registry.Registry; public class ArgParserManager { @@ -69,7 +68,7 @@ public class ArgParserManager { * @return An instance of the specified class. * @throws CommandInvalidTypeException If the parsing failed */ - public static T parseStateless(Class type, CommandArgument arg) throws CommandInvalidTypeException { + public static T parseStateless(Class type, ICommandArgument arg) throws CommandInvalidTypeException { IArgParser.Stateless parser = getParserStateless(type); if (parser == null) { throw new CommandNoParserForTypeException(type); @@ -91,7 +90,7 @@ public class ArgParserManager { * @throws CommandInvalidTypeException If the parsing failed * @see IArgParser.Stated */ - public static T parseStated(Class type, Class stateKlass, CommandArgument arg, S state) throws CommandInvalidTypeException { + public static T parseStated(Class type, Class stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException { IArgParser.Stated parser = getParserStated(type, stateKlass); if (parser == null) { throw new CommandNoParserForTypeException(type); diff --git a/src/api/java/baritone/api/utils/command/argparser/DefaultArgParsers.java b/src/api/java/baritone/api/utils/command/argparser/DefaultArgParsers.java index c33f61fb..b734f47d 100644 --- a/src/api/java/baritone/api/utils/command/argparser/DefaultArgParsers.java +++ b/src/api/java/baritone/api/utils/command/argparser/DefaultArgParsers.java @@ -17,7 +17,7 @@ package baritone.api.utils.command.argparser; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import java.util.Arrays; import java.util.List; @@ -34,8 +34,8 @@ public class DefaultArgParsers { } @Override - public Integer parseArg(CommandArgument arg) throws RuntimeException { - return Integer.parseInt(arg.value); + public Integer parseArg(ICommandArgument arg) throws RuntimeException { + return Integer.parseInt(arg.getValue()); } } @@ -48,8 +48,8 @@ public class DefaultArgParsers { } @Override - public Long parseArg(CommandArgument arg) throws RuntimeException { - return Long.parseLong(arg.value); + public Long parseArg(ICommandArgument arg) throws RuntimeException { + return Long.parseLong(arg.getValue()); } } @@ -62,8 +62,8 @@ public class DefaultArgParsers { } @Override - public Float parseArg(CommandArgument arg) throws RuntimeException { - String value = arg.value; + public Float parseArg(ICommandArgument arg) throws RuntimeException { + String value = arg.getValue(); if (!value.matches("^([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)|)$")) { throw new IllegalArgumentException("failed float format check"); } @@ -80,8 +80,8 @@ public class DefaultArgParsers { } @Override - public Double parseArg(CommandArgument arg) throws RuntimeException { - String value = arg.value; + public Double parseArg(ICommandArgument arg) throws RuntimeException { + String value = arg.getValue(); if (!value.matches("^([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)|)$")) { throw new IllegalArgumentException("failed double format check"); } @@ -101,8 +101,8 @@ public class DefaultArgParsers { } @Override - public Boolean parseArg(CommandArgument arg) throws RuntimeException { - String value = arg.value; + public Boolean parseArg(ICommandArgument arg) throws RuntimeException { + String value = arg.getValue(); if (TRUTHY_VALUES.contains(value.toLowerCase(Locale.US))) { return true; } else if (FALSY_VALUES.contains(value.toLowerCase(Locale.US))) { diff --git a/src/api/java/baritone/api/utils/command/argparser/IArgParser.java b/src/api/java/baritone/api/utils/command/argparser/IArgParser.java index 09e2aa2e..c1f615f6 100644 --- a/src/api/java/baritone/api/utils/command/argparser/IArgParser.java +++ b/src/api/java/baritone/api/utils/command/argparser/IArgParser.java @@ -17,7 +17,7 @@ package baritone.api.utils.command.argparser; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; public interface IArgParser { @@ -27,7 +27,7 @@ public interface IArgParser { Class getTarget(); /** - * A stateless argument parser is just that. It takes a {@link CommandArgument} and outputs its type. + * A stateless argument parser is just that. It takes a {@link ICommandArgument} and outputs its type. * * @see ArgParserManager#REGISTRY */ @@ -39,11 +39,11 @@ public interface IArgParser { * @throws RuntimeException if you want the parsing to fail. The exception will be caught and turned into an * appropriate error. */ - T parseArg(CommandArgument arg) throws Exception; + T parseArg(ICommandArgument arg) throws Exception; } /** - * A stated argument parser is similar to a stateless one. It also takes a {@link CommandArgument}, but it also + * A stated argument parser is similar to a stateless one. It also takes a {@link ICommandArgument}, but it also * takes a second argument that can be any type, referred to as the state. * * @see ArgParserManager#REGISTRY @@ -59,6 +59,6 @@ public interface IArgParser { * @throws RuntimeException if you want the parsing to fail. The exception will be caught and turned into an * appropriate error. */ - T parseArg(CommandArgument arg, S state) throws Exception; + T parseArg(ICommandArgument arg, S state) throws Exception; } } diff --git a/src/api/java/baritone/api/utils/command/argument/CommandArgument.java b/src/api/java/baritone/api/utils/command/argument/CommandArgument.java deleted file mode 100644 index a5b044c4..00000000 --- a/src/api/java/baritone/api/utils/command/argument/CommandArgument.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * 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.api.utils.command.argument; - -import baritone.api.utils.command.argparser.ArgParserManager; -import baritone.api.utils.command.argparser.IArgParser; -import baritone.api.utils.command.exception.CommandInvalidArgumentException; -import baritone.api.utils.command.exception.CommandInvalidTypeException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; -import net.minecraft.util.EnumFacing; - -import java.util.ArrayList; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import java.util.stream.Stream; - -/** - * A {@link CommandArgument} is an immutable object representing one command argument. It contains data on the index of - * that argument, its value, and the rest of the string that argument was found in - *

- * You're recommended to use {@link ArgConsumer}s to handle these. - */ -public class CommandArgument { - - public final int index; - public final String value; - public final String rawRest; - public final static Pattern argPattern = Pattern.compile("\\S+"); - - private CommandArgument(int index, String value, String rawRest) { - this.index = index; - this.value = value; - this.rawRest = rawRest; - } - - /** - * Gets an enum value from the enum class with the same name as this argument's value - *

- * For example if you getEnum as an {@link EnumFacing}, and this argument's value is "up", it will return {@link - * EnumFacing#UP} - * - * @param enumClass The enum class to search - * @return An enum constant of that class with the same name as this argument's value - * @throws CommandInvalidTypeException If the constant couldn't be found - * @see ArgConsumer#peekEnum(Class) - * @see ArgConsumer#peekEnum(Class, int) - * @see ArgConsumer#peekEnumOrNull(Class) - * @see ArgConsumer#peekEnumOrNull(Class, int) - * @see ArgConsumer#getEnum(Class) - * @see ArgConsumer#getEnumOrNull(Class) - */ - public > E getEnum(Class enumClass) throws CommandInvalidTypeException { - return Stream.of(enumClass.getEnumConstants()) - .filter(e -> e.name().equalsIgnoreCase(value)) - .findFirst() - .orElseThrow(() -> new CommandInvalidTypeException(this, enumClass.getSimpleName())); - } - - /** - * Tries to use a stateless {@link IArgParser} to parse this argument into the specified class - * - * @param type The class to parse this argument into - * @return An instance of the specified type - * @throws CommandInvalidTypeException If the parsing failed - */ - public T getAs(Class type) throws CommandInvalidTypeException { - return ArgParserManager.parseStateless(type, this); - } - - /** - * Tries to use a stateless {@link IArgParser} to parse this argument into the specified class - * - * @param type The class to parse this argument into - * @return If the parser succeeded - */ - public boolean is(Class type) { - try { - getAs(type); - return true; - } catch (Throwable t) { - return false; - } - } - - /** - * Tries to use a stated {@link IArgParser} to parse this argument into the specified class - * - * @param type The class to parse this argument into - * @return An instance of the specified type - * @throws CommandInvalidTypeException If the parsing failed - */ - @SuppressWarnings("UnusedReturnValue") - public T getAs(Class type, Class stateType, S state) throws CommandInvalidTypeException { - return ArgParserManager.parseStated(type, stateType, this, state); - } - - /** - * Tries to use a stated {@link IArgParser} to parse this argument into the specified class - * - * @param type The class to parse this argument into - * @return If the parser succeeded - */ - public boolean is(Class type, Class stateType, S state) { - try { - getAs(type, stateType, state); - return true; - } catch (Throwable t) { - return false; - } - } - - /** - * Turn a string into a list of {@link CommandArgument}s. This is needed because of {@link CommandArgument#rawRest} - * - * @param string The string to convert - * @param preserveEmptyLast If the string ends with whitespace, add an empty {@link CommandArgument} to the end This - * is useful for tab completion - * @return A list of {@link CommandArgument}s - */ - public static List from(String string, boolean preserveEmptyLast) { - List args = new ArrayList<>(); - Matcher argMatcher = argPattern.matcher(string); - int lastEnd = -1; - while (argMatcher.find()) { - args.add(new CommandArgument( - args.size(), - argMatcher.group(), - string.substring(argMatcher.start()) - )); - lastEnd = argMatcher.end(); - } - if (preserveEmptyLast && lastEnd < string.length()) { - args.add(new CommandArgument(args.size(), "", "")); - } - return args; - } - - /** - * @see #from(String, boolean) - */ - public static List from(String string) { - return from(string, false); - } - - /** - * Returns an "unknown" {@link CommandArgument}. This shouldn't be used unless you absolutely have no information - - * ESPECIALLY not with {@link CommandInvalidArgumentException}s - * - * @return The unknown {@link CommandArgument} - */ - public static CommandArgument unknown() { - return new CommandArgument(-1, "", ""); - } -} diff --git a/src/api/java/baritone/api/utils/command/argument/ICommandArgument.java b/src/api/java/baritone/api/utils/command/argument/ICommandArgument.java new file mode 100644 index 00000000..7cbe2540 --- /dev/null +++ b/src/api/java/baritone/api/utils/command/argument/ICommandArgument.java @@ -0,0 +1,102 @@ +/* + * 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.api.utils.command.argument; + +import baritone.api.utils.command.argparser.IArgParser; +import baritone.api.utils.command.exception.CommandInvalidTypeException; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; +import net.minecraft.util.EnumFacing; + +/** + * A {@link ICommandArgument} is an immutable object representing one command argument. It contains data on the index of + * that argument, its value, and the rest of the string that argument was found in + *

+ * You're recommended to use {@link IArgConsumer}}s to handle these. + * + * @author Brady + * @since 10/2/2019 + */ +public interface ICommandArgument { + + /** + * @return The index of this command argument in the list of command arguments generated + */ + int getIndex(); + + /** + * @return The raw value of just this argument + */ + String getValue(); + + /** + * @return The raw value of the remaining arguments after this one was captured + */ + String getRawRest(); + + /** + * Gets an enum value from the enum class with the same name as this argument's value + *

+ * For example if you getEnum as an {@link EnumFacing}, and this argument's value is "up", it will return {@link + * EnumFacing#UP} + * + * @param enumClass The enum class to search + * @return An enum constant of that class with the same name as this argument's value + * @throws CommandInvalidTypeException If the constant couldn't be found + * @see IArgConsumer#peekEnum(Class) + * @see IArgConsumer#peekEnum(Class, int) + * @see IArgConsumer#peekEnumOrNull(Class) + * @see IArgConsumer#peekEnumOrNull(Class, int) + * @see IArgConsumer#getEnum(Class) + * @see IArgConsumer#getEnumOrNull(Class) + */ + > E getEnum(Class enumClass) throws CommandInvalidTypeException; + + /** + * Tries to use a stateless {@link IArgParser} to parse this argument into the specified class + * + * @param type The class to parse this argument into + * @return An instance of the specified type + * @throws CommandInvalidTypeException If the parsing failed + */ + T getAs(Class type) throws CommandInvalidTypeException; + + /** + * Tries to use a stateless {@link IArgParser} to parse this argument into the specified class + * + * @param type The class to parse this argument into + * @return If the parser succeeded + */ + boolean is(Class type); + + /** + * Tries to use a stated {@link IArgParser} to parse this argument into the specified class + * + * @param type The class to parse this argument into + * @return An instance of the specified type + * @throws CommandInvalidTypeException If the parsing failed + */ + T getAs(Class type, Class stateType, S state) throws CommandInvalidTypeException; + + /** + * Tries to use a stated {@link IArgParser} to parse this argument into the specified class + * + * @param type The class to parse this argument into + * @return If the parser succeeded + */ + boolean is(Class type, Class stateType, S state); +} diff --git a/src/api/java/baritone/api/utils/command/datatypes/IDatatype.java b/src/api/java/baritone/api/utils/command/datatypes/IDatatype.java index 8c155fb6..36a37fec 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/IDatatype.java +++ b/src/api/java/baritone/api/utils/command/datatypes/IDatatype.java @@ -19,7 +19,7 @@ package baritone.api.utils.command.datatypes; import baritone.api.utils.command.argparser.IArgParser; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.stream.Stream; @@ -41,7 +41,7 @@ import java.util.stream.Stream; public interface IDatatype { /** - * Attempts to complete missing or partial input provided through the {@link ArgConsumer} provided by + * Attempts to complete missing or partial input provided through the {@link IArgConsumer}} provided by * {@link IDatatypeContext#getConsumer()} in order to aide the user in executing commands. *

* One benefit over datatypes over {@link IArgParser}s is that instead of each command trying to guess what values @@ -50,7 +50,7 @@ public interface IDatatype { * * @param ctx The argument consumer to tab complete * @return A stream representing the strings that can be tab completed. DO NOT INCLUDE SPACES IN ANY STRINGS. - * @see ArgConsumer#tabCompleteDatatype(IDatatype) + * @see IArgConsumer#tabCompleteDatatype(IDatatype) */ Stream tabComplete(IDatatypeContext ctx) throws CommandException; } diff --git a/src/api/java/baritone/api/utils/command/datatypes/IDatatypeContext.java b/src/api/java/baritone/api/utils/command/datatypes/IDatatypeContext.java index 1452bf4a..33f3ad23 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/IDatatypeContext.java +++ b/src/api/java/baritone/api/utils/command/datatypes/IDatatypeContext.java @@ -18,7 +18,7 @@ package baritone.api.utils.command.datatypes; import baritone.api.IBaritone; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; /** * Provides an {@link IDatatype} with contextual information so @@ -39,9 +39,9 @@ public interface IDatatypeContext { IBaritone getBaritone(); /** - * Provides the {@link ArgConsumer} to fetch input information from. + * Provides the {@link IArgConsumer}} to fetch input information from. * - * @return The context {@link ArgConsumer}. + * @return The context {@link IArgConsumer}}. */ - ArgConsumer getConsumer(); + IArgConsumer getConsumer(); } diff --git a/src/api/java/baritone/api/utils/command/datatypes/RelativeBlockPos.java b/src/api/java/baritone/api/utils/command/datatypes/RelativeBlockPos.java index 8459db88..4a876220 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/RelativeBlockPos.java +++ b/src/api/java/baritone/api/utils/command/datatypes/RelativeBlockPos.java @@ -19,7 +19,7 @@ package baritone.api.utils.command.datatypes; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.stream.Stream; @@ -32,7 +32,7 @@ public enum RelativeBlockPos implements IDatatypePost tabComplete(IDatatypeContext ctx) throws CommandException { - final ArgConsumer consumer = ctx.getConsumer(); + final IArgConsumer consumer = ctx.getConsumer(); if (consumer.hasAny() && !consumer.has(4)) { while (consumer.has(2)) { if (consumer.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) == null) { diff --git a/src/api/java/baritone/api/utils/command/datatypes/RelativeCoordinate.java b/src/api/java/baritone/api/utils/command/datatypes/RelativeCoordinate.java index 84a29f63..7ef3532a 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/RelativeCoordinate.java +++ b/src/api/java/baritone/api/utils/command/datatypes/RelativeCoordinate.java @@ -18,7 +18,7 @@ package baritone.api.utils.command.datatypes; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -51,7 +51,7 @@ public enum RelativeCoordinate implements IDatatypePost { @Override public Stream tabComplete(IDatatypeContext ctx) throws CommandException { - final ArgConsumer consumer = ctx.getConsumer(); + final IArgConsumer consumer = ctx.getConsumer(); if (!consumer.has(2) && consumer.getString().matches("^(~|$)")) { return Stream.of("~"); } diff --git a/src/api/java/baritone/api/utils/command/datatypes/RelativeFile.java b/src/api/java/baritone/api/utils/command/datatypes/RelativeFile.java index 71a9ca58..0ad4b88b 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/RelativeFile.java +++ b/src/api/java/baritone/api/utils/command/datatypes/RelativeFile.java @@ -18,7 +18,7 @@ package baritone.api.utils.command.datatypes; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.io.File; import java.io.IOException; @@ -70,7 +70,7 @@ public enum RelativeFile implements IDatatypePost { } } - public static Stream tabComplete(ArgConsumer consumer, File base0) throws CommandException { + public static Stream tabComplete(IArgConsumer consumer, File base0) throws CommandException { // I will not make the caller deal with this, seriously // Tab complete code is beautiful and I'm not going to bloat it with dumb ass checked exception bullshit -LoganDark diff --git a/src/api/java/baritone/api/utils/command/datatypes/RelativeGoal.java b/src/api/java/baritone/api/utils/command/datatypes/RelativeGoal.java index d47e05ce..0c98073f 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/RelativeGoal.java +++ b/src/api/java/baritone/api/utils/command/datatypes/RelativeGoal.java @@ -23,7 +23,7 @@ import baritone.api.pathing.goals.GoalXZ; import baritone.api.pathing.goals.GoalYLevel; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.util.math.MathHelper; import java.util.ArrayList; @@ -38,10 +38,10 @@ public enum RelativeGoal implements IDatatypePost { if (origin == null) { origin = BetterBlockPos.ORIGIN; } - final ArgConsumer consumer = ctx.getConsumer(); + final IArgConsumer consumer = ctx.getConsumer(); List> coords = new ArrayList<>(); - final ArgConsumer copy = consumer.copy(); // This is a hack and should be fixed in the future probably + final IArgConsumer copy = consumer.copy(); // This is a hack and should be fixed in the future probably for (int i = 0; i < 3; i++) { if (copy.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) != null) { coords.add(o -> consumer.getDatatypePost(RelativeCoordinate.INSTANCE, o)); diff --git a/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalBlock.java b/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalBlock.java index cd1c0be1..b3c0e284 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalBlock.java +++ b/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalBlock.java @@ -20,7 +20,7 @@ package baritone.api.utils.command.datatypes; import baritone.api.pathing.goals.GoalBlock; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.util.math.MathHelper; import java.util.stream.Stream; @@ -34,7 +34,7 @@ public enum RelativeGoalBlock implements IDatatypePost tabComplete(IDatatypeContext ctx) { - final ArgConsumer consumer = ctx.getConsumer(); + final IArgConsumer consumer = ctx.getConsumer(); if (consumer.hasAtMost(3)) { return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE); } diff --git a/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalXZ.java b/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalXZ.java index b8bd0cd6..22d50ebf 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalXZ.java +++ b/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalXZ.java @@ -20,7 +20,7 @@ package baritone.api.utils.command.datatypes; import baritone.api.pathing.goals.GoalXZ; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.util.math.MathHelper; import java.util.stream.Stream; @@ -34,7 +34,7 @@ public enum RelativeGoalXZ implements IDatatypePost { origin = BetterBlockPos.ORIGIN; } - final ArgConsumer consumer = ctx.getConsumer(); + final IArgConsumer consumer = ctx.getConsumer(); return new GoalXZ( MathHelper.floor(consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.x)), MathHelper.floor(consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.y)) @@ -43,7 +43,7 @@ public enum RelativeGoalXZ implements IDatatypePost { @Override public Stream tabComplete(IDatatypeContext ctx) { - final ArgConsumer consumer = ctx.getConsumer(); + final IArgConsumer consumer = ctx.getConsumer(); if (consumer.hasAtMost(2)) { return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE); } diff --git a/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalYLevel.java b/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalYLevel.java index a8a9b232..7a443b27 100644 --- a/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalYLevel.java +++ b/src/api/java/baritone/api/utils/command/datatypes/RelativeGoalYLevel.java @@ -20,7 +20,7 @@ package baritone.api.utils.command.datatypes; import baritone.api.pathing.goals.GoalYLevel; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.util.math.MathHelper; import java.util.stream.Stream; @@ -41,7 +41,7 @@ public enum RelativeGoalYLevel implements IDatatypePost tabComplete(IDatatypeContext ctx) { - final ArgConsumer consumer = ctx.getConsumer(); + final IArgConsumer consumer = ctx.getConsumer(); if (consumer.hasAtMost(1)) { return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE); } diff --git a/src/api/java/baritone/api/utils/command/exception/CommandInvalidArgumentException.java b/src/api/java/baritone/api/utils/command/exception/CommandInvalidArgumentException.java index 62706705..bc9236e4 100644 --- a/src/api/java/baritone/api/utils/command/exception/CommandInvalidArgumentException.java +++ b/src/api/java/baritone/api/utils/command/exception/CommandInvalidArgumentException.java @@ -17,16 +17,16 @@ package baritone.api.utils.command.exception; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; public abstract class CommandInvalidArgumentException extends CommandErrorMessageException { - public final CommandArgument arg; + public final ICommandArgument arg; - protected CommandInvalidArgumentException(CommandArgument arg, String reason) { + protected CommandInvalidArgumentException(ICommandArgument arg, String reason) { super(String.format( "Error at argument #%s: %s", - arg.index == -1 ? "" : Integer.toString(arg.index + 1), + arg.getIndex() == -1 ? "" : Integer.toString(arg.getIndex() + 1), reason )); this.arg = arg; diff --git a/src/api/java/baritone/api/utils/command/exception/CommandInvalidTypeException.java b/src/api/java/baritone/api/utils/command/exception/CommandInvalidTypeException.java index 5e5b81cd..8dffe1d0 100644 --- a/src/api/java/baritone/api/utils/command/exception/CommandInvalidTypeException.java +++ b/src/api/java/baritone/api/utils/command/exception/CommandInvalidTypeException.java @@ -17,23 +17,23 @@ package baritone.api.utils.command.exception; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; public class CommandInvalidTypeException extends CommandInvalidArgumentException { - public CommandInvalidTypeException(CommandArgument arg, String expected) { + public CommandInvalidTypeException(ICommandArgument arg, String expected) { super(arg, String.format("Expected %s", expected)); } - public CommandInvalidTypeException(CommandArgument arg, String expected, Throwable cause) { + public CommandInvalidTypeException(ICommandArgument arg, String expected, Throwable cause) { super(arg, String.format("Expected %s.\nMore details: %s", expected, cause.getMessage())); } - public CommandInvalidTypeException(CommandArgument arg, String expected, String got) { + public CommandInvalidTypeException(ICommandArgument arg, String expected, String got) { super(arg, String.format("Expected %s, but got %s instead", expected, got)); } - public CommandInvalidTypeException(CommandArgument arg, String expected, String got, Throwable cause) { + public CommandInvalidTypeException(ICommandArgument arg, String expected, String got, Throwable cause) { super(arg, String.format("Expected %s, but got %s instead.\nMore details: %s", expected, got, cause.getMessage())); } } diff --git a/src/api/java/baritone/api/utils/command/exception/CommandNotFoundException.java b/src/api/java/baritone/api/utils/command/exception/CommandNotFoundException.java index e8904cbc..bca8d543 100644 --- a/src/api/java/baritone/api/utils/command/exception/CommandNotFoundException.java +++ b/src/api/java/baritone/api/utils/command/exception/CommandNotFoundException.java @@ -18,7 +18,7 @@ package baritone.api.utils.command.exception; import baritone.api.utils.command.Command; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import java.util.List; @@ -34,7 +34,7 @@ public class CommandNotFoundException extends CommandException { } @Override - public void handle(Command command, List args) { + public void handle(Command command, List args) { HELPER.logDirect(getMessage()); } } 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 6d647a20..55b359cc 100644 --- a/src/api/java/baritone/api/utils/command/exception/CommandUnhandledException.java +++ b/src/api/java/baritone/api/utils/command/exception/CommandUnhandledException.java @@ -18,7 +18,7 @@ package baritone.api.utils.command.exception; import baritone.api.utils.command.Command; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import net.minecraft.util.text.TextFormatting; import java.util.List; @@ -36,7 +36,7 @@ public class CommandUnhandledException extends RuntimeException implements IComm } @Override - public void handle(Command command, List args) { + 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); diff --git a/src/api/java/baritone/api/utils/command/exception/ICommandException.java b/src/api/java/baritone/api/utils/command/exception/ICommandException.java index 379e97b7..229c08c0 100644 --- a/src/api/java/baritone/api/utils/command/exception/ICommandException.java +++ b/src/api/java/baritone/api/utils/command/exception/ICommandException.java @@ -18,7 +18,7 @@ package baritone.api.utils.command.exception; import baritone.api.utils.command.Command; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import net.minecraft.util.text.TextFormatting; import java.util.List; @@ -49,7 +49,7 @@ public interface ICommandException { * @param command The command that threw it. * @param args The arguments the command was called with. */ - default void handle(Command command, List args) { + default void handle(Command command, List args) { HELPER.logDirect(this.getMessage(), TextFormatting.RED); } } diff --git a/src/api/java/baritone/api/utils/command/helpers/arguments/ArgConsumer.java b/src/api/java/baritone/api/utils/command/helpers/arguments/IArgConsumer.java similarity index 58% rename from src/api/java/baritone/api/utils/command/helpers/arguments/ArgConsumer.java rename to src/api/java/baritone/api/utils/command/helpers/arguments/IArgConsumer.java index e92855d1..1e50a530 100644 --- a/src/api/java/baritone/api/utils/command/helpers/arguments/ArgConsumer.java +++ b/src/api/java/baritone/api/utils/command/helpers/arguments/IArgConsumer.java @@ -17,171 +17,118 @@ package baritone.api.utils.command.helpers.arguments; -import baritone.api.IBaritone; import baritone.api.utils.Helper; import baritone.api.utils.command.Command; import baritone.api.utils.command.argparser.IArgParser; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import baritone.api.utils.command.datatypes.IDatatype; -import baritone.api.utils.command.datatypes.IDatatypeContext; import baritone.api.utils.command.datatypes.IDatatypeFor; import baritone.api.utils.command.datatypes.IDatatypePost; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidTypeException; import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException; import baritone.api.utils.command.exception.CommandTooManyArgumentsException; -import baritone.api.utils.command.manager.ICommandManager; import net.minecraft.util.EnumFacing; -import java.util.ArrayList; import java.util.Deque; import java.util.LinkedList; -import java.util.List; import java.util.stream.Stream; /** - * The {@link ArgConsumer} is how {@link Command}s read the arguments passed to them. This class has many benefits: + * The {@link IArgConsumer} is how {@link Command}s read the arguments passed to them. This class has many benefits: * *

    - *
  • Mutability. The whole concept of the {@link ArgConsumer} is to let you gradually consume arguments in any way + *
  • Mutability. The whole concept of the {@link IArgConsumer}} is to let you gradually consume arguments in any way * you'd like. You can change your consumption based on earlier arguments, for subcommands for example.
  • - *
  • You don't need to keep track of your consumption. The {@link ArgConsumer} keeps track of the arguments you + *
  • You don't need to keep track of your consumption. The {@link IArgConsumer}} keeps track of the arguments you * consume so that it can throw detailed exceptions whenever something is out of the ordinary. Additionally, if you * need to retrieve an argument after you've already consumed it - look no further than {@link #consumed()}!
  • *
  • Easy retrieval of many different types. If you need to retrieve an instance of an int or float for example, * look no further than {@link #getAs(Class)}. If you need a more powerful way of retrieving data, try out the many * {@code getDatatype...} methods.
  • - *
  • It's very easy to throw detailed exceptions. The {@link ArgConsumer} has many different methods that can + *
  • It's very easy to throw detailed exceptions. The {@link IArgConsumer}} has many different methods that can * enforce the number of arguments, the type of arguments, and more, throwing different types of * {@link CommandException}s if something seems off. You're recommended to do all validation and store all needed * data in variables BEFORE logging any data to chat via {@link Helper#logDirect(String)}, so that the error * handlers can do their job and log the error to chat.
  • *
*/ -public class ArgConsumer { +public interface IArgConsumer { - /** - * The parent {@link ICommandManager} for this {@link ArgConsumer}. Used by {@link #context}. - */ - private final ICommandManager manager; + LinkedList getArgs(); - /** - * The {@link IDatatypeContext} instance for this {@link ArgConsumer}, passed to - * datatypes when an operation is performed upon them. - * - * @see IDatatype - * @see IDatatypeContext - */ - private final IDatatypeContext context; - - /** - * The list of arguments in this ArgConsumer - */ - public final LinkedList args; - - /** - * The list of consumed arguments for this ArgConsumer. The most recently consumed argument is the last one - */ - public final Deque consumed; - - private ArgConsumer(ICommandManager manager, Deque args, Deque consumed) { - this.manager = manager; - this.context = this.new Context(); - this.args = new LinkedList<>(args); - this.consumed = new LinkedList<>(consumed); - } - - public ArgConsumer(ICommandManager manager, List args) { - this(manager, new LinkedList<>(args), new LinkedList<>()); - } + Deque getConsumed(); /** * @param num The number of arguments to check for - * @return {@code true} if there are at least {@code num} arguments left in this {@link ArgConsumer} + * @return {@code true} if there are at least {@code num} arguments left in this {@link IArgConsumer}} * @see #hasAny() * @see #hasAtMost(int) * @see #hasExactly(int) */ - public boolean has(int num) { - return args.size() >= num; - } + boolean has(int num); /** - * @return {@code true} if there is at least 1 argument left in this {@link ArgConsumer} + * @return {@code true} if there is at least 1 argument left in this {@link IArgConsumer}} * @see #has(int) * @see #hasAtMostOne() * @see #hasExactlyOne() */ - public boolean hasAny() { - return has(1); - } + boolean hasAny(); /** * @param num The number of arguments to check for - * @return {@code true} if there are at most {@code num} arguments left in this {@link ArgConsumer} + * @return {@code true} if there are at most {@code num} arguments left in this {@link IArgConsumer}} * @see #has(int) * @see #hasAtMost(int) * @see #hasExactly(int) */ - public boolean hasAtMost(int num) { - return args.size() <= num; - } + boolean hasAtMost(int num); /** - * @return {@code true} if there is at most 1 argument left in this {@link ArgConsumer} + * @return {@code true} if there is at most 1 argument left in this {@link IArgConsumer}} * @see #hasAny() * @see #hasAtMostOne() * @see #hasExactlyOne() */ - public boolean hasAtMostOne() { - return hasAtMost(1); - } + boolean hasAtMostOne(); /** * @param num The number of arguments to check for - * @return {@code true} if there are exactly {@code num} arguments left in this {@link ArgConsumer} + * @return {@code true} if there are exactly {@code num} arguments left in this {@link IArgConsumer}} * @see #has(int) * @see #hasAtMost(int) */ - public boolean hasExactly(int num) { - return args.size() == num; - } + boolean hasExactly(int num); /** - * @return {@code true} if there is exactly 1 argument left in this {@link ArgConsumer} + * @return {@code true} if there is exactly 1 argument left in this {@link IArgConsumer}} * @see #hasAny() * @see #hasAtMostOne() */ - public boolean hasExactlyOne() { - return hasExactly(1); - } + boolean hasExactlyOne(); /** * @param index The index to peek - * @return The argument at index {@code index} in this {@link ArgConsumer}, with 0 being the next one. This does not - * mutate the {@link ArgConsumer} + * @return The argument at index {@code index} in this {@link IArgConsumer}}, with 0 being the next one. This does not + * mutate the {@link IArgConsumer}} * @throws CommandNotEnoughArgumentsException If there is less than {@code index + 1} arguments left * @see #peek() * @see #peekString(int) * @see #peekAs(Class, int) * @see #get() */ - public CommandArgument peek(int index) throws CommandNotEnoughArgumentsException { - requireMin(index + 1); - return args.get(index); - } + ICommandArgument peek(int index) throws CommandNotEnoughArgumentsException; /** - * @return The next argument in this {@link ArgConsumer}. This does not mutate the {@link ArgConsumer} + * @return The next argument in this {@link IArgConsumer}}. This does not mutate the {@link IArgConsumer}} * @throws CommandNotEnoughArgumentsException If there is less than one argument left * @see #peek(int) * @see #peekString() * @see #peekAs(Class) * @see #get() */ - public CommandArgument peek() throws CommandNotEnoughArgumentsException { - return peek(0); - } + ICommandArgument peek() throws CommandNotEnoughArgumentsException; /** * @param index The index to peek @@ -192,9 +139,7 @@ public class ArgConsumer { * @see #peek() * @see #getAs(Class) */ - public boolean is(Class type, int index) throws CommandNotEnoughArgumentsException { - return peek(index).is(type); - } + boolean is(Class type, int index) throws CommandNotEnoughArgumentsException; /** * @param type The type to check for @@ -204,31 +149,25 @@ public class ArgConsumer { * @see #peek() * @see #getAs(Class) */ - public boolean is(Class type) throws CommandNotEnoughArgumentsException { - return is(type, 0); - } + boolean is(Class type) throws CommandNotEnoughArgumentsException; /** * @param index The index to peek - * @return The value of the argument at index {@code index} in this {@link ArgConsumer}, with 0 being the next one - * This does not mutate the {@link ArgConsumer} + * @return The value of the argument at index {@code index} in this {@link IArgConsumer}}, with 0 being the next one + * This does not mutate the {@link IArgConsumer}} * @throws CommandNotEnoughArgumentsException If there is less than {@code index + 1} arguments left * @see #peek() * @see #peekString() */ - public String peekString(int index) throws CommandNotEnoughArgumentsException { - return peek(index).value; - } + String peekString(int index) throws CommandNotEnoughArgumentsException; /** - * @return The value of the next argument in this {@link ArgConsumer}. This does not mutate the {@link ArgConsumer} + * @return The value of the next argument in this {@link IArgConsumer}}. This does not mutate the {@link IArgConsumer}} * @throws CommandNotEnoughArgumentsException If there is less than one argument left * @see #peekString(int) * @see #getString() */ - public String peekString() throws CommandNotEnoughArgumentsException { - return peekString(0); - } + String peekString() throws CommandNotEnoughArgumentsException; /** * @param index The index to peek @@ -238,11 +177,9 @@ public class ArgConsumer { * @throws java.util.NoSuchElementException If the constant couldn't be found * @see #peekEnumOrNull(Class) * @see #getEnum(Class) - * @see CommandArgument#getEnum(Class) + * @see ICommandArgument#getEnum(Class) */ - public > E peekEnum(Class enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return peek(index).getEnum(enumClass); - } + > E peekEnum(Class enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; /** * @param enumClass The class to search @@ -251,11 +188,9 @@ public class ArgConsumer { * @throws CommandInvalidTypeException If the constant couldn't be found * @see #peekEnumOrNull(Class) * @see #getEnum(Class) - * @see CommandArgument#getEnum(Class) + * @see ICommandArgument#getEnum(Class) */ - public > E peekEnum(Class enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return peekEnum(enumClass, 0); - } + > E peekEnum(Class enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; /** * @param index The index to peek @@ -264,15 +199,9 @@ public class ArgConsumer { * next argument's value. If no constant could be found, null * @see #peekEnum(Class) * @see #getEnumOrNull(Class) - * @see CommandArgument#getEnum(Class) + * @see ICommandArgument#getEnum(Class) */ - public > E peekEnumOrNull(Class enumClass, int index) throws CommandNotEnoughArgumentsException { - try { - return peekEnum(enumClass, index); - } catch (CommandInvalidTypeException e) { - return null; - } - } + > E peekEnumOrNull(Class enumClass, int index) throws CommandNotEnoughArgumentsException; /** * @param enumClass The class to search @@ -280,11 +209,9 @@ public class ArgConsumer { * next argument's value. If no constant could be found, null * @see #peekEnum(Class) * @see #getEnumOrNull(Class) - * @see CommandArgument#getEnum(Class) + * @see ICommandArgument#getEnum(Class) */ - public > E peekEnumOrNull(Class enumClass) throws CommandNotEnoughArgumentsException { - return peekEnumOrNull(enumClass, 0); - } + > E peekEnumOrNull(Class enumClass) throws CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the argument at the specified index into the specified @@ -292,7 +219,7 @@ public class ArgConsumer { *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @param index The index to peek @@ -303,16 +230,14 @@ public class ArgConsumer { * @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrNull(Class, int) */ - public T peekAs(Class type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return peek(index).getAs(type); - } + T peekAs(Class type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the next argument into the specified class *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @return An instance of the specified type @@ -322,9 +247,7 @@ public class ArgConsumer { * @see #peekAsOrDefault(Class, Object) * @see #peekAsOrNull(Class) */ - public T peekAs(Class type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return peekAs(type, 0); - } + T peekAs(Class type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the argument at the specified index into the specified @@ -332,7 +255,7 @@ public class ArgConsumer { *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @param def The value to return if the argument can't be parsed @@ -343,20 +266,14 @@ public class ArgConsumer { * @see #peekAs(Class, int) * @see #peekAsOrNull(Class, int) */ - public T peekAsOrDefault(Class type, T def, int index) throws CommandNotEnoughArgumentsException { - try { - return peekAs(type, index); - } catch (CommandInvalidTypeException e) { - return def; - } - } + T peekAsOrDefault(Class type, T def, int index) throws CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the next argument into the specified class *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @param def The value to return if the argument can't be parsed @@ -366,9 +283,7 @@ public class ArgConsumer { * @see #peekAs(Class) * @see #peekAsOrNull(Class) */ - public T peekAsOrDefault(Class type, T def) throws CommandNotEnoughArgumentsException { - return peekAsOrDefault(type, def, 0); - } + T peekAsOrDefault(Class type, T def) throws CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the argument at the specified index into the specified @@ -376,7 +291,7 @@ public class ArgConsumer { *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @param index The index to peek @@ -386,16 +301,14 @@ public class ArgConsumer { * @see #peekAs(Class, int) * @see #peekAsOrDefault(Class, Object, int) */ - public T peekAsOrNull(Class type, int index) throws CommandNotEnoughArgumentsException { - return peekAsOrDefault(type, null, index); - } + T peekAsOrNull(Class type, int index) throws CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the next argument into the specified class *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @return An instance of the specified type, or {@code null} if it couldn't be parsed @@ -404,48 +317,30 @@ public class ArgConsumer { * @see #peekAs(Class) * @see #peekAsOrDefault(Class, Object) */ - public T peekAsOrNull(Class type) throws CommandNotEnoughArgumentsException { - return peekAsOrNull(type, 0); - } + T peekAsOrNull(Class type) throws CommandNotEnoughArgumentsException; - public T peekDatatype(IDatatypeFor datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return copy().getDatatypeFor(datatype); - } + T peekDatatype(IDatatypeFor datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; - public T peekDatatype(IDatatypePost datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return this.peekDatatype(datatype, null); - } + T peekDatatype(IDatatypePost datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; - public T peekDatatype(IDatatypePost datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return copy().getDatatypePost(datatype, original); - } + T peekDatatype(IDatatypePost datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; - public T peekDatatypeOrNull(IDatatypeFor datatype) { - return copy().getDatatypeForOrNull(datatype); - } + T peekDatatypeOrNull(IDatatypeFor datatype); - public T peekDatatypeOrNull(IDatatypePost datatype) { - return copy().getDatatypePostOrNull(datatype, null); - } + T peekDatatypeOrNull(IDatatypePost datatype); - public > T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return copy().getDatatypePost(datatype, original); - } + > T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; - public > T peekDatatypePostOrDefault(D datatype, O original, T def) { - return copy().getDatatypePostOrDefault(datatype, original, def); - } + > T peekDatatypePostOrDefault(D datatype, O original, T def); - public > T peekDatatypePostOrNull(D datatype, O original) { - return peekDatatypePostOrDefault(datatype, original, null); - } + > T peekDatatypePostOrNull(D datatype, O original); /** * Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. *

* Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method. * @@ -454,16 +349,14 @@ public class ArgConsumer { * @see IDatatype * @see IDatatypeFor */ - public > T peekDatatypeFor(Class datatype) { - return copy().peekDatatypeFor(datatype); - } + > T peekDatatypeFor(Class datatype); /** * Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. *

* Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method. * @@ -473,16 +366,14 @@ public class ArgConsumer { * @see IDatatype * @see IDatatypeFor */ - public > T peekDatatypeForOrDefault(Class datatype, T def) { - return copy().peekDatatypeForOrDefault(datatype, def); - } + > T peekDatatypeForOrDefault(Class datatype, T def); /** * Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. *

* Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method. * @@ -491,9 +382,7 @@ public class ArgConsumer { * @see IDatatype * @see IDatatypeFor */ - public > T peekDatatypeForOrNull(Class datatype) { - return peekDatatypeForOrDefault(datatype, null); - } + > T peekDatatypeForOrNull(Class datatype); /** * Gets the next argument and returns it. This consumes the first argument so that subsequent calls will return @@ -502,12 +391,7 @@ public class ArgConsumer { * @return The next argument * @throws CommandNotEnoughArgumentsException If there's less than one argument left */ - public CommandArgument get() throws CommandNotEnoughArgumentsException { - requireMin(1); - CommandArgument arg = args.removeFirst(); - consumed.add(arg); - return arg; - } + ICommandArgument get() throws CommandNotEnoughArgumentsException; /** * Gets the value of the next argument and returns it. This consumes the first argument so that subsequent calls @@ -516,9 +400,7 @@ public class ArgConsumer { * @return The value of the next argument * @throws CommandNotEnoughArgumentsException If there's less than one argument left */ - public String getString() throws CommandNotEnoughArgumentsException { - return get().value; - } + String getString() throws CommandNotEnoughArgumentsException; /** * Gets an enum value from the enum class with the same name as the next argument's value @@ -531,11 +413,9 @@ public class ArgConsumer { * @throws CommandInvalidTypeException If the constant couldn't be found * @see #peekEnum(Class) * @see #getEnumOrNull(Class) - * @see CommandArgument#getEnum(Class) + * @see ICommandArgument#getEnum(Class) */ - public > E getEnum(Class enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return get().getEnum(enumClass); - } + > E getEnum(Class enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; /** * Gets an enum value from the enum class with the same name as the next argument's value @@ -550,16 +430,9 @@ public class ArgConsumer { * @see #getEnum(Class) * @see #getEnumOrNull(Class) * @see #peekEnumOrNull(Class) - * @see CommandArgument#getEnum(Class) + * @see ICommandArgument#getEnum(Class) */ - public > E getEnumOrDefault(Class enumClass, E def) throws CommandNotEnoughArgumentsException { - try { - peekEnum(enumClass); - return getEnum(enumClass); - } catch (CommandInvalidTypeException e) { - return def; - } - } + > E getEnumOrDefault(Class enumClass, E def) throws CommandNotEnoughArgumentsException; /** * Gets an enum value from the enum class with the same name as the next argument's value @@ -573,18 +446,16 @@ public class ArgConsumer { * @see #getEnum(Class) * @see #getEnumOrDefault(Class, Enum) * @see #peekEnumOrNull(Class) - * @see CommandArgument#getEnum(Class) + * @see ICommandArgument#getEnum(Class) */ - public > E getEnumOrNull(Class enumClass) throws CommandNotEnoughArgumentsException { - return getEnumOrDefault(enumClass, null); - } + > E getEnumOrNull(Class enumClass) throws CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the next argument into the specified class *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @return An instance of the specified type @@ -597,16 +468,14 @@ public class ArgConsumer { * @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrNull(Class, int) */ - public T getAs(Class type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - return get().getAs(type); - } + T getAs(Class type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the next argument into the specified class *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @param def The default value @@ -619,22 +488,14 @@ public class ArgConsumer { * @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrNull(Class, int) */ - public T getAsOrDefault(Class type, T def) throws CommandNotEnoughArgumentsException { - try { - T val = peek().getAs(type); - get(); - return val; - } catch (CommandInvalidTypeException e) { - return def; - } - } + T getAsOrDefault(Class type, T def) throws CommandNotEnoughArgumentsException; /** * Tries to use a stateless {@link IArgParser} to parse the next argument into the specified class *

* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take. * While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire - * {@link ArgConsumer}. + * {@link IArgConsumer}}. * * @param type The type to peek as * @return An instance of the specified type, or {@code null} if it couldn't be parsed @@ -646,75 +507,25 @@ public class ArgConsumer { * @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrNull(Class, int) */ - public T getAsOrNull(Class type) throws CommandNotEnoughArgumentsException { - return getAsOrDefault(type, null); - } + T getAsOrNull(Class type) throws CommandNotEnoughArgumentsException; - public > T getDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - try { - return datatype.apply(this.context, original); - } catch (Exception e) { - e.printStackTrace(); - throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getClass().getSimpleName()); - } - } + > T getDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; - public > T getDatatypePostOrDefault(D datatype, O original, T _default) { - final List argsSnapshot = new ArrayList<>(this.args); - final List consumedSnapshot = new ArrayList<>(this.consumed); - try { - return this.getDatatypePost(datatype, original); - } catch (Exception e) { - this.args.clear(); - this.args.addAll(argsSnapshot); - this.consumed.clear(); - this.consumed.addAll(consumedSnapshot); - return _default; - } - } + > T getDatatypePostOrDefault(D datatype, O original, T _default); - public > T getDatatypePostOrNull(D datatype, O original) { - return this.getDatatypePostOrDefault(datatype, original, null); - } + > T getDatatypePostOrNull(D datatype, O original); - public > T getDatatypeFor(D datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { - try { - return datatype.get(this.context); - } catch (Exception e) { - throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getClass().getSimpleName()); - } - } + > T getDatatypeFor(D datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException; - public > T getDatatypeForOrDefault(D datatype, T def) { - final List argsSnapshot = new ArrayList<>(this.args); - final List consumedSnapshot = new ArrayList<>(this.consumed); - try { - return this.getDatatypeFor(datatype); - } catch (Exception e) { - this.args.clear(); - this.args.addAll(argsSnapshot); - this.consumed.clear(); - this.consumed.addAll(consumedSnapshot); - return def; - } - } + > T getDatatypeForOrDefault(D datatype, T def); - public > T getDatatypeForOrNull(D datatype) { - return this.getDatatypeForOrDefault(datatype, null); - } + > T getDatatypeForOrNull(D datatype); - public Stream tabCompleteDatatype(T datatype) { - try { - return datatype.tabComplete(this.context); - } catch (Exception e) { - e.printStackTrace(); - } - return Stream.empty(); - } + Stream tabCompleteDatatype(T datatype); /** * Returns the "raw rest" of the string. For example, from a string arg1 arg2  arg3, split - * into three {@link CommandArgument}s {@code "arg1"}, {@code "arg2"}, and {@code "arg3"}: + * into three {@link ICommandArgument}s {@code "arg1"}, {@code "arg2"}, and {@code "arg3"}: * *

    *
  • {@code rawRest()} would return arg1 arg2  arg3
  • @@ -726,9 +537,7 @@ public class ArgConsumer { * * @return The "raw rest" of the string. */ - public String rawRest() { - return args.size() > 0 ? args.getFirst().rawRest : ""; - } + String rawRest(); /** * @param min The minimum amount of arguments to require. @@ -736,11 +545,7 @@ public class ArgConsumer { * @see #requireMax(int) * @see #requireExactly(int) */ - public void requireMin(int min) throws CommandNotEnoughArgumentsException { - if (args.size() < min) { - throw new CommandNotEnoughArgumentsException(min + consumed.size()); - } - } + void requireMin(int min) throws CommandNotEnoughArgumentsException; /** * @param max The maximum amount of arguments allowed. @@ -748,11 +553,7 @@ public class ArgConsumer { * @see #requireMin(int) * @see #requireExactly(int) */ - public void requireMax(int max) throws CommandTooManyArgumentsException { - if (args.size() > max) { - throw new CommandTooManyArgumentsException(max + consumed.size()); - } - } + void requireMax(int max) throws CommandTooManyArgumentsException; /** * @param args The exact amount of arguments to require. @@ -761,58 +562,34 @@ public class ArgConsumer { * @see #requireMin(int) * @see #requireMax(int) */ - public void requireExactly(int args) throws CommandException { - requireMin(args); - requireMax(args); - } + void requireExactly(int args) throws CommandException; /** - * @return If this {@link ArgConsumer} has consumed at least one argument. + * @return If this {@link IArgConsumer}} has consumed at least one argument. * @see #consumed() * @see #consumedString() */ - public boolean hasConsumed() { - return !consumed.isEmpty(); - } + boolean hasConsumed(); /** - * @return The last argument this {@link ArgConsumer} has consumed, or the {@link CommandArgument#unknown() unknown} - * argument if no arguments have been consumed yet. + * @return The last argument this {@link IArgConsumer}} has consumed, or an "unknown" argument, indicated by a + * comamnd argument index that has a value of {@code -1}, if no arguments have been consumed yet. * @see #consumedString() * @see #hasConsumed() */ - public CommandArgument consumed() { - return consumed.size() > 0 ? consumed.getLast() : CommandArgument.unknown(); - } + ICommandArgument consumed(); /** - * @return The value of thelast argument this {@link ArgConsumer} has consumed, or an empty string if no arguments + * @return The value of thelast argument this {@link IArgConsumer}} has consumed, or an empty string if no arguments * have been consumed yet * @see #consumed() * @see #hasConsumed() */ - public String consumedString() { - return consumed().value; - } + String consumedString(); /** - * @return A copy of this {@link ArgConsumer}. It has the same arguments (both consumed and not), but does not + * @return A copy of this {@link IArgConsumer}}. It has the same arguments (both consumed and not), but does not * affect or mutate this instance. Useful for the various {@code peek} functions */ - public ArgConsumer copy() { - return new ArgConsumer(manager, args, consumed); - } - - private final class Context implements IDatatypeContext { - - @Override - public final IBaritone getBaritone() { - return ArgConsumer.this.manager.getBaritone(); - } - - @Override - public final ArgConsumer getConsumer() { - return ArgConsumer.this; - } - } + IArgConsumer copy(); } diff --git a/src/api/java/baritone/api/utils/command/helpers/pagination/Paginator.java b/src/api/java/baritone/api/utils/command/helpers/pagination/Paginator.java index da5ac436..cf5d5d7c 100644 --- a/src/api/java/baritone/api/utils/command/helpers/pagination/Paginator.java +++ b/src/api/java/baritone/api/utils/command/helpers/pagination/Paginator.java @@ -20,7 +20,7 @@ package baritone.api.utils.command.helpers.pagination; import baritone.api.utils.Helper; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidTypeException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextFormatting; @@ -115,7 +115,7 @@ public class Paginator implements Helper { display(transform, null); } - public static void paginate(ArgConsumer consumer, Paginator pagi, Runnable pre, Function transform, String commandPrefix) throws CommandException { + public static void paginate(IArgConsumer consumer, Paginator pagi, Runnable pre, Function transform, String commandPrefix) throws CommandException { int page = 1; consumer.requireMax(1); if (consumer.hasAny()) { @@ -127,7 +127,7 @@ public class Paginator implements Helper { "a valid page (1-%d)", pagi.getMaxPage() ), - consumer.consumed().value + consumer.consumed().getValue() ); } } @@ -138,47 +138,47 @@ public class Paginator implements Helper { pagi.display(transform, commandPrefix); } - public static void paginate(ArgConsumer consumer, List elems, Runnable pre, Function transform, String commandPrefix) throws CommandException { + public static void paginate(IArgConsumer consumer, List elems, Runnable pre, Function transform, String commandPrefix) throws CommandException { paginate(consumer, new Paginator<>(elems), pre, transform, commandPrefix); } - public static void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function transform, String commandPrefix) throws CommandException { + public static void paginate(IArgConsumer consumer, T[] elems, Runnable pre, Function transform, String commandPrefix) throws CommandException { paginate(consumer, Arrays.asList(elems), pre, transform, commandPrefix); } - public static void paginate(ArgConsumer consumer, Paginator pagi, Function transform, String commandPrefix) throws CommandException { + public static void paginate(IArgConsumer consumer, Paginator pagi, Function transform, String commandPrefix) throws CommandException { paginate(consumer, pagi, null, transform, commandPrefix); } - public static void paginate(ArgConsumer consumer, List elems, Function transform, String commandPrefix) throws CommandException { + public static void paginate(IArgConsumer consumer, List elems, Function transform, String commandPrefix) throws CommandException { paginate(consumer, new Paginator<>(elems), null, transform, commandPrefix); } - public static void paginate(ArgConsumer consumer, T[] elems, Function transform, String commandPrefix) throws CommandException { + public static void paginate(IArgConsumer consumer, T[] elems, Function transform, String commandPrefix) throws CommandException { paginate(consumer, Arrays.asList(elems), null, transform, commandPrefix); } - public static void paginate(ArgConsumer consumer, Paginator pagi, Runnable pre, Function transform) throws CommandException { + public static void paginate(IArgConsumer consumer, Paginator pagi, Runnable pre, Function transform) throws CommandException { paginate(consumer, pagi, pre, transform, null); } - public static void paginate(ArgConsumer consumer, List elems, Runnable pre, Function transform) throws CommandException { + public static void paginate(IArgConsumer consumer, List elems, Runnable pre, Function transform) throws CommandException { paginate(consumer, new Paginator<>(elems), pre, transform, null); } - public static void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function transform) throws CommandException { + public static void paginate(IArgConsumer consumer, T[] elems, Runnable pre, Function transform) throws CommandException { paginate(consumer, Arrays.asList(elems), pre, transform, null); } - public static void paginate(ArgConsumer consumer, Paginator pagi, Function transform) throws CommandException { + public static void paginate(IArgConsumer consumer, Paginator pagi, Function transform) throws CommandException { paginate(consumer, pagi, null, transform, null); } - public static void paginate(ArgConsumer consumer, List elems, Function transform) throws CommandException { + public static void paginate(IArgConsumer consumer, List elems, Function transform) throws CommandException { paginate(consumer, new Paginator<>(elems), null, transform, null); } - public static void paginate(ArgConsumer consumer, T[] elems, Function transform) throws CommandException { + public static void paginate(IArgConsumer consumer, T[] elems, Function transform) throws CommandException { paginate(consumer, Arrays.asList(elems), null, transform, null); } } diff --git a/src/api/java/baritone/api/utils/command/helpers/tabcomplete/TabCompleteHelper.java b/src/api/java/baritone/api/utils/command/helpers/tabcomplete/TabCompleteHelper.java index db63b2d5..9be9dfb3 100644 --- a/src/api/java/baritone/api/utils/command/helpers/tabcomplete/TabCompleteHelper.java +++ b/src/api/java/baritone/api/utils/command/helpers/tabcomplete/TabCompleteHelper.java @@ -21,8 +21,7 @@ import baritone.api.BaritoneAPI; import baritone.api.Settings; import baritone.api.event.events.TabCompleteEvent; import baritone.api.utils.SettingsUtil; -import baritone.api.utils.command.datatypes.IDatatype; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.api.utils.command.manager.ICommandManager; import net.minecraft.util.ResourceLocation; @@ -45,7 +44,7 @@ import java.util.stream.Stream; * {@link #filterPrefix(String)} *
  • Get the stream using {@link #stream()}
  • *
  • Pass it up to whatever's calling your tab complete function (i.e. - * {@link ICommandManager#tabComplete(String)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)})
  • + * {@link ICommandManager#tabComplete(String)} or {@link IArgConsumer}#tabCompleteDatatype(IDatatype)}) *
*

* For advanced users: if you're intercepting {@link TabCompleteEvent}s directly, use {@link #build()} instead for an diff --git a/src/api/java/baritone/api/utils/command/manager/ICommandManager.java b/src/api/java/baritone/api/utils/command/manager/ICommandManager.java index 63682433..d3fac415 100644 --- a/src/api/java/baritone/api/utils/command/manager/ICommandManager.java +++ b/src/api/java/baritone/api/utils/command/manager/ICommandManager.java @@ -19,7 +19,7 @@ package baritone.api.utils.command.manager; import baritone.api.IBaritone; import baritone.api.utils.command.Command; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import baritone.api.utils.command.registry.Registry; import net.minecraft.util.Tuple; @@ -44,9 +44,9 @@ public interface ICommandManager { boolean execute(String string); - boolean execute(Tuple> expanded); + boolean execute(Tuple> expanded); - Stream tabComplete(Tuple> expanded); + Stream tabComplete(Tuple> expanded); Stream tabComplete(String prefix); } diff --git a/src/main/java/baritone/utils/command/BaritoneChatControl.java b/src/main/java/baritone/utils/command/BaritoneChatControl.java index 30ba9244..70e2370e 100644 --- a/src/main/java/baritone/utils/command/BaritoneChatControl.java +++ b/src/main/java/baritone/utils/command/BaritoneChatControl.java @@ -26,12 +26,13 @@ import baritone.api.event.events.TabCompleteEvent; import baritone.api.event.listener.AbstractGameEventListener; import baritone.api.utils.Helper; import baritone.api.utils.SettingsUtil; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException; import baritone.api.utils.command.exception.CommandNotFoundException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.manager.ICommandManager; +import baritone.utils.command.argument.CommandArguments; import baritone.utils.command.manager.CommandManager; import net.minecraft.util.Tuple; import net.minecraft.util.text.ITextComponent; @@ -106,7 +107,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener { if (msg.isEmpty()) { return this.runCommand("help"); } - Tuple> pair = CommandManager.expand(msg); + Tuple> pair = CommandManager.expand(msg); String command = pair.getFirst(); String rest = msg.substring(pair.getFirst().length()); ArgConsumer argc = new ArgConsumer(this.manager, pair.getSecond()); @@ -155,7 +156,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener { return; } String msg = prefix.substring(commandPrefix.length()); - List args = CommandArgument.from(msg, true); + List args = CommandArguments.from(msg, true); Stream stream = tabComplete(msg); if (args.size() == 1) { stream = stream.map(x -> commandPrefix + x); @@ -165,7 +166,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener { public Stream tabComplete(String msg) { try { - List args = CommandArgument.from(msg, true); + List args = CommandArguments.from(msg, true); ArgConsumer argc = new ArgConsumer(this.manager, args); if (argc.hasAtMost(2)) { if (argc.hasExactly(1)) { diff --git a/src/main/java/baritone/utils/command/argument/CommandArgument.java b/src/main/java/baritone/utils/command/argument/CommandArgument.java new file mode 100644 index 00000000..9c374248 --- /dev/null +++ b/src/main/java/baritone/utils/command/argument/CommandArgument.java @@ -0,0 +1,97 @@ +/* + * 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.command.argument; + +import baritone.api.utils.command.argparser.ArgParserManager; +import baritone.api.utils.command.argument.ICommandArgument; +import baritone.api.utils.command.exception.CommandInvalidArgumentException; +import baritone.api.utils.command.exception.CommandInvalidTypeException; + +import java.util.stream.Stream; + +/** + * The default implementation of {@link ICommandArgument} + * + * @author LoganDark + */ +class CommandArgument implements ICommandArgument { + + private final int index; + private final String value; + private final String rawRest; + + CommandArgument(int index, String value, String rawRest) { + this.index = index; + this.value = value; + this.rawRest = rawRest; + } + + @Override + public int getIndex() { + return this.index; + } + + @Override + public String getValue() { + return this.value; + } + + @Override + public String getRawRest() { + return this.rawRest; + } + + @Override + public > E getEnum(Class enumClass) throws CommandInvalidTypeException { + return Stream.of(enumClass.getEnumConstants()) + .filter(e -> e.name().equalsIgnoreCase(value)) + .findFirst() + .orElseThrow(() -> new CommandInvalidTypeException(this, enumClass.getSimpleName())); + } + + @Override + public T getAs(Class type) throws CommandInvalidTypeException { + return ArgParserManager.parseStateless(type, this); + } + + @Override + public boolean is(Class type) { + try { + getAs(type); + return true; + } catch (Throwable t) { + return false; + } + } + + @SuppressWarnings("UnusedReturnValue") + @Override + public T getAs(Class type, Class stateType, S state) throws CommandInvalidTypeException { + return ArgParserManager.parseStated(type, stateType, this, state); + } + + @Override + public boolean is(Class type, Class stateType, S state) { + try { + getAs(type, stateType, state); + return true; + } catch (Throwable t) { + return false; + } + } +} diff --git a/src/main/java/baritone/utils/command/argument/CommandArguments.java b/src/main/java/baritone/utils/command/argument/CommandArguments.java new file mode 100644 index 00000000..22f89559 --- /dev/null +++ b/src/main/java/baritone/utils/command/argument/CommandArguments.java @@ -0,0 +1,79 @@ +/* + * 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.command.argument; + +import baritone.api.utils.command.argument.ICommandArgument; +import baritone.api.utils.command.exception.CommandInvalidArgumentException; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @author LoganDark + */ +public final class CommandArguments { + + private CommandArguments() {} + + private static final Pattern ARG_PATTERN = Pattern.compile("\\S+"); + + /** + * Turn a string into a list of {@link ICommandArgument}s. This is needed because of {@link ICommandArgument#getRawRest()} + * + * @param string The string to convert + * @param preserveEmptyLast If the string ends with whitespace, add an empty {@link ICommandArgument} to the end This + * is useful for tab completion + * @return A list of {@link ICommandArgument}s + */ + public static List from(String string, boolean preserveEmptyLast) { + List args = new ArrayList<>(); + Matcher argMatcher = ARG_PATTERN.matcher(string); + int lastEnd = -1; + while (argMatcher.find()) { + args.add(new CommandArgument( + args.size(), + argMatcher.group(), + string.substring(argMatcher.start()) + )); + lastEnd = argMatcher.end(); + } + if (preserveEmptyLast && lastEnd < string.length()) { + args.add(new CommandArgument(args.size(), "", "")); + } + return args; + } + + /** + * @see #from(String, boolean) + */ + public static List from(String string) { + return from(string, false); + } + + /** + * Returns an "unknown" {@link CommandArgument}. This shouldn't be used unless you absolutely have no information - + * ESPECIALLY not with {@link CommandInvalidArgumentException}s + * + * @return The unknown {@link CommandArgument} + */ + public static CommandArgument unknown() { + return new CommandArgument(-1, "", ""); + } +} diff --git a/src/main/java/baritone/utils/command/defaults/AxisCommand.java b/src/main/java/baritone/utils/command/defaults/AxisCommand.java index f80356ad..f67c82e5 100644 --- a/src/main/java/baritone/utils/command/defaults/AxisCommand.java +++ b/src/main/java/baritone/utils/command/defaults/AxisCommand.java @@ -22,7 +22,7 @@ import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalAxis; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -35,7 +35,7 @@ public class AxisCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); Goal goal = new GoalAxis(); baritone.getCustomGoalProcess().setGoal(goal); @@ -43,7 +43,7 @@ public class AxisCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/BlacklistCommand.java b/src/main/java/baritone/utils/command/defaults/BlacklistCommand.java index 43aaa737..accda72b 100644 --- a/src/main/java/baritone/utils/command/defaults/BlacklistCommand.java +++ b/src/main/java/baritone/utils/command/defaults/BlacklistCommand.java @@ -22,7 +22,7 @@ import baritone.api.process.IGetToBlockProcess; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -35,7 +35,7 @@ public class BlacklistCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); IGetToBlockProcess proc = baritone.getGetToBlockProcess(); if (!proc.isActive()) { @@ -49,7 +49,7 @@ public class BlacklistCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/BuildCommand.java b/src/main/java/baritone/utils/command/defaults/BuildCommand.java index 5d084d48..b852d0be 100644 --- a/src/main/java/baritone/utils/command/defaults/BuildCommand.java +++ b/src/main/java/baritone/utils/command/defaults/BuildCommand.java @@ -24,7 +24,7 @@ import baritone.api.utils.command.datatypes.RelativeBlockPos; import baritone.api.utils.command.datatypes.RelativeFile; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.client.Minecraft; import java.io.File; @@ -42,7 +42,7 @@ public class BuildCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { File file = args.getDatatypePost(RelativeFile.INSTANCE, schematicsDir).getAbsoluteFile(); if (!file.getName().toLowerCase(Locale.US).endsWith(".schematic")) { file = new File(file.getAbsolutePath() + ".schematic"); @@ -64,7 +64,7 @@ public class BuildCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { if (args.hasExactlyOne()) { return RelativeFile.tabComplete(args, schematicsDir); } else if (args.has(2)) { diff --git a/src/main/java/baritone/utils/command/defaults/CancelCommand.java b/src/main/java/baritone/utils/command/defaults/CancelCommand.java index 67cde8f4..868da5e0 100644 --- a/src/main/java/baritone/utils/command/defaults/CancelCommand.java +++ b/src/main/java/baritone/utils/command/defaults/CancelCommand.java @@ -20,7 +20,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -33,14 +33,14 @@ public class CancelCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); baritone.getPathingBehavior().cancelEverything(); logDirect("ok canceled"); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/ChestsCommand.java b/src/main/java/baritone/utils/command/defaults/ChestsCommand.java index f65a18e5..130d5909 100644 --- a/src/main/java/baritone/utils/command/defaults/ChestsCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ChestsCommand.java @@ -23,7 +23,7 @@ import baritone.api.utils.BetterBlockPos; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.item.ItemStack; import net.minecraft.util.math.BlockPos; import net.minecraft.util.text.ITextComponent; @@ -41,7 +41,7 @@ public class ChestsCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); Set> entries = ctx.worldData().getContainerMemory().getRememberedInventories().entrySet(); @@ -62,7 +62,7 @@ public class ChestsCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/ClickCommand.java b/src/main/java/baritone/utils/command/defaults/ClickCommand.java index 07bd4de9..ba8e917c 100644 --- a/src/main/java/baritone/utils/command/defaults/ClickCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ClickCommand.java @@ -20,7 +20,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -33,14 +33,14 @@ public class ClickCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); baritone.openClick(); logDirect("aight dude"); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/ComeCommand.java b/src/main/java/baritone/utils/command/defaults/ComeCommand.java index 52a0cec6..cc4f4071 100644 --- a/src/main/java/baritone/utils/command/defaults/ComeCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ComeCommand.java @@ -22,7 +22,7 @@ import baritone.api.pathing.goals.GoalBlock; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.entity.Entity; import net.minecraft.util.math.BlockPos; @@ -37,7 +37,7 @@ public class ComeCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); Entity entity = mc.getRenderViewEntity(); if (entity == null) { @@ -48,7 +48,7 @@ public class ComeCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/CommandAlias.java b/src/main/java/baritone/utils/command/defaults/CommandAlias.java index 0f28e48e..6aa32af1 100644 --- a/src/main/java/baritone/utils/command/defaults/CommandAlias.java +++ b/src/main/java/baritone/utils/command/defaults/CommandAlias.java @@ -19,7 +19,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Collections; import java.util.List; @@ -43,12 +43,12 @@ public class CommandAlias extends Command { } @Override - public void execute(String label, ArgConsumer args) { + public void execute(String label, IArgConsumer args) { this.baritone.getCommandManager().execute(String.format("%s %s", target, args.rawRest())); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return this.baritone.getCommandManager().tabComplete(String.format("%s %s", target, args.rawRest())); } diff --git a/src/main/java/baritone/utils/command/defaults/ExploreCommand.java b/src/main/java/baritone/utils/command/defaults/ExploreCommand.java index 55e3d756..15a8e241 100644 --- a/src/main/java/baritone/utils/command/defaults/ExploreCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ExploreCommand.java @@ -22,7 +22,7 @@ import baritone.api.pathing.goals.GoalXZ; import baritone.api.utils.command.Command; import baritone.api.utils.command.datatypes.RelativeGoalXZ; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -35,7 +35,7 @@ public class ExploreCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { if (args.hasAny()) { args.requireExactly(2); } else { @@ -49,7 +49,7 @@ public class ExploreCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { if (args.hasAtMost(2)) { return args.tabCompleteDatatype(RelativeGoalXZ.INSTANCE); } diff --git a/src/main/java/baritone/utils/command/defaults/ExploreFilterCommand.java b/src/main/java/baritone/utils/command/defaults/ExploreFilterCommand.java index 29f34c86..6f60a55d 100644 --- a/src/main/java/baritone/utils/command/defaults/ExploreFilterCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ExploreFilterCommand.java @@ -23,7 +23,7 @@ import baritone.api.utils.command.datatypes.RelativeFile; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; import baritone.api.utils.command.exception.CommandInvalidTypeException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import com.google.gson.JsonSyntaxException; import java.io.File; @@ -39,7 +39,7 @@ public class ExploreFilterCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(2); File file = args.getDatatypePost(RelativeFile.INSTANCE, mc.gameDir.getAbsoluteFile().getParentFile()); boolean invert = false; @@ -63,7 +63,7 @@ public class ExploreFilterCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { if (args.hasExactlyOne()) { return RelativeFile.tabComplete(args, RelativeFile.gameDir()); } diff --git a/src/main/java/baritone/utils/command/defaults/FarmCommand.java b/src/main/java/baritone/utils/command/defaults/FarmCommand.java index a9dc7a8b..0ece390d 100644 --- a/src/main/java/baritone/utils/command/defaults/FarmCommand.java +++ b/src/main/java/baritone/utils/command/defaults/FarmCommand.java @@ -20,7 +20,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -33,14 +33,14 @@ public class FarmCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); baritone.getFarmProcess().farm(); logDirect("Farming"); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/FindCommand.java b/src/main/java/baritone/utils/command/defaults/FindCommand.java index 513d6539..62a49e02 100644 --- a/src/main/java/baritone/utils/command/defaults/FindCommand.java +++ b/src/main/java/baritone/utils/command/defaults/FindCommand.java @@ -22,7 +22,7 @@ import baritone.api.utils.BetterBlockPos; import baritone.api.utils.command.Command; import baritone.api.utils.command.datatypes.BlockById; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import net.minecraft.block.Block; import java.util.ArrayList; @@ -37,7 +37,7 @@ public class FindCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { List toFind = new ArrayList<>(); while (args.hasAny()) { toFind.add(args.getDatatypeFor(BlockById.INSTANCE)); @@ -59,7 +59,7 @@ public class FindCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return args.tabCompleteDatatype(BlockById.INSTANCE); } diff --git a/src/main/java/baritone/utils/command/defaults/FollowCommand.java b/src/main/java/baritone/utils/command/defaults/FollowCommand.java index d2e674c9..b1cb5a80 100644 --- a/src/main/java/baritone/utils/command/defaults/FollowCommand.java +++ b/src/main/java/baritone/utils/command/defaults/FollowCommand.java @@ -23,7 +23,7 @@ import baritone.api.utils.command.datatypes.EntityClassById; import baritone.api.utils.command.datatypes.IDatatypeFor; import baritone.api.utils.command.datatypes.NearbyPlayer; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityList; @@ -42,7 +42,7 @@ public class FollowCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMin(1); FollowGroup group; FollowList list; @@ -88,7 +88,7 @@ public class FollowCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { if (args.hasExactlyOne()) { return new TabCompleteHelper() .append(FollowGroup.class) diff --git a/src/main/java/baritone/utils/command/defaults/ForceCancelCommand.java b/src/main/java/baritone/utils/command/defaults/ForceCancelCommand.java index 05b62992..7a020b14 100644 --- a/src/main/java/baritone/utils/command/defaults/ForceCancelCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ForceCancelCommand.java @@ -21,7 +21,7 @@ import baritone.api.IBaritone; import baritone.api.behavior.IPathingBehavior; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -34,7 +34,7 @@ public class ForceCancelCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); IPathingBehavior pathingBehavior = baritone.getPathingBehavior(); pathingBehavior.cancelEverything(); @@ -43,7 +43,7 @@ public class ForceCancelCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/GcCommand.java b/src/main/java/baritone/utils/command/defaults/GcCommand.java index 068b5136..20b2e334 100644 --- a/src/main/java/baritone/utils/command/defaults/GcCommand.java +++ b/src/main/java/baritone/utils/command/defaults/GcCommand.java @@ -20,7 +20,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -33,14 +33,14 @@ public class GcCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); System.gc(); logDirect("ok called System.gc()"); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/GoalCommand.java b/src/main/java/baritone/utils/command/defaults/GoalCommand.java index baac24b6..f2692fb2 100644 --- a/src/main/java/baritone/utils/command/defaults/GoalCommand.java +++ b/src/main/java/baritone/utils/command/defaults/GoalCommand.java @@ -25,7 +25,7 @@ import baritone.api.utils.command.Command; import baritone.api.utils.command.datatypes.RelativeCoordinate; import baritone.api.utils.command.datatypes.RelativeGoal; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import java.util.Arrays; @@ -39,7 +39,7 @@ public class GoalCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { ICustomGoalProcess goalProcess = baritone.getCustomGoalProcess(); if (args.hasAny() && Arrays.asList("reset", "clear", "none").contains(args.peekString())) { args.requireMax(1); @@ -59,7 +59,7 @@ public class GoalCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { TabCompleteHelper helper = new TabCompleteHelper(); if (args.hasExactlyOne()) { helper.append("reset", "clear", "none", "~"); diff --git a/src/main/java/baritone/utils/command/defaults/HelpCommand.java b/src/main/java/baritone/utils/command/defaults/HelpCommand.java index 7db0d893..480207f1 100644 --- a/src/main/java/baritone/utils/command/defaults/HelpCommand.java +++ b/src/main/java/baritone/utils/command/defaults/HelpCommand.java @@ -21,7 +21,7 @@ import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandNotFoundException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.api.utils.command.helpers.pagination.Paginator; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import net.minecraft.util.text.ITextComponent; @@ -44,7 +44,7 @@ public class HelpCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(1); if (!args.hasAny() || args.is(Integer.class)) { Paginator.paginate( @@ -97,7 +97,7 @@ public class HelpCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { if (args.hasExactlyOne()) { return new TabCompleteHelper() .addCommands(this.baritone.getCommandManager()) diff --git a/src/main/java/baritone/utils/command/defaults/InvertCommand.java b/src/main/java/baritone/utils/command/defaults/InvertCommand.java index 232ac0f3..9b5d0a16 100644 --- a/src/main/java/baritone/utils/command/defaults/InvertCommand.java +++ b/src/main/java/baritone/utils/command/defaults/InvertCommand.java @@ -24,7 +24,7 @@ import baritone.api.process.ICustomGoalProcess; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -37,7 +37,7 @@ public class InvertCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess(); Goal goal; @@ -54,7 +54,7 @@ public class InvertCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/MineCommand.java b/src/main/java/baritone/utils/command/defaults/MineCommand.java index da92b098..2a91c6e6 100644 --- a/src/main/java/baritone/utils/command/defaults/MineCommand.java +++ b/src/main/java/baritone/utils/command/defaults/MineCommand.java @@ -23,7 +23,7 @@ import baritone.api.utils.command.Command; import baritone.api.utils.command.datatypes.BlockById; import baritone.api.utils.command.datatypes.ForBlockOptionalMeta; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.cache.WorldScanner; import java.util.ArrayList; @@ -38,7 +38,7 @@ public class MineCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { int quantity = args.getAsOrDefault(Integer.class, 0); args.requireMin(1); List boms = new ArrayList<>(); @@ -51,7 +51,7 @@ public class MineCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return args.tabCompleteDatatype(BlockById.INSTANCE); } diff --git a/src/main/java/baritone/utils/command/defaults/PathCommand.java b/src/main/java/baritone/utils/command/defaults/PathCommand.java index 6de33373..e52a93a4 100644 --- a/src/main/java/baritone/utils/command/defaults/PathCommand.java +++ b/src/main/java/baritone/utils/command/defaults/PathCommand.java @@ -25,7 +25,7 @@ import baritone.api.utils.command.datatypes.RelativeCoordinate; import baritone.api.utils.command.datatypes.RelativeGoal; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.cache.WorldScanner; @@ -40,7 +40,7 @@ public class PathCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess(); Goal goal; if (args.hasAny()) { @@ -56,7 +56,7 @@ public class PathCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { if (args.hasAny() && !args.has(4)) { while (args.has(2)) { if (args.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) == null) { diff --git a/src/main/java/baritone/utils/command/defaults/PauseResumeCommands.java b/src/main/java/baritone/utils/command/defaults/PauseResumeCommands.java index 2c363ff4..08a0ae66 100644 --- a/src/main/java/baritone/utils/command/defaults/PauseResumeCommands.java +++ b/src/main/java/baritone/utils/command/defaults/PauseResumeCommands.java @@ -24,7 +24,7 @@ import baritone.api.process.PathingCommandType; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -79,7 +79,7 @@ public class PauseResumeCommands { ); pauseCommand = new Command(baritone, "pause") { @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); if (paused[0]) { throw new CommandInvalidStateException("Already paused"); @@ -89,7 +89,7 @@ public class PauseResumeCommands { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } @@ -112,7 +112,7 @@ public class PauseResumeCommands { }; resumeCommand = new Command(baritone, "resume") { @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); if (!paused[0]) { throw new CommandInvalidStateException("Not paused"); @@ -122,7 +122,7 @@ public class PauseResumeCommands { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } @@ -143,13 +143,13 @@ public class PauseResumeCommands { }; pausedCommand = new Command(baritone, "paused") { @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); logDirect(String.format("Baritone is %spaused", paused[0] ? "" : "not ")); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/ProcCommand.java b/src/main/java/baritone/utils/command/defaults/ProcCommand.java index 232a53a2..233c18a3 100644 --- a/src/main/java/baritone/utils/command/defaults/ProcCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ProcCommand.java @@ -24,7 +24,7 @@ import baritone.api.process.PathingCommand; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -37,7 +37,7 @@ public class ProcCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); IPathingControlManager pathingControlManager = baritone.getPathingControlManager(); IBaritoneProcess process = pathingControlManager.mostRecentInControl().orElse(null); @@ -62,7 +62,7 @@ public class ProcCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/ReloadAllCommand.java b/src/main/java/baritone/utils/command/defaults/ReloadAllCommand.java index f13af77d..6128094c 100644 --- a/src/main/java/baritone/utils/command/defaults/ReloadAllCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ReloadAllCommand.java @@ -20,7 +20,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -33,14 +33,14 @@ public class ReloadAllCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); ctx.worldData().getCachedWorld().reloadAllFromDisk(); logDirect("Reloaded"); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/RenderCommand.java b/src/main/java/baritone/utils/command/defaults/RenderCommand.java index 710df10f..15473afa 100644 --- a/src/main/java/baritone/utils/command/defaults/RenderCommand.java +++ b/src/main/java/baritone/utils/command/defaults/RenderCommand.java @@ -21,7 +21,7 @@ import baritone.api.IBaritone; import baritone.api.utils.BetterBlockPos; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -34,7 +34,7 @@ public class RenderCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); BetterBlockPos origin = ctx.playerFeet(); int renderDistance = (mc.gameSettings.renderDistanceChunks + 1) * 16; @@ -50,7 +50,7 @@ public class RenderCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/RepackCommand.java b/src/main/java/baritone/utils/command/defaults/RepackCommand.java index 2f1a988d..1f11802a 100644 --- a/src/main/java/baritone/utils/command/defaults/RepackCommand.java +++ b/src/main/java/baritone/utils/command/defaults/RepackCommand.java @@ -20,7 +20,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.cache.WorldScanner; import java.util.Arrays; @@ -34,13 +34,13 @@ public class RepackCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); logDirect(String.format("Queued %d chunks for repacking", WorldScanner.INSTANCE.repack(ctx))); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/SaveAllCommand.java b/src/main/java/baritone/utils/command/defaults/SaveAllCommand.java index 21dbfb64..9c507f69 100644 --- a/src/main/java/baritone/utils/command/defaults/SaveAllCommand.java +++ b/src/main/java/baritone/utils/command/defaults/SaveAllCommand.java @@ -20,7 +20,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -33,14 +33,14 @@ public class SaveAllCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); ctx.worldData().getCachedWorld().save(); logDirect("Saved"); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/SchematicaCommand.java b/src/main/java/baritone/utils/command/defaults/SchematicaCommand.java index ed695169..c531a48b 100644 --- a/src/main/java/baritone/utils/command/defaults/SchematicaCommand.java +++ b/src/main/java/baritone/utils/command/defaults/SchematicaCommand.java @@ -20,7 +20,7 @@ package baritone.utils.command.defaults; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -33,13 +33,13 @@ public class SchematicaCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); baritone.getBuilderProcess().buildOpenSchematic(); } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/SelCommand.java b/src/main/java/baritone/utils/command/defaults/SelCommand.java index 8e2512d1..83fc77e3 100644 --- a/src/main/java/baritone/utils/command/defaults/SelCommand.java +++ b/src/main/java/baritone/utils/command/defaults/SelCommand.java @@ -35,7 +35,7 @@ import baritone.api.utils.command.datatypes.RelativeBlockPos; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; import baritone.api.utils.command.exception.CommandInvalidTypeException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.utils.IRenderer; import net.minecraft.init.Blocks; @@ -75,7 +75,7 @@ public class SelCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { Action action = Action.getByName(args.getString()); if (action == null) { throw new CommandInvalidTypeException(args.consumed(), "an action"); @@ -186,7 +186,7 @@ public class SelCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { if (args.hasExactlyOne()) { return new TabCompleteHelper() .append(Action.getAllNames()) diff --git a/src/main/java/baritone/utils/command/defaults/SetCommand.java b/src/main/java/baritone/utils/command/defaults/SetCommand.java index 8add3fed..be07a7ae 100644 --- a/src/main/java/baritone/utils/command/defaults/SetCommand.java +++ b/src/main/java/baritone/utils/command/defaults/SetCommand.java @@ -24,7 +24,7 @@ import baritone.api.utils.SettingsUtil; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidTypeException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.api.utils.command.helpers.pagination.Paginator; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import net.minecraft.util.text.ITextComponent; @@ -50,7 +50,7 @@ public class SetCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { String arg = args.hasAny() ? args.getString().toLowerCase(Locale.US) : "list"; if (Arrays.asList("s", "save").contains(arg)) { SettingsUtil.save(Baritone.settings()); @@ -186,7 +186,7 @@ public class SetCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { if (args.hasAny()) { String arg = args.getString(); if (args.hasExactlyOne() && !Arrays.asList("s", "save").contains(args.peekString().toLowerCase(Locale.US))) { diff --git a/src/main/java/baritone/utils/command/defaults/ThisWayCommand.java b/src/main/java/baritone/utils/command/defaults/ThisWayCommand.java index d11346cc..bef3a513 100644 --- a/src/main/java/baritone/utils/command/defaults/ThisWayCommand.java +++ b/src/main/java/baritone/utils/command/defaults/ThisWayCommand.java @@ -21,7 +21,7 @@ import baritone.api.IBaritone; import baritone.api.pathing.goals.GoalXZ; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -34,7 +34,7 @@ public class ThisWayCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireExactly(1); GoalXZ goal = GoalXZ.fromDirection( ctx.playerFeetAsVec(), @@ -46,7 +46,7 @@ public class ThisWayCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/TunnelCommand.java b/src/main/java/baritone/utils/command/defaults/TunnelCommand.java index b7e00fe6..1b9066f9 100644 --- a/src/main/java/baritone/utils/command/defaults/TunnelCommand.java +++ b/src/main/java/baritone/utils/command/defaults/TunnelCommand.java @@ -22,7 +22,7 @@ import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.GoalStrictDirection; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -35,7 +35,7 @@ public class TunnelCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); Goal goal = new GoalStrictDirection( ctx.playerFeet(), @@ -46,7 +46,7 @@ public class TunnelCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/VersionCommand.java b/src/main/java/baritone/utils/command/defaults/VersionCommand.java index 89dc6576..4c9aa850 100644 --- a/src/main/java/baritone/utils/command/defaults/VersionCommand.java +++ b/src/main/java/baritone/utils/command/defaults/VersionCommand.java @@ -21,7 +21,7 @@ import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import java.util.Arrays; import java.util.List; @@ -34,7 +34,7 @@ public class VersionCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { args.requireMax(0); String version = getClass().getPackage().getImplementationVersion(); if (version == null) { @@ -45,7 +45,7 @@ public class VersionCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) { + public Stream tabComplete(String label, IArgConsumer args) { return Stream.empty(); } diff --git a/src/main/java/baritone/utils/command/defaults/WaypointsCommand.java b/src/main/java/baritone/utils/command/defaults/WaypointsCommand.java index 45bd7b50..bda39e2a 100644 --- a/src/main/java/baritone/utils/command/defaults/WaypointsCommand.java +++ b/src/main/java/baritone/utils/command/defaults/WaypointsCommand.java @@ -29,7 +29,7 @@ import baritone.api.utils.command.datatypes.RelativeBlockPos; import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.exception.CommandInvalidStateException; import baritone.api.utils.command.exception.CommandInvalidTypeException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; import baritone.api.utils.command.helpers.pagination.Paginator; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import net.minecraft.util.text.ITextComponent; @@ -52,7 +52,7 @@ public class WaypointsCommand extends Command { } @Override - public void execute(String label, ArgConsumer args) throws CommandException { + public void execute(String label, IArgConsumer args) throws CommandException { Action action = args.hasAny() ? Action.getByName(args.getString()) : Action.LIST; if (action == null) { throw new CommandInvalidTypeException(args.consumed(), "an action"); @@ -241,7 +241,7 @@ public class WaypointsCommand extends Command { } @Override - public Stream tabComplete(String label, ArgConsumer args) throws CommandException { + public Stream tabComplete(String label, IArgConsumer args) throws CommandException { if (args.hasAny()) { if (args.hasExactlyOne()) { return new TabCompleteHelper() diff --git a/src/main/java/baritone/utils/command/helpers/arguments/ArgConsumer.java b/src/main/java/baritone/utils/command/helpers/arguments/ArgConsumer.java new file mode 100644 index 00000000..06aaa6eb --- /dev/null +++ b/src/main/java/baritone/utils/command/helpers/arguments/ArgConsumer.java @@ -0,0 +1,444 @@ +/* + * 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.command.helpers.arguments; + +import baritone.api.IBaritone; +import baritone.api.utils.command.argument.ICommandArgument; +import baritone.api.utils.command.datatypes.IDatatype; +import baritone.api.utils.command.datatypes.IDatatypeContext; +import baritone.api.utils.command.datatypes.IDatatypeFor; +import baritone.api.utils.command.datatypes.IDatatypePost; +import baritone.api.utils.command.exception.CommandException; +import baritone.api.utils.command.exception.CommandInvalidTypeException; +import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException; +import baritone.api.utils.command.exception.CommandTooManyArgumentsException; +import baritone.api.utils.command.helpers.arguments.IArgConsumer; +import baritone.api.utils.command.manager.ICommandManager; +import baritone.utils.command.argument.CommandArguments; + +import java.util.ArrayList; +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Stream; + +public class ArgConsumer implements IArgConsumer { + + /** + * The parent {@link ICommandManager} for this {@link IArgConsumer}}. Used by {@link #context}. + */ + private final ICommandManager manager; + + /** + * The {@link IDatatypeContext} instance for this {@link IArgConsumer}}, passed to + * datatypes when an operation is performed upon them. + * + * @see IDatatype + * @see IDatatypeContext + */ + private final IDatatypeContext context; + + /** + * The list of arguments in this ArgConsumer + */ + private final LinkedList args; + + /** + * The list of consumed arguments for this ArgConsumer. The most recently consumed argument is the last one + */ + private final Deque consumed; + + private ArgConsumer(ICommandManager manager, Deque args, Deque consumed) { + this.manager = manager; + this.context = this.new Context(); + this.args = new LinkedList<>(args); + this.consumed = new LinkedList<>(consumed); + } + + public ArgConsumer(ICommandManager manager, List args) { + this(manager, new LinkedList<>(args), new LinkedList<>()); + } + + @Override + public LinkedList getArgs() { + return this.args; + } + + @Override + public Deque getConsumed() { + return this.consumed; + } + + @Override + public boolean has(int num) { + return args.size() >= num; + } + + @Override + public boolean hasAny() { + return has(1); + } + + @Override + public boolean hasAtMost(int num) { + return args.size() <= num; + } + + @Override + public boolean hasAtMostOne() { + return hasAtMost(1); + } + + @Override + public boolean hasExactly(int num) { + return args.size() == num; + } + + @Override + public boolean hasExactlyOne() { + return hasExactly(1); + } + + @Override + public ICommandArgument peek(int index) throws CommandNotEnoughArgumentsException { + requireMin(index + 1); + return args.get(index); + } + + @Override + public ICommandArgument peek() throws CommandNotEnoughArgumentsException { + return peek(0); + } + + @Override + public boolean is(Class type, int index) throws CommandNotEnoughArgumentsException { + return peek(index).is(type); + } + + @Override + public boolean is(Class type) throws CommandNotEnoughArgumentsException { + return is(type, 0); + } + + @Override + public String peekString(int index) throws CommandNotEnoughArgumentsException { + return peek(index).getValue(); + } + + @Override + public String peekString() throws CommandNotEnoughArgumentsException { + return peekString(0); + } + + @Override + public > E peekEnum(Class enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return peek(index).getEnum(enumClass); + } + + @Override + public > E peekEnum(Class enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return peekEnum(enumClass, 0); + } + + @Override + public > E peekEnumOrNull(Class enumClass, int index) throws CommandNotEnoughArgumentsException { + try { + return peekEnum(enumClass, index); + } catch (CommandInvalidTypeException e) { + return null; + } + } + + @Override + public > E peekEnumOrNull(Class enumClass) throws CommandNotEnoughArgumentsException { + return peekEnumOrNull(enumClass, 0); + } + + @Override + public T peekAs(Class type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return peek(index).getAs(type); + } + + @Override + public T peekAs(Class type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return peekAs(type, 0); + } + + @Override + public T peekAsOrDefault(Class type, T def, int index) throws CommandNotEnoughArgumentsException { + try { + return peekAs(type, index); + } catch (CommandInvalidTypeException e) { + return def; + } + } + + @Override + public T peekAsOrDefault(Class type, T def) throws CommandNotEnoughArgumentsException { + return peekAsOrDefault(type, def, 0); + } + + @Override + public T peekAsOrNull(Class type, int index) throws CommandNotEnoughArgumentsException { + return peekAsOrDefault(type, null, index); + } + + @Override + public T peekAsOrNull(Class type) throws CommandNotEnoughArgumentsException { + return peekAsOrNull(type, 0); + } + + @Override + public T peekDatatype(IDatatypeFor datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return copy().getDatatypeFor(datatype); + } + + @Override + public T peekDatatype(IDatatypePost datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return this.peekDatatype(datatype, null); + } + + @Override + public T peekDatatype(IDatatypePost datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return copy().getDatatypePost(datatype, original); + } + + @Override + public T peekDatatypeOrNull(IDatatypeFor datatype) { + return copy().getDatatypeForOrNull(datatype); + } + + @Override + public T peekDatatypeOrNull(IDatatypePost datatype) { + return copy().getDatatypePostOrNull(datatype, null); + } + + @Override + public > T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return copy().getDatatypePost(datatype, original); + } + + @Override + public > T peekDatatypePostOrDefault(D datatype, O original, T def) { + return copy().getDatatypePostOrDefault(datatype, original, def); + } + + @Override + public > T peekDatatypePostOrNull(D datatype, O original) { + return peekDatatypePostOrDefault(datatype, original, null); + } + + @Override + public > T peekDatatypeFor(Class datatype) { + return copy().peekDatatypeFor(datatype); + } + + @Override + public > T peekDatatypeForOrDefault(Class datatype, T def) { + return copy().peekDatatypeForOrDefault(datatype, def); + } + + @Override + public > T peekDatatypeForOrNull(Class datatype) { + return peekDatatypeForOrDefault(datatype, null); + } + + @Override + public ICommandArgument get() throws CommandNotEnoughArgumentsException { + requireMin(1); + ICommandArgument arg = args.removeFirst(); + consumed.add(arg); + return arg; + } + + @Override + public String getString() throws CommandNotEnoughArgumentsException { + return get().getValue(); + } + + @Override + public > E getEnum(Class enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return get().getEnum(enumClass); + } + + @Override + public > E getEnumOrDefault(Class enumClass, E def) throws CommandNotEnoughArgumentsException { + try { + peekEnum(enumClass); + return getEnum(enumClass); + } catch (CommandInvalidTypeException e) { + return def; + } + } + + @Override + public > E getEnumOrNull(Class enumClass) throws CommandNotEnoughArgumentsException { + return getEnumOrDefault(enumClass, null); + } + + @Override + public T getAs(Class type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + return get().getAs(type); + } + + @Override + public T getAsOrDefault(Class type, T def) throws CommandNotEnoughArgumentsException { + try { + T val = peek().getAs(type); + get(); + return val; + } catch (CommandInvalidTypeException e) { + return def; + } + } + + @Override + public T getAsOrNull(Class type) throws CommandNotEnoughArgumentsException { + return getAsOrDefault(type, null); + } + + @Override + public > T getDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + try { + return datatype.apply(this.context, original); + } catch (Exception e) { + e.printStackTrace(); + throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getClass().getSimpleName()); + } + } + + @Override + public > T getDatatypePostOrDefault(D datatype, O original, T _default) { + final List argsSnapshot = new ArrayList<>(this.args); + final List consumedSnapshot = new ArrayList<>(this.consumed); + try { + return this.getDatatypePost(datatype, original); + } catch (Exception e) { + this.args.clear(); + this.args.addAll(argsSnapshot); + this.consumed.clear(); + this.consumed.addAll(consumedSnapshot); + return _default; + } + } + + @Override + public > T getDatatypePostOrNull(D datatype, O original) { + return this.getDatatypePostOrDefault(datatype, original, null); + } + + @Override + public > T getDatatypeFor(D datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException { + try { + return datatype.get(this.context); + } catch (Exception e) { + throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getClass().getSimpleName()); + } + } + + @Override + public > T getDatatypeForOrDefault(D datatype, T def) { + final List argsSnapshot = new ArrayList<>(this.args); + final List consumedSnapshot = new ArrayList<>(this.consumed); + try { + return this.getDatatypeFor(datatype); + } catch (Exception e) { + this.args.clear(); + this.args.addAll(argsSnapshot); + this.consumed.clear(); + this.consumed.addAll(consumedSnapshot); + return def; + } + } + + @Override + public > T getDatatypeForOrNull(D datatype) { + return this.getDatatypeForOrDefault(datatype, null); + } + + @Override + public Stream tabCompleteDatatype(T datatype) { + try { + return datatype.tabComplete(this.context); + } catch (Exception e) { + e.printStackTrace(); + } + return Stream.empty(); + } + + @Override + public String rawRest() { + return args.size() > 0 ? args.getFirst().getRawRest() : ""; + } + + @Override + public void requireMin(int min) throws CommandNotEnoughArgumentsException { + if (args.size() < min) { + throw new CommandNotEnoughArgumentsException(min + consumed.size()); + } + } + + @Override + public void requireMax(int max) throws CommandTooManyArgumentsException { + if (args.size() > max) { + throw new CommandTooManyArgumentsException(max + consumed.size()); + } + } + + @Override + public void requireExactly(int args) throws CommandException { + requireMin(args); + requireMax(args); + } + + @Override + public boolean hasConsumed() { + return !consumed.isEmpty(); + } + + @Override + public ICommandArgument consumed() { + return consumed.size() > 0 ? consumed.getLast() : CommandArguments.unknown(); + } + + @Override + public String consumedString() { + return consumed().getValue(); + } + + @Override + public ArgConsumer copy() { + return new ArgConsumer(manager, args, consumed); + } + + /** + * Implementation of {@link IDatatypeContext} which adapts to the parent {@link IArgConsumer}} + */ + private final class Context implements IDatatypeContext { + + @Override + public final IBaritone getBaritone() { + return ArgConsumer.this.manager.getBaritone(); + } + + @Override + public final ArgConsumer getConsumer() { + return ArgConsumer.this; + } + } +} diff --git a/src/main/java/baritone/utils/command/manager/CommandManager.java b/src/main/java/baritone/utils/command/manager/CommandManager.java index c9c51eb1..290b3852 100644 --- a/src/main/java/baritone/utils/command/manager/CommandManager.java +++ b/src/main/java/baritone/utils/command/manager/CommandManager.java @@ -20,13 +20,14 @@ package baritone.utils.command.manager; import baritone.Baritone; import baritone.api.IBaritone; import baritone.api.utils.command.Command; -import baritone.api.utils.command.argument.CommandArgument; +import baritone.api.utils.command.argument.ICommandArgument; import baritone.api.utils.command.exception.CommandUnhandledException; import baritone.api.utils.command.exception.ICommandException; -import baritone.api.utils.command.helpers.arguments.ArgConsumer; +import baritone.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.manager.ICommandManager; import baritone.api.utils.command.registry.Registry; +import baritone.utils.command.argument.CommandArguments; import baritone.utils.command.defaults.DefaultCommands; import net.minecraft.util.Tuple; @@ -76,7 +77,7 @@ public class CommandManager implements ICommandManager { } @Override - public boolean execute(Tuple> expanded) { + public boolean execute(Tuple> expanded) { ExecutionWrapper execution = this.from(expanded); if (execution != null) { execution.execute(); @@ -85,16 +86,16 @@ public class CommandManager implements ICommandManager { } @Override - public Stream tabComplete(Tuple> expanded) { + public Stream tabComplete(Tuple> expanded) { ExecutionWrapper execution = this.from(expanded); return execution == null ? Stream.empty() : execution.tabComplete(); } @Override public Stream tabComplete(String prefix) { - Tuple> pair = expand(prefix, true); + Tuple> pair = expand(prefix, true); String label = pair.getFirst(); - List args = pair.getSecond(); + List args = pair.getSecond(); if (args.isEmpty()) { return new TabCompleteHelper() .addCommands(this.baritone.getCommandManager()) @@ -105,7 +106,7 @@ public class CommandManager implements ICommandManager { } } - private ExecutionWrapper from(Tuple> expanded) { + private ExecutionWrapper from(Tuple> expanded) { String label = expanded.getFirst(); ArgConsumer args = new ArgConsumer(this, expanded.getSecond()); @@ -113,13 +114,13 @@ public class CommandManager implements ICommandManager { return command == null ? null : new ExecutionWrapper(command, label, args); } - private static Tuple> expand(String string, boolean preserveEmptyLast) { + private static Tuple> expand(String string, boolean preserveEmptyLast) { String label = string.split("\\s", 2)[0]; - List args = CommandArgument.from(string.substring(label.length()), preserveEmptyLast); + List args = CommandArguments.from(string.substring(label.length()), preserveEmptyLast); return new Tuple<>(label, args); } - public static Tuple> expand(String string) { + public static Tuple> expand(String string) { return expand(string, false); } @@ -143,7 +144,7 @@ public class CommandManager implements ICommandManager { ? (ICommandException) t : new CommandUnhandledException(t); - exception.handle(command, args.args); + exception.handle(command, args.getArgs()); } }