ArgConsumer API separation

This commit is contained in:
Brady 2019-10-04 08:55:02 -05:00
parent 089c5fb095
commit 3a3d880d81
No known key found for this signature in database
GPG Key ID: 73A788379A197567
62 changed files with 1025 additions and 696 deletions

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException;
public abstract Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException;
/**
* @return A <b>single-line</b> string containing a short description of this command's purpose.

View File

@ -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> T parseStateless(Class<T> type, CommandArgument arg) throws CommandInvalidTypeException {
public static <T> T parseStateless(Class<T> type, ICommandArgument arg) throws CommandInvalidTypeException {
IArgParser.Stateless<T> 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, S> T parseStated(Class<T> type, Class<S> stateKlass, CommandArgument arg, S state) throws CommandInvalidTypeException {
public static <T, S> T parseStated(Class<T> type, Class<S> stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException {
IArgParser.Stated<T, S> parser = getParserStated(type, stateKlass);
if (parser == null) {
throw new CommandNoParserForTypeException(type);

View File

@ -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))) {

View File

@ -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<T> {
@ -27,7 +27,7 @@ public interface IArgParser<T> {
Class<T> 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<T> {
* @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<T> {
* @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;
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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
* <p>
* 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
* <p>
* 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 extends Enum<?>> E getEnum(Class<E> 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 <b>stateless</b> {@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> T getAs(Class<T> type) throws CommandInvalidTypeException {
return ArgParserManager.parseStateless(type, this);
}
/**
* Tries to use a <b>stateless</b> {@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 <T> boolean is(Class<T> type) {
try {
getAs(type);
return true;
} catch (Throwable t) {
return false;
}
}
/**
* Tries to use a <b>stated</b> {@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, S> T getAs(Class<T> type, Class<S> stateType, S state) throws CommandInvalidTypeException {
return ArgParserManager.parseStated(type, stateType, this, state);
}
/**
* Tries to use a <b>stated</b> {@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 <T, S> boolean is(Class<T> type, Class<S> 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<CommandArgument> from(String string, boolean preserveEmptyLast) {
List<CommandArgument> 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<CommandArgument> 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, "<unknown>", "");
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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
* <p>
* 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
* <p>
* 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 extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException;
/**
* Tries to use a <b>stateless</b> {@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> T getAs(Class<T> type) throws CommandInvalidTypeException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return If the parser succeeded
*/
<T> boolean is(Class<T> type);
/**
* Tries to use a <b>stated</b> {@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, S> T getAs(Class<T> type, Class<S> stateType, S state) throws CommandInvalidTypeException;
/**
* Tries to use a <b>stated</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return If the parser succeeded
*/
<T, S> boolean is(Class<T> type, Class<S> stateType, S state);
}

View File

@ -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.
* <p>
* 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<String> tabComplete(IDatatypeContext ctx) throws CommandException;
}

View File

@ -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();
}

View File

@ -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<BetterBlockPos, BetterBloc
origin = BetterBlockPos.ORIGIN;
}
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
return new BetterBlockPos(
consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.x),
consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.y),
@ -42,7 +42,7 @@ public enum RelativeBlockPos implements IDatatypePost<BetterBlockPos, BetterBloc
@Override
public Stream<String> 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) {

View File

@ -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<Double, Double> {
@Override
public Stream<String> 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("~");
}

View File

@ -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<File, File> {
}
}
public static Stream<String> tabComplete(ArgConsumer consumer, File base0) throws CommandException {
public static Stream<String> 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

View File

@ -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<Goal, BetterBlockPos> {
if (origin == null) {
origin = BetterBlockPos.ORIGIN;
}
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
List<IDatatypePostFunction<Double, Double>> 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));

View File

@ -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<GoalBlock, BetterBlockPos
origin = BetterBlockPos.ORIGIN;
}
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
return new GoalBlock(
MathHelper.floor(consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.x)),
MathHelper.floor(consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.y)),
@ -44,7 +44,7 @@ public enum RelativeGoalBlock implements IDatatypePost<GoalBlock, BetterBlockPos
@Override
public Stream<String> tabComplete(IDatatypeContext ctx) {
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
if (consumer.hasAtMost(3)) {
return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE);
}

View File

@ -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<GoalXZ, BetterBlockPos> {
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<GoalXZ, BetterBlockPos> {
@Override
public Stream<String> tabComplete(IDatatypeContext ctx) {
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
if (consumer.hasAtMost(2)) {
return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE);
}

View File

@ -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<GoalYLevel, BetterBlockP
@Override
public Stream<String> tabComplete(IDatatypeContext ctx) {
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
if (consumer.hasAtMost(1)) {
return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE);
}

View File

@ -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 ? "<unknown>" : Integer.toString(arg.index + 1),
arg.getIndex() == -1 ? "<unknown>" : Integer.toString(arg.getIndex() + 1),
reason
));
this.arg = arg;

View File

@ -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()));
}
}

View File

@ -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<CommandArgument> args) {
public void handle(Command command, List<ICommandArgument> args) {
HELPER.logDirect(getMessage());
}
}

View File

@ -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<CommandArgument> args) {
public void handle(Command command, List<ICommandArgument> 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);

View File

@ -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<CommandArgument> args) {
default void handle(Command command, List<ICommandArgument> args) {
HELPER.logDirect(this.getMessage(), TextFormatting.RED);
}
}

View File

@ -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:
*
* <ul>
* <li>Mutability. The whole concept of the {@link ArgConsumer} is to let you gradually consume arguments in any way
* <li>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.</li>
* <li>You don't need to keep track of your consumption. The {@link ArgConsumer} keeps track of the arguments you
* <li>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()}!</li>
* <li>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.</li>
* <li>It's very easy to throw detailed exceptions. The {@link ArgConsumer} has many different methods that can
* <li>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.</li>
* </ul>
*/
public class ArgConsumer {
public interface IArgConsumer {
/**
* The parent {@link ICommandManager} for this {@link ArgConsumer}. Used by {@link #context}.
*/
private final ICommandManager manager;
LinkedList<ICommandArgument> 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<CommandArgument> args;
/**
* The list of consumed arguments for this ArgConsumer. The most recently consumed argument is the last one
*/
public final Deque<CommandArgument> consumed;
private ArgConsumer(ICommandManager manager, Deque<CommandArgument> args, Deque<CommandArgument> consumed) {
this.manager = manager;
this.context = this.new Context();
this.args = new LinkedList<>(args);
this.consumed = new LinkedList<>(consumed);
}
public ArgConsumer(ICommandManager manager, List<CommandArgument> args) {
this(manager, new LinkedList<>(args), new LinkedList<>());
}
Deque<ICommandArgument> getConsumed();
/**
* @param num The number of arguments to check for
* @return {@code true} if there are <i>at least</i> {@code num} arguments left in this {@link ArgConsumer}
* @return {@code true} if there are <i>at least</i> {@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 <i>at least</i> 1 argument left in this {@link ArgConsumer}
* @return {@code true} if there is <i>at least</i> 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 <i>at most</i> {@code num} arguments left in this {@link ArgConsumer}
* @return {@code true} if there are <i>at most</i> {@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 <i>at most</i> 1 argument left in this {@link ArgConsumer}
* @return {@code true} if there is <i>at most</i> 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 <i>exactly</i> {@code num} arguments left in this {@link ArgConsumer}
* @return {@code true} if there are <i>exactly</i> {@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 <i>exactly</i> 1 argument left in this {@link ArgConsumer}
* @return {@code true} if there is <i>exactly</i> 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 extends Enum<?>> E peekEnum(Class<E> enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getEnum(enumClass);
}
<E extends Enum<?>> E peekEnum(Class<E> 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 extends Enum<?>> E peekEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekEnum(enumClass, 0);
}
<E extends Enum<?>> E peekEnum(Class<E> 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 extends Enum<?>> E peekEnumOrNull(Class<E> enumClass, int index) throws CommandNotEnoughArgumentsException {
try {
return peekEnum(enumClass, index);
} catch (CommandInvalidTypeException e) {
return null;
}
}
<E extends Enum<?>> E peekEnumOrNull(Class<E> 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 extends Enum<?>> E peekEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return peekEnumOrNull(enumClass, 0);
}
<E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the argument at the specified index into the specified
@ -292,7 +219,7 @@ public class ArgConsumer {
* <p>
* 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> T peekAs(Class<T> type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getAs(type);
}
<T> T peekAs(Class<T> type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* 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> T peekAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekAs(type, 0);
}
<T> T peekAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the argument at the specified index into the specified
@ -332,7 +255,7 @@ public class ArgConsumer {
* <p>
* 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> T peekAsOrDefault(Class<T> type, T def, int index) throws CommandNotEnoughArgumentsException {
try {
return peekAs(type, index);
} catch (CommandInvalidTypeException e) {
return def;
}
}
<T> T peekAsOrDefault(Class<T> type, T def, int index) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* 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> T peekAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, def, 0);
}
<T> T peekAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the argument at the specified index into the specified
@ -376,7 +291,7 @@ public class ArgConsumer {
* <p>
* 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> T peekAsOrNull(Class<T> type, int index) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, null, index);
}
<T> T peekAsOrNull(Class<T> type, int index) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* 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> T peekAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return peekAsOrNull(type, 0);
}
<T> T peekAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException;
public <T> T peekDatatype(IDatatypeFor<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypeFor(datatype);
}
<T> T peekDatatype(IDatatypeFor<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, O> T peekDatatype(IDatatypePost<T, O> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return this.peekDatatype(datatype, null);
}
<T, O> T peekDatatype(IDatatypePost<T, O> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, O> T peekDatatype(IDatatypePost<T, O> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original);
}
<T, O> T peekDatatype(IDatatypePost<T, O> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T> T peekDatatypeOrNull(IDatatypeFor<T> datatype) {
return copy().getDatatypeForOrNull(datatype);
}
<T> T peekDatatypeOrNull(IDatatypeFor<T> datatype);
public <T, O> T peekDatatypeOrNull(IDatatypePost<T, O> datatype) {
return copy().getDatatypePostOrNull(datatype, null);
}
<T, O> T peekDatatypeOrNull(IDatatypePost<T, O> datatype);
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original);
}
<T, O, D extends IDatatypePost<T, O>> T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrDefault(D datatype, O original, T def) {
return copy().getDatatypePostOrDefault(datatype, original, def);
}
<T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrDefault(D datatype, O original, T def);
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrNull(D datatype, O original) {
return peekDatatypePostOrDefault(datatype, original, null);
}
<T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrNull(D datatype, O original);
/**
* Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer
* <p>
* 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}}.
* <p>
* 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, D extends IDatatypeFor<T>> T peekDatatypeFor(Class<D> datatype) {
return copy().peekDatatypeFor(datatype);
}
<T, D extends IDatatypeFor<T>> T peekDatatypeFor(Class<D> datatype);
/**
* Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer
* <p>
* 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}}.
* <p>
* 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, D extends IDatatypeFor<T>> T peekDatatypeForOrDefault(Class<D> datatype, T def) {
return copy().peekDatatypeForOrDefault(datatype, def);
}
<T, D extends IDatatypeFor<T>> T peekDatatypeForOrDefault(Class<D> datatype, T def);
/**
* Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer
* <p>
* 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}}.
* <p>
* 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, D extends IDatatypeFor<T>> T peekDatatypeForOrNull(Class<D> datatype) {
return peekDatatypeForOrDefault(datatype, null);
}
<T, D extends IDatatypeFor<T>> T peekDatatypeForOrNull(Class<D> 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 extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getEnum(enumClass);
}
<E extends Enum<?>> E getEnum(Class<E> 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 extends Enum<?>> E getEnumOrDefault(Class<E> enumClass, E def) throws CommandNotEnoughArgumentsException {
try {
peekEnum(enumClass);
return getEnum(enumClass);
} catch (CommandInvalidTypeException e) {
return def;
}
}
<E extends Enum<?>> E getEnumOrDefault(Class<E> 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 extends Enum<?>> E getEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return getEnumOrDefault(enumClass, null);
}
<E extends Enum<?>> E getEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* 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> T getAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getAs(type);
}
<T> T getAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* 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> T getAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
try {
T val = peek().getAs(type);
get();
return val;
} catch (CommandInvalidTypeException e) {
return def;
}
}
<T> T getAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* 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> T getAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return getAsOrDefault(type, null);
}
<T> T getAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException;
public <T, O, D extends IDatatypePost<T, O>> 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, O, D extends IDatatypePost<T, O>> T getDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrDefault(D datatype, O original, T _default) {
final List<CommandArgument> argsSnapshot = new ArrayList<>(this.args);
final List<CommandArgument> 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, O, D extends IDatatypePost<T, O>> T getDatatypePostOrDefault(D datatype, O original, T _default);
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrNull(D datatype, O original) {
return this.getDatatypePostOrDefault(datatype, original, null);
}
<T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrNull(D datatype, O original);
public <T, D extends IDatatypeFor<T>> 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, D extends IDatatypeFor<T>> T getDatatypeFor(D datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, D extends IDatatypeFor<T>> T getDatatypeForOrDefault(D datatype, T def) {
final List<CommandArgument> argsSnapshot = new ArrayList<>(this.args);
final List<CommandArgument> 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, D extends IDatatypeFor<T>> T getDatatypeForOrDefault(D datatype, T def);
public <T, D extends IDatatypeFor<T>> T getDatatypeForOrNull(D datatype) {
return this.getDatatypeForOrDefault(datatype, null);
}
<T, D extends IDatatypeFor<T>> T getDatatypeForOrNull(D datatype);
public <T extends IDatatype> Stream<String> tabCompleteDatatype(T datatype) {
try {
return datatype.tabComplete(this.context);
} catch (Exception e) {
e.printStackTrace();
}
return Stream.empty();
}
<T extends IDatatype> Stream<String> tabCompleteDatatype(T datatype);
/**
* Returns the "raw rest" of the string. For example, from a string <code>arg1 arg2&nbsp;&nbsp;arg3</code>, 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"}:
*
* <ul>
* <li>{@code rawRest()} would return <code>arg1 arg2&nbsp;&nbsp;arg3</code></li>
@ -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();
}

View File

@ -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<E> implements Helper {
display(transform, null);
}
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
int page = 1;
consumer.requireMax(1);
if (consumer.hasAny()) {
@ -127,7 +127,7 @@ public class Paginator<E> implements Helper {
"a valid page (1-%d)",
pagi.getMaxPage()
),
consumer.consumed().value
consumer.consumed().getValue()
);
}
}
@ -138,47 +138,47 @@ public class Paginator<E> implements Helper {
pagi.display(transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, new Paginator<>(elems), pre, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, Arrays.asList(elems), pre, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, pagi, null, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, new Paginator<>(elems), null, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, Arrays.asList(elems), null, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, pagi, pre, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, new Paginator<>(elems), pre, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, Arrays.asList(elems), pre, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, pagi, null, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, new Paginator<>(elems), null, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, Arrays.asList(elems), null, transform, null);
}
}

View File

@ -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)}</li>
* <li>Get the stream using {@link #stream()}</li>
* <li>Pass it up to whatever's calling your tab complete function (i.e.
* {@link ICommandManager#tabComplete(String)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)})</li>
* {@link ICommandManager#tabComplete(String)} or {@link IArgConsumer}#tabCompleteDatatype(IDatatype)})</li>
* </ul>
* <p>
* For advanced users: if you're intercepting {@link TabCompleteEvent}s directly, use {@link #build()} instead for an

View File

@ -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<String, List<CommandArgument>> expanded);
boolean execute(Tuple<String, List<ICommandArgument>> expanded);
Stream<String> tabComplete(Tuple<String, List<CommandArgument>> expanded);
Stream<String> tabComplete(Tuple<String, List<ICommandArgument>> expanded);
Stream<String> tabComplete(String prefix);
}

View File

@ -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<String, List<CommandArgument>> pair = CommandManager.expand(msg);
Tuple<String, List<ICommandArgument>> 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<CommandArgument> args = CommandArgument.from(msg, true);
List<ICommandArgument> args = CommandArguments.from(msg, true);
Stream<String> 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<String> tabComplete(String msg) {
try {
List<CommandArgument> args = CommandArgument.from(msg, true);
List<ICommandArgument> args = CommandArguments.from(msg, true);
ArgConsumer argc = new ArgConsumer(this.manager, args);
if (argc.hasAtMost(2)) {
if (argc.hasExactly(1)) {

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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 extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException {
return Stream.of(enumClass.getEnumConstants())
.filter(e -> e.name().equalsIgnoreCase(value))
.findFirst()
.orElseThrow(() -> new CommandInvalidTypeException(this, enumClass.getSimpleName()));
}
@Override
public <T> T getAs(Class<T> type) throws CommandInvalidTypeException {
return ArgParserManager.parseStateless(type, this);
}
@Override
public <T> boolean is(Class<T> type) {
try {
getAs(type);
return true;
} catch (Throwable t) {
return false;
}
}
@SuppressWarnings("UnusedReturnValue")
@Override
public <T, S> T getAs(Class<T> type, Class<S> stateType, S state) throws CommandInvalidTypeException {
return ArgParserManager.parseStated(type, stateType, this, state);
}
@Override
public <T, S> boolean is(Class<T> type, Class<S> stateType, S state) {
try {
getAs(type, stateType, state);
return true;
} catch (Throwable t) {
return false;
}
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<ICommandArgument> from(String string, boolean preserveEmptyLast) {
List<ICommandArgument> 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<ICommandArgument> 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, "<unknown>", "");
}
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return RelativeFile.tabComplete(args, schematicsDir);
} else if (args.has(2)) {

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<Map.Entry<BlockPos, IRememberedInventory>> entries =
ctx.worldData().getContainerMemory().getRememberedInventories().entrySet();
@ -62,7 +62,7 @@ public class ChestsCommand extends Command {
}
@Override
public Stream<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return this.baritone.getCommandManager().tabComplete(String.format("%s %s", target, args.rawRest()));
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
if (args.hasAtMost(2)) {
return args.tabCompleteDatatype(RelativeGoalXZ.INSTANCE);
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return RelativeFile.tabComplete(args, RelativeFile.gameDir());
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<Block> toFind = new ArrayList<>();
while (args.hasAny()) {
toFind.add(args.getDatatypeFor(BlockById.INSTANCE));
@ -59,7 +59,7 @@ public class FindCommand extends Command {
}
@Override
public Stream<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return args.tabCompleteDatatype(BlockById.INSTANCE);
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.append(FollowGroup.class)

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
TabCompleteHelper helper = new TabCompleteHelper();
if (args.hasExactlyOne()) {
helper.append("reset", "clear", "none", "~");

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.addCommands(this.baritone.getCommandManager())

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<BlockOptionalMeta> boms = new ArrayList<>();
@ -51,7 +51,7 @@ public class MineCommand extends Command {
}
@Override
public Stream<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return args.tabCompleteDatatype(BlockById.INSTANCE);
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasAny() && !args.has(4)) {
while (args.has(2)) {
if (args.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) == null) {

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> 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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> 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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.append(Action.getAllNames())

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> 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))) {

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}

View File

@ -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<String> tabComplete(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasAny()) {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<ICommandArgument> args;
/**
* The list of consumed arguments for this ArgConsumer. The most recently consumed argument is the last one
*/
private final Deque<ICommandArgument> consumed;
private ArgConsumer(ICommandManager manager, Deque<ICommandArgument> args, Deque<ICommandArgument> consumed) {
this.manager = manager;
this.context = this.new Context();
this.args = new LinkedList<>(args);
this.consumed = new LinkedList<>(consumed);
}
public ArgConsumer(ICommandManager manager, List<ICommandArgument> args) {
this(manager, new LinkedList<>(args), new LinkedList<>());
}
@Override
public LinkedList<ICommandArgument> getArgs() {
return this.args;
}
@Override
public Deque<ICommandArgument> 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 extends Enum<?>> E peekEnum(Class<E> enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getEnum(enumClass);
}
@Override
public <E extends Enum<?>> E peekEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekEnum(enumClass, 0);
}
@Override
public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass, int index) throws CommandNotEnoughArgumentsException {
try {
return peekEnum(enumClass, index);
} catch (CommandInvalidTypeException e) {
return null;
}
}
@Override
public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return peekEnumOrNull(enumClass, 0);
}
@Override
public <T> T peekAs(Class<T> type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getAs(type);
}
@Override
public <T> T peekAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekAs(type, 0);
}
@Override
public <T> T peekAsOrDefault(Class<T> type, T def, int index) throws CommandNotEnoughArgumentsException {
try {
return peekAs(type, index);
} catch (CommandInvalidTypeException e) {
return def;
}
}
@Override
public <T> T peekAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, def, 0);
}
@Override
public <T> T peekAsOrNull(Class<T> type, int index) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, null, index);
}
@Override
public <T> T peekAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return peekAsOrNull(type, 0);
}
@Override
public <T> T peekDatatype(IDatatypeFor<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypeFor(datatype);
}
@Override
public <T, O> T peekDatatype(IDatatypePost<T, O> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return this.peekDatatype(datatype, null);
}
@Override
public <T, O> T peekDatatype(IDatatypePost<T, O> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original);
}
@Override
public <T> T peekDatatypeOrNull(IDatatypeFor<T> datatype) {
return copy().getDatatypeForOrNull(datatype);
}
@Override
public <T, O> T peekDatatypeOrNull(IDatatypePost<T, O> datatype) {
return copy().getDatatypePostOrNull(datatype, null);
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original);
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrDefault(D datatype, O original, T def) {
return copy().getDatatypePostOrDefault(datatype, original, def);
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrNull(D datatype, O original) {
return peekDatatypePostOrDefault(datatype, original, null);
}
@Override
public <T, D extends IDatatypeFor<T>> T peekDatatypeFor(Class<D> datatype) {
return copy().peekDatatypeFor(datatype);
}
@Override
public <T, D extends IDatatypeFor<T>> T peekDatatypeForOrDefault(Class<D> datatype, T def) {
return copy().peekDatatypeForOrDefault(datatype, def);
}
@Override
public <T, D extends IDatatypeFor<T>> T peekDatatypeForOrNull(Class<D> 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 extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getEnum(enumClass);
}
@Override
public <E extends Enum<?>> E getEnumOrDefault(Class<E> enumClass, E def) throws CommandNotEnoughArgumentsException {
try {
peekEnum(enumClass);
return getEnum(enumClass);
} catch (CommandInvalidTypeException e) {
return def;
}
}
@Override
public <E extends Enum<?>> E getEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return getEnumOrDefault(enumClass, null);
}
@Override
public <T> T getAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getAs(type);
}
@Override
public <T> T getAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
try {
T val = peek().getAs(type);
get();
return val;
} catch (CommandInvalidTypeException e) {
return def;
}
}
@Override
public <T> T getAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return getAsOrDefault(type, null);
}
@Override
public <T, O, D extends IDatatypePost<T, O>> 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, O, D extends IDatatypePost<T, O>> T getDatatypePostOrDefault(D datatype, O original, T _default) {
final List<ICommandArgument> argsSnapshot = new ArrayList<>(this.args);
final List<ICommandArgument> 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, O, D extends IDatatypePost<T, O>> T getDatatypePostOrNull(D datatype, O original) {
return this.getDatatypePostOrDefault(datatype, original, null);
}
@Override
public <T, D extends IDatatypeFor<T>> 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, D extends IDatatypeFor<T>> T getDatatypeForOrDefault(D datatype, T def) {
final List<ICommandArgument> argsSnapshot = new ArrayList<>(this.args);
final List<ICommandArgument> 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, D extends IDatatypeFor<T>> T getDatatypeForOrNull(D datatype) {
return this.getDatatypeForOrDefault(datatype, null);
}
@Override
public <T extends IDatatype> Stream<String> 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;
}
}
}

View File

@ -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<String, List<CommandArgument>> expanded) {
public boolean execute(Tuple<String, List<ICommandArgument>> expanded) {
ExecutionWrapper execution = this.from(expanded);
if (execution != null) {
execution.execute();
@ -85,16 +86,16 @@ public class CommandManager implements ICommandManager {
}
@Override
public Stream<String> tabComplete(Tuple<String, List<CommandArgument>> expanded) {
public Stream<String> tabComplete(Tuple<String, List<ICommandArgument>> expanded) {
ExecutionWrapper execution = this.from(expanded);
return execution == null ? Stream.empty() : execution.tabComplete();
}
@Override
public Stream<String> tabComplete(String prefix) {
Tuple<String, List<CommandArgument>> pair = expand(prefix, true);
Tuple<String, List<ICommandArgument>> pair = expand(prefix, true);
String label = pair.getFirst();
List<CommandArgument> args = pair.getSecond();
List<ICommandArgument> 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<String, List<CommandArgument>> expanded) {
private ExecutionWrapper from(Tuple<String, List<ICommandArgument>> 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<String, List<CommandArgument>> expand(String string, boolean preserveEmptyLast) {
private static Tuple<String, List<ICommandArgument>> expand(String string, boolean preserveEmptyLast) {
String label = string.split("\\s", 2)[0];
List<CommandArgument> args = CommandArgument.from(string.substring(label.length()), preserveEmptyLast);
List<ICommandArgument> args = CommandArguments.from(string.substring(label.length()), preserveEmptyLast);
return new Tuple<>(label, args);
}
public static Tuple<String, List<CommandArgument>> expand(String string) {
public static Tuple<String, List<ICommandArgument>> 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());
}
}