Checked Command Exceptions

This commit is contained in:
Brady
2019-09-20 18:32:43 -05:00
parent e70d18f046
commit b7eaae88a8
58 changed files with 320 additions and 292 deletions
@@ -28,6 +28,7 @@ import baritone.api.utils.Helper;
import baritone.api.utils.IPlayerContext; import baritone.api.utils.IPlayerContext;
import baritone.api.utils.SettingsUtil; import baritone.api.utils.SettingsUtil;
import baritone.api.utils.command.argument.CommandArgument; import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.exception.CommandNotFoundException; import baritone.api.utils.command.exception.CommandNotFoundException;
import baritone.api.utils.command.execution.CommandExecution; import baritone.api.utils.command.execution.CommandExecution;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -124,7 +125,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
String command = pair.first(); String command = pair.first();
String rest = msg.substring(pair.first().length()); String rest = msg.substring(pair.first().length());
ArgConsumer argc = new ArgConsumer(pair.second()); ArgConsumer argc = new ArgConsumer(pair.second());
if (!argc.has()) { if (!argc.hasAny()) {
Settings.Setting setting = settings.byLowerName.get(command.toLowerCase(Locale.US)); Settings.Setting setting = settings.byLowerName.get(command.toLowerCase(Locale.US));
if (setting != null) { if (setting != null) {
logRanCommand(command, rest); logRanCommand(command, rest);
@@ -142,7 +143,9 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
} }
if (setting.getName().equalsIgnoreCase(pair.first())) { if (setting.getName().equalsIgnoreCase(pair.first())) {
logRanCommand(command, rest); logRanCommand(command, rest);
CommandManager.execute(String.format("set %s %s", setting.getName(), argc.getString())); try {
CommandManager.execute(String.format("set %s %s", setting.getName(), argc.getString()));
} catch (CommandNotEnoughArgumentsException ignored) {} // The operation is safe
return true; return true;
} }
} }
@@ -176,31 +179,35 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
} }
public Stream<String> tabComplete(String msg) { public Stream<String> tabComplete(String msg) {
List<CommandArgument> args = CommandArgument.from(msg, true); try {
ArgConsumer argc = new ArgConsumer(args); List<CommandArgument> args = CommandArgument.from(msg, true);
if (argc.hasAtMost(2)) { ArgConsumer argc = new ArgConsumer(args);
if (argc.hasExactly(1)) { if (argc.hasAtMost(2)) {
return new TabCompleteHelper() if (argc.hasExactly(1)) {
.addCommands() return new TabCompleteHelper()
.addSettings() .addCommands()
.filterPrefix(argc.getString()) .addSettings()
.stream(); .filterPrefix(argc.getString())
} .stream();
Settings.Setting setting = settings.byLowerName.get(argc.getString().toLowerCase(Locale.US)); }
if (setting != null) { Settings.Setting setting = settings.byLowerName.get(argc.getString().toLowerCase(Locale.US));
if (setting.getValueClass() == Boolean.class) { if (setting != null) {
TabCompleteHelper helper = new TabCompleteHelper(); if (setting.getValueClass() == Boolean.class) {
if ((Boolean) setting.value) { TabCompleteHelper helper = new TabCompleteHelper();
helper.append(Stream.of("true", "false")); if ((Boolean) setting.value) {
helper.append(Stream.of("true", "false"));
} else {
helper.append(Stream.of("false", "true"));
}
return helper.filterPrefix(argc.getString()).stream();
} else { } else {
helper.append(Stream.of("false", "true")); return Stream.of(SettingsUtil.settingValueToString(setting));
} }
return helper.filterPrefix(argc.getString()).stream();
} else {
return Stream.of(SettingsUtil.settingValueToString(setting));
} }
} }
return CommandManager.tabComplete(msg);
} catch (CommandNotEnoughArgumentsException ignored) { // Shouldn't happen, the operation is safe
return Stream.empty();
} }
return CommandManager.tabComplete(msg);
} }
} }
@@ -21,6 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.Helper; import baritone.api.utils.Helper;
import baritone.api.utils.IPlayerContext; import baritone.api.utils.IPlayerContext;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.execution.CommandExecution; import baritone.api.utils.command.execution.CommandExecution;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -61,7 +62,7 @@ public abstract class Command implements Helper {
* *
* @param execution The command execution to execute this command with * @param execution The command execution to execute this command with
*/ */
public void execute(CommandExecution execution) { public void execute(CommandExecution execution) throws CommandException {
executed(execution.label, execution.args, execution.settings); executed(execution.label, execution.args, execution.settings);
} }
@@ -82,13 +83,13 @@ public abstract class Command implements Helper {
/** /**
* Called when this command is executed. * Called when this command is executed.
*/ */
protected abstract void executed(String label, ArgConsumer args, Settings settings); protected abstract void executed(String label, ArgConsumer args, Settings settings) throws CommandException;
/** /**
* Called when the command needs to tab complete. Return a Stream representing the entries to put in the completions * Called when the command needs to tab complete. Return a Stream representing the entries to put in the completions
* list. * list.
*/ */
protected abstract Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings); protected abstract Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException;
/** /**
* @return A <b>single-line</b> string containing a short description of this command's purpose. * @return A <b>single-line</b> string containing a short description of this command's purpose.
@@ -20,6 +20,7 @@ package baritone.api.utils.command.argparser;
import baritone.api.utils.command.argument.CommandArgument; import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.exception.CommandInvalidTypeException; import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.exception.CommandNoParserForTypeException; import baritone.api.utils.command.exception.CommandNoParserForTypeException;
import baritone.api.utils.command.exception.CommandUnhandledException;
import baritone.api.utils.command.registry.Registry; import baritone.api.utils.command.registry.Registry;
public class ArgParserManager { public class ArgParserManager {
@@ -66,13 +67,13 @@ public class ArgParserManager {
* @param type The type to try and parse the argument into. * @param type The type to try and parse the argument into.
* @param arg The argument to parse. * @param arg The argument to parse.
* @return An instance of the specified class. * @return An instance of the specified class.
* @throws CommandNoParserForTypeException If no parser exists for that type
* @throws CommandInvalidTypeException If the parsing failed * @throws CommandInvalidTypeException If the parsing failed
*/ */
public static <T> T parseStateless(Class<T> type, CommandArgument arg) { public static <T> T parseStateless(Class<T> type, CommandArgument arg) throws CommandInvalidTypeException {
ArgParser.Stateless<T> parser = getParserStateless(type); ArgParser.Stateless<T> parser = getParserStateless(type);
if (parser == null) { if (parser == null) {
throw new CommandNoParserForTypeException(type); // TODO: Fix this scuff lol
throw new CommandUnhandledException(new CommandNoParserForTypeException(type));
} }
try { try {
return parser.parseArg(arg); return parser.parseArg(arg);
@@ -88,14 +89,14 @@ public class ArgParserManager {
* @param arg The argument to parse. * @param arg The argument to parse.
* @param state The state to pass to the {@link ArgParser.Stated}. * @param state The state to pass to the {@link ArgParser.Stated}.
* @return An instance of the specified class. * @return An instance of the specified class.
* @throws CommandNoParserForTypeException If no parser exists for that type
* @throws CommandInvalidTypeException If the parsing failed * @throws CommandInvalidTypeException If the parsing failed
* @see ArgParser.Stated * @see ArgParser.Stated
*/ */
public static <T, S> T parseStated(Class<T> type, Class<S> stateKlass, CommandArgument arg, S state) { public static <T, S> T parseStated(Class<T> type, Class<S> stateKlass, CommandArgument arg, S state) throws CommandInvalidTypeException {
ArgParser.Stated<T, S> parser = getParserStated(type, stateKlass); ArgParser.Stated<T, S> parser = getParserStated(type, stateKlass);
if (parser == null) { if (parser == null) {
throw new CommandNoParserForTypeException(type); // TODO: Fix this scuff lol
throw new CommandUnhandledException(new CommandNoParserForTypeException(type));
} }
try { try {
return parser.parseArg(arg, state); return parser.parseArg(arg, state);
@@ -66,7 +66,7 @@ public class CommandArgument {
* @see ArgConsumer#getEnum(Class) * @see ArgConsumer#getEnum(Class)
* @see ArgConsumer#getEnumOrNull(Class) * @see ArgConsumer#getEnumOrNull(Class)
*/ */
public <E extends Enum<?>> E getEnum(Class<E> enumClass) { public <E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException {
return Arrays.stream(enumClass.getEnumConstants()) return Arrays.stream(enumClass.getEnumConstants())
.filter(e -> e.name().equalsIgnoreCase(value)) .filter(e -> e.name().equalsIgnoreCase(value))
.findFirst() .findFirst()
@@ -78,10 +78,9 @@ public class CommandArgument {
* *
* @param type The class to parse this argument into * @param type The class to parse this argument into
* @return An instance of the specified type * @return An instance of the specified type
* @throws CommandNoParserForTypeException If no parser exists for that type * @throws CommandInvalidTypeException If the parsing failed
* @throws CommandInvalidTypeException If the parsing failed
*/ */
public <T> T getAs(Class<T> type) { public <T> T getAs(Class<T> type) throws CommandInvalidTypeException {
return ArgParserManager.parseStateless(type, this); return ArgParserManager.parseStateless(type, this);
} }
@@ -105,11 +104,10 @@ public class CommandArgument {
* *
* @param type The class to parse this argument into * @param type The class to parse this argument into
* @return An instance of the specified type * @return An instance of the specified type
* @throws CommandNoParserForTypeException If no parser exists for that type
* @throws CommandInvalidTypeException If the parsing failed * @throws CommandInvalidTypeException If the parsing failed
*/ */
@SuppressWarnings("UnusedReturnValue") @SuppressWarnings("UnusedReturnValue")
public <T, S> T getAs(Class<T> type, Class<S> stateType, S state) { public <T, S> T getAs(Class<T> type, Class<S> stateType, S state) throws CommandInvalidTypeException {
return ArgParserManager.parseStated(type, stateType, this, state); return ArgParserManager.parseStated(type, stateType, this, state);
} }
@@ -17,6 +17,8 @@
package baritone.api.utils.command.datatypes; package baritone.api.utils.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@@ -33,7 +35,7 @@ public class BlockById implements IDatatypeFor<Block> {
block = null; block = null;
} }
public BlockById(ArgConsumer consumer) { public BlockById(ArgConsumer consumer) throws CommandNotEnoughArgumentsException {
ResourceLocation id = new ResourceLocation(consumer.getString()); ResourceLocation id = new ResourceLocation(consumer.getString());
if ((block = Block.REGISTRY.getObject(id)) == Blocks.AIR) { if ((block = Block.REGISTRY.getObject(id)) == Blocks.AIR) {
throw new IllegalArgumentException("no block found by that id"); throw new IllegalArgumentException("no block found by that id");
@@ -46,7 +48,7 @@ public class BlockById implements IDatatypeFor<Block> {
} }
@Override @Override
public Stream<String> tabComplete(ArgConsumer consumer) { public Stream<String> tabComplete(ArgConsumer consumer) throws CommandException {
return new TabCompleteHelper() return new TabCompleteHelper()
.append( .append(
Block.REGISTRY.getKeys() Block.REGISTRY.getKeys()
@@ -17,6 +17,7 @@
package baritone.api.utils.command.datatypes; 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.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@@ -33,7 +34,7 @@ public class EntityClassById implements IDatatypeFor<Class<? extends Entity>> {
entity = null; entity = null;
} }
public EntityClassById(ArgConsumer consumer) { public EntityClassById(ArgConsumer consumer) throws CommandException {
ResourceLocation id = new ResourceLocation(consumer.getString()); ResourceLocation id = new ResourceLocation(consumer.getString());
if ((entity = EntityList.REGISTRY.getObject(id)) == null) { if ((entity = EntityList.REGISTRY.getObject(id)) == null) {
throw new IllegalArgumentException("no entity found by that id"); throw new IllegalArgumentException("no entity found by that id");
@@ -46,7 +47,7 @@ public class EntityClassById implements IDatatypeFor<Class<? extends Entity>> {
} }
@Override @Override
public Stream<String> tabComplete(ArgConsumer consumer) { public Stream<String> tabComplete(ArgConsumer consumer) throws CommandException {
return new TabCompleteHelper() return new TabCompleteHelper()
.append( .append(
EntityList.getEntityNameList() EntityList.getEntityNameList()
@@ -18,6 +18,7 @@
package baritone.api.utils.command.datatypes; package baritone.api.utils.command.datatypes;
import baritone.api.utils.BlockOptionalMeta; import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -30,7 +31,7 @@ public class ForBlockOptionalMeta implements IDatatypeFor<BlockOptionalMeta> {
selector = null; selector = null;
} }
public ForBlockOptionalMeta(ArgConsumer consumer) { public ForBlockOptionalMeta(ArgConsumer consumer) throws CommandException {
selector = new BlockOptionalMeta(consumer.getString()); selector = new BlockOptionalMeta(consumer.getString());
} }
@@ -17,6 +17,8 @@
package baritone.api.utils.command.datatypes; package baritone.api.utils.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
@@ -33,7 +35,7 @@ public class ForEnumFacing implements IDatatypeFor<EnumFacing> {
facing = null; facing = null;
} }
public ForEnumFacing(ArgConsumer consumer) { public ForEnumFacing(ArgConsumer consumer) throws CommandNotEnoughArgumentsException {
facing = EnumFacing.valueOf(consumer.getString().toUpperCase(Locale.US)); facing = EnumFacing.valueOf(consumer.getString().toUpperCase(Locale.US));
} }
@@ -43,7 +45,7 @@ public class ForEnumFacing implements IDatatypeFor<EnumFacing> {
} }
@Override @Override
public Stream<String> tabComplete(ArgConsumer consumer) { public Stream<String> tabComplete(ArgConsumer consumer) throws CommandException {
return new TabCompleteHelper() return new TabCompleteHelper()
.append( .append(
Arrays.stream(EnumFacing.values()) Arrays.stream(EnumFacing.values())
@@ -20,6 +20,8 @@ package baritone.api.utils.command.datatypes;
import baritone.api.BaritoneAPI; import baritone.api.BaritoneAPI;
import baritone.api.cache.IWaypoint; import baritone.api.cache.IWaypoint;
import baritone.api.cache.IWaypointCollection; import baritone.api.cache.IWaypointCollection;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
@@ -40,7 +42,7 @@ public class ForWaypoints implements IDatatypeFor<IWaypoint[]> {
waypoints = tag == null ? getWaypointsByName(arg) : getWaypointsByTag(tag); waypoints = tag == null ? getWaypointsByName(arg) : getWaypointsByTag(tag);
} }
public ForWaypoints(ArgConsumer consumer) { public ForWaypoints(ArgConsumer consumer) throws CommandNotEnoughArgumentsException {
this(consumer.getString()); this(consumer.getString());
} }
@@ -50,7 +52,7 @@ public class ForWaypoints implements IDatatypeFor<IWaypoint[]> {
} }
@Override @Override
public Stream<String> tabComplete(ArgConsumer consumer) { public Stream<String> tabComplete(ArgConsumer consumer) throws CommandException {
return new TabCompleteHelper() return new TabCompleteHelper()
.append(getWaypointNames()) .append(getWaypointNames())
.sortAlphabetically() .sortAlphabetically()
@@ -18,6 +18,7 @@
package baritone.api.utils.command.datatypes; package baritone.api.utils.command.datatypes;
import baritone.api.utils.command.argparser.ArgParser; import baritone.api.utils.command.argparser.ArgParser;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidArgumentException; import baritone.api.utils.command.exception.CommandInvalidArgumentException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -44,5 +45,5 @@ public interface IDatatype {
* @return A stream representing the strings that can be tab completed. DO NOT INCLUDE SPACES IN ANY STRINGS. * @return A stream representing the strings that can be tab completed. DO NOT INCLUDE SPACES IN ANY STRINGS.
* @see ArgConsumer#tabCompleteDatatype(Class) * @see ArgConsumer#tabCompleteDatatype(Class)
*/ */
Stream<String> tabComplete(ArgConsumer consumer); Stream<String> tabComplete(ArgConsumer consumer) throws CommandException;
} }
@@ -18,6 +18,7 @@
package baritone.api.utils.command.datatypes; package baritone.api.utils.command.datatypes;
import baritone.api.BaritoneAPI; import baritone.api.BaritoneAPI;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
@@ -35,7 +36,7 @@ public class PlayerByUsername implements IDatatypeFor<EntityPlayer> {
player = null; player = null;
} }
public PlayerByUsername(ArgConsumer consumer) { public PlayerByUsername(ArgConsumer consumer) throws CommandException {
String username = consumer.getString(); String username = consumer.getString();
player = players player = players
.stream() .stream()
@@ -53,7 +54,7 @@ public class PlayerByUsername implements IDatatypeFor<EntityPlayer> {
} }
@Override @Override
public Stream<String> tabComplete(ArgConsumer consumer) { public Stream<String> tabComplete(ArgConsumer consumer) throws CommandException {
return new TabCompleteHelper() return new TabCompleteHelper()
.append( .append(
players players
@@ -18,6 +18,7 @@
package baritone.api.utils.command.datatypes; package baritone.api.utils.command.datatypes;
import baritone.api.utils.BetterBlockPos; 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.ArgConsumer;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -34,7 +35,7 @@ public class RelativeBlockPos implements IDatatypePost<BetterBlockPos, BetterBlo
z = null; z = null;
} }
public RelativeBlockPos(ArgConsumer consumer) { public RelativeBlockPos(ArgConsumer consumer) throws CommandException {
x = consumer.getDatatype(RelativeCoordinate.class); x = consumer.getDatatype(RelativeCoordinate.class);
y = consumer.getDatatype(RelativeCoordinate.class); y = consumer.getDatatype(RelativeCoordinate.class);
z = consumer.getDatatype(RelativeCoordinate.class); z = consumer.getDatatype(RelativeCoordinate.class);
@@ -50,8 +51,8 @@ public class RelativeBlockPos implements IDatatypePost<BetterBlockPos, BetterBlo
} }
@Override @Override
public Stream<String> tabComplete(ArgConsumer consumer) { public Stream<String> tabComplete(ArgConsumer consumer) throws CommandException {
if (consumer.has() && !consumer.has(4)) { if (consumer.hasAny() && !consumer.has(4)) {
while (consumer.has(2)) { while (consumer.has(2)) {
if (consumer.peekDatatypeOrNull(RelativeCoordinate.class) == null) { if (consumer.peekDatatypeOrNull(RelativeCoordinate.class) == null) {
break; break;
@@ -17,6 +17,8 @@
package baritone.api.utils.command.datatypes; package baritone.api.utils.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
@@ -35,7 +37,7 @@ public class RelativeCoordinate implements IDatatypePost<Double, Double> {
offset = 0; offset = 0;
} }
public RelativeCoordinate(ArgConsumer consumer) { public RelativeCoordinate(ArgConsumer consumer) throws CommandNotEnoughArgumentsException {
Matcher matcher = PATTERN.matcher(consumer.getString()); Matcher matcher = PATTERN.matcher(consumer.getString());
if (!matcher.matches()) { if (!matcher.matches()) {
throw new IllegalArgumentException("pattern doesn't match"); throw new IllegalArgumentException("pattern doesn't match");
@@ -57,7 +59,7 @@ public class RelativeCoordinate implements IDatatypePost<Double, Double> {
} }
@Override @Override
public Stream<String> tabComplete(ArgConsumer consumer) { public Stream<String> tabComplete(ArgConsumer consumer) throws CommandException {
if (!consumer.has(2) && consumer.getString().matches("^(~|$)")) { if (!consumer.has(2) && consumer.getString().matches("^(~|$)")) {
return Stream.of("~"); return Stream.of("~");
} }
@@ -17,6 +17,8 @@
package baritone.api.utils.command.datatypes; package baritone.api.utils.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import java.io.File; import java.io.File;
@@ -40,7 +42,7 @@ public class RelativeFile implements IDatatypePost<File, File> {
path = null; path = null;
} }
public RelativeFile(ArgConsumer consumer) { public RelativeFile(ArgConsumer consumer) throws CommandNotEnoughArgumentsException {
try { try {
path = FileSystems.getDefault().getPath(consumer.getString()); path = FileSystems.getDefault().getPath(consumer.getString());
} catch (InvalidPathException e) { } catch (InvalidPathException e) {
@@ -68,9 +70,12 @@ public class RelativeFile implements IDatatypePost<File, File> {
} }
} }
public static Stream<String> tabComplete(ArgConsumer consumer, File base0) { public static Stream<String> tabComplete(ArgConsumer consumer, File base0) throws CommandException {
// I will not make the caller deal with this, seriously // 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 // Tab complete code is beautiful and I'm not going to bloat it with dumb ass checked exception bullshit -LoganDark
// lol owned -Brady
File base = getCanonicalFileUnchecked(base0); File base = getCanonicalFileUnchecked(base0);
String currentPathStringThing = consumer.getString(); String currentPathStringThing = consumer.getString();
Path currentPath = FileSystems.getDefault().getPath(currentPathStringThing); Path currentPath = FileSystems.getDefault().getPath(currentPathStringThing);
@@ -22,6 +22,7 @@ import baritone.api.pathing.goals.GoalBlock;
import baritone.api.pathing.goals.GoalXZ; import baritone.api.pathing.goals.GoalXZ;
import baritone.api.pathing.goals.GoalYLevel; import baritone.api.pathing.goals.GoalYLevel;
import baritone.api.utils.BetterBlockPos; 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.ArgConsumer;
import java.util.ArrayList; import java.util.ArrayList;
@@ -36,7 +37,7 @@ public class RelativeGoal implements IDatatypePost<Goal, BetterBlockPos> {
coords = new RelativeCoordinate[0]; coords = new RelativeCoordinate[0];
} }
public RelativeGoal(ArgConsumer consumer) { public RelativeGoal(ArgConsumer consumer) throws CommandException {
List<RelativeCoordinate> coordsList = new ArrayList<>(); List<RelativeCoordinate> coordsList = new ArrayList<>();
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
if (consumer.peekDatatypeOrNull(RelativeCoordinate.class) != null) { if (consumer.peekDatatypeOrNull(RelativeCoordinate.class) != null) {
@@ -19,6 +19,8 @@ package baritone.api.utils.command.datatypes;
import baritone.api.pathing.goals.GoalBlock; import baritone.api.pathing.goals.GoalBlock;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
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.ArgConsumer;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -31,7 +33,7 @@ public class RelativeGoalBlock implements IDatatypePost<GoalBlock, BetterBlockPo
coords = new RelativeCoordinate[0]; coords = new RelativeCoordinate[0];
} }
public RelativeGoalBlock(ArgConsumer consumer) { public RelativeGoalBlock(ArgConsumer consumer) throws CommandException {
coords = new RelativeCoordinate[]{ coords = new RelativeCoordinate[]{
consumer.getDatatype(RelativeCoordinate.class), consumer.getDatatype(RelativeCoordinate.class),
consumer.getDatatype(RelativeCoordinate.class), consumer.getDatatype(RelativeCoordinate.class),
@@ -19,6 +19,7 @@ package baritone.api.utils.command.datatypes;
import baritone.api.pathing.goals.GoalXZ; import baritone.api.pathing.goals.GoalXZ;
import baritone.api.utils.BetterBlockPos; 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.ArgConsumer;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -31,7 +32,7 @@ public class RelativeGoalXZ implements IDatatypePost<GoalXZ, BetterBlockPos> {
coords = new RelativeCoordinate[0]; coords = new RelativeCoordinate[0];
} }
public RelativeGoalXZ(ArgConsumer consumer) { public RelativeGoalXZ(ArgConsumer consumer) throws CommandException {
coords = new RelativeCoordinate[]{ coords = new RelativeCoordinate[]{
consumer.getDatatype(RelativeCoordinate.class), consumer.getDatatype(RelativeCoordinate.class),
consumer.getDatatype(RelativeCoordinate.class) consumer.getDatatype(RelativeCoordinate.class)
@@ -19,6 +19,7 @@ package baritone.api.utils.command.datatypes;
import baritone.api.pathing.goals.GoalYLevel; import baritone.api.pathing.goals.GoalYLevel;
import baritone.api.utils.BetterBlockPos; 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.ArgConsumer;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -31,7 +32,7 @@ public class RelativeGoalYLevel implements IDatatypePost<GoalYLevel, BetterBlock
coord = null; coord = null;
} }
public RelativeGoalYLevel(ArgConsumer consumer) { public RelativeGoalYLevel(ArgConsumer consumer) throws CommandException {
coord = consumer.getDatatype(RelativeCoordinate.class); coord = consumer.getDatatype(RelativeCoordinate.class);
} }
@@ -17,22 +17,9 @@
package baritone.api.utils.command.exception; package baritone.api.utils.command.exception;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument;
import net.minecraft.util.text.TextFormatting;
import java.util.List;
import static baritone.api.utils.Helper.HELPER;
public abstract class CommandErrorMessageException extends CommandException { public abstract class CommandErrorMessageException extends CommandException {
protected CommandErrorMessageException(String reason) { protected CommandErrorMessageException(String reason) {
super(reason); super(reason);
} }
@Override
public void handle(Command command, List<CommandArgument> args) {
HELPER.logDirect(getMessage(), TextFormatting.RED);
}
} }
@@ -17,22 +17,9 @@
package baritone.api.utils.command.exception; package baritone.api.utils.command.exception;
import baritone.api.utils.command.Command; public abstract class CommandException extends Exception implements ICommandException {
import baritone.api.utils.command.argument.CommandArgument;
import java.util.List;
public abstract class CommandException extends RuntimeException {
protected CommandException(String reason) { protected CommandException(String reason) {
super(reason); super(reason);
} }
/**
* Called when this exception is thrown, to handle the exception.
*
* @param command The command that threw it.
* @param args The arguments the command was called with.
*/
public abstract void handle(Command command, List<CommandArgument> args);
} }
@@ -23,15 +23,22 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class CommandUnhandledException extends CommandErrorMessageException { public class CommandUnhandledException extends RuntimeException implements ICommandException {
public static String getStackTrace(Throwable throwable) { public CommandUnhandledException(Throwable cause) {
super(String.format(
"An unhandled exception has occurred:\n\n%s",
getFriendlierStackTrace(cause)
));
}
private static String getStackTrace(Throwable throwable) {
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
throwable.printStackTrace(new PrintWriter(sw)); throwable.printStackTrace(new PrintWriter(sw));
return sw.toString(); return sw.toString();
} }
public static String getBaritoneStackTrace(String stackTrace) { private static String getBaritoneStackTrace(String stackTrace) {
List<String> lines = Arrays.stream(stackTrace.split("\n")) List<String> lines = Arrays.stream(stackTrace.split("\n"))
.collect(Collectors.toList()); .collect(Collectors.toList());
int lastBaritoneLine = 0; int lastBaritoneLine = 0;
@@ -43,11 +50,11 @@ public class CommandUnhandledException extends CommandErrorMessageException {
return String.join("\n", lines.subList(0, lastBaritoneLine + 1)); return String.join("\n", lines.subList(0, lastBaritoneLine + 1));
} }
public static String getBaritoneStackTrace(Throwable throwable) { private static String getBaritoneStackTrace(Throwable throwable) {
return getBaritoneStackTrace(getStackTrace(throwable)); return getBaritoneStackTrace(getStackTrace(throwable));
} }
public static String getFriendlierStackTrace(String stackTrace) { private static String getFriendlierStackTrace(String stackTrace) {
List<String> lines = Arrays.asList(stackTrace.split("\n")); List<String> lines = Arrays.asList(stackTrace.split("\n"));
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i); String line = lines.get(i);
@@ -64,14 +71,7 @@ public class CommandUnhandledException extends CommandErrorMessageException {
return String.join("\n", lines); return String.join("\n", lines);
} }
public static String getFriendlierStackTrace(Throwable throwable) { private static String getFriendlierStackTrace(Throwable throwable) {
return getFriendlierStackTrace(getBaritoneStackTrace(throwable)); return getFriendlierStackTrace(getBaritoneStackTrace(throwable));
} }
public CommandUnhandledException(Throwable cause) {
super(String.format(
"An unhandled exception has occurred:\n\n%s",
getFriendlierStackTrace(cause)
));
}
} }
@@ -0,0 +1,55 @@
/*
* 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.exception;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument;
import net.minecraft.util.text.TextFormatting;
import java.util.List;
import static baritone.api.utils.Helper.HELPER;
/**
* The base for a Baritone Command Exception, checked or unchecked. Provides a
* {@link #handle(Command, List)} method that is used to provide useful output
* to the user for diagnosing issues that may have occurred during execution.
* <p>
* Anything implementing this interface should be assignable to {@link Exception}.
*
* @author Brady
* @since 9/20/2019
*/
public interface ICommandException {
/**
* @see Exception#getMessage()
* @return The exception details
*/
String getMessage();
/**
* Called when this exception is thrown, to handle the exception.
*
* @param command The command that threw it.
* @param args The arguments the command was called with.
*/
default void handle(Command command, List<CommandArgument> args) {
HELPER.logDirect(this.getMessage(), TextFormatting.RED);
}
}
@@ -77,7 +77,7 @@ public class ArgConsumer {
/** /**
* @param num The number of arguments to check for * @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 ArgConsumer}
* @see #has() * @see #hasAny()
* @see #hasAtMost(int) * @see #hasAtMost(int)
* @see #hasExactly(int) * @see #hasExactly(int)
*/ */
@@ -91,7 +91,7 @@ public class ArgConsumer {
* @see #hasAtMostOne() * @see #hasAtMostOne()
* @see #hasExactlyOne() * @see #hasExactlyOne()
*/ */
public boolean has() { public boolean hasAny() {
return has(1); return has(1);
} }
@@ -108,7 +108,7 @@ public class ArgConsumer {
/** /**
* @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 ArgConsumer}
* @see #has() * @see #hasAny()
* @see #hasAtMostOne() * @see #hasAtMostOne()
* @see #hasExactlyOne() * @see #hasExactlyOne()
*/ */
@@ -128,7 +128,7 @@ public class ArgConsumer {
/** /**
* @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 ArgConsumer}
* @see #has() * @see #hasAny()
* @see #hasAtMostOne() * @see #hasAtMostOne()
*/ */
public boolean hasExactlyOne() { public boolean hasExactlyOne() {
@@ -145,7 +145,7 @@ public class ArgConsumer {
* @see #peekAs(Class, int) * @see #peekAs(Class, int)
* @see #get() * @see #get()
*/ */
public CommandArgument peek(int index) { public CommandArgument peek(int index) throws CommandNotEnoughArgumentsException {
requireMin(index + 1); requireMin(index + 1);
return args.get(index); return args.get(index);
} }
@@ -161,7 +161,7 @@ public class ArgConsumer {
* @see #peekDatatypePost(Class, Object) * @see #peekDatatypePost(Class, Object)
* @see #get() * @see #get()
*/ */
public CommandArgument peek() { public CommandArgument peek() throws CommandNotEnoughArgumentsException {
return peek(0); return peek(0);
} }
@@ -174,7 +174,7 @@ public class ArgConsumer {
* @see #peek() * @see #peek()
* @see #getAs(Class) * @see #getAs(Class)
*/ */
public boolean is(Class<?> type, int index) { public boolean is(Class<?> type, int index) throws CommandNotEnoughArgumentsException {
return peek(index).is(type); return peek(index).is(type);
} }
@@ -186,7 +186,7 @@ public class ArgConsumer {
* @see #peek() * @see #peek()
* @see #getAs(Class) * @see #getAs(Class)
*/ */
public boolean is(Class<?> type) { public boolean is(Class<?> type) throws CommandNotEnoughArgumentsException {
return is(type, 0); return is(type, 0);
} }
@@ -198,7 +198,7 @@ public class ArgConsumer {
* @see #peek() * @see #peek()
* @see #peekString() * @see #peekString()
*/ */
public String peekString(int index) { public String peekString(int index) throws CommandNotEnoughArgumentsException {
return peek(index).value; return peek(index).value;
} }
@@ -208,7 +208,7 @@ public class ArgConsumer {
* @see #peekString(int) * @see #peekString(int)
* @see #getString() * @see #getString()
*/ */
public String peekString() { public String peekString() throws CommandNotEnoughArgumentsException {
return peekString(0); return peekString(0);
} }
@@ -222,7 +222,7 @@ public class ArgConsumer {
* @see #getEnum(Class) * @see #getEnum(Class)
* @see CommandArgument#getEnum(Class) * @see CommandArgument#getEnum(Class)
*/ */
public <E extends Enum<?>> E peekEnum(Class<E> enumClass, int index) { public <E extends Enum<?>> E peekEnum(Class<E> enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getEnum(enumClass); return peek(index).getEnum(enumClass);
} }
@@ -235,7 +235,7 @@ public class ArgConsumer {
* @see #getEnum(Class) * @see #getEnum(Class)
* @see CommandArgument#getEnum(Class) * @see CommandArgument#getEnum(Class)
*/ */
public <E extends Enum<?>> E peekEnum(Class<E> enumClass) { public <E extends Enum<?>> E peekEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekEnum(enumClass, 0); return peekEnum(enumClass, 0);
} }
@@ -248,7 +248,7 @@ public class ArgConsumer {
* @see #getEnumOrNull(Class) * @see #getEnumOrNull(Class)
* @see CommandArgument#getEnum(Class) * @see CommandArgument#getEnum(Class)
*/ */
public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass, int index) { public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass, int index) throws CommandNotEnoughArgumentsException {
try { try {
return peekEnum(enumClass, index); return peekEnum(enumClass, index);
} catch (CommandInvalidTypeException e) { } catch (CommandInvalidTypeException e) {
@@ -264,12 +264,8 @@ public class ArgConsumer {
* @see #getEnumOrNull(Class) * @see #getEnumOrNull(Class)
* @see CommandArgument#getEnum(Class) * @see CommandArgument#getEnum(Class)
*/ */
public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass) { public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
try { return peekEnumOrNull(enumClass, 0);
return peekEnumOrNull(enumClass, 0);
} catch (CommandInvalidTypeException e) {
return null;
}
} }
/** /**
@@ -283,14 +279,13 @@ public class ArgConsumer {
* @param type The type to peek as * @param type The type to peek as
* @param index The index to peek * @param index The index to peek
* @return An instance of the specified type * @return An instance of the specified type
* @throws CommandNoParserForTypeException If no parser exists for that type
* @throws CommandInvalidTypeException If the parsing failed * @throws CommandInvalidTypeException If the parsing failed
* @see ArgParser * @see ArgParser
* @see #peekAs(Class) * @see #peekAs(Class)
* @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrDefault(Class, Object, int)
* @see #peekAsOrNull(Class, int) * @see #peekAsOrNull(Class, int)
*/ */
public <T> T peekAs(Class<T> type, int index) { public <T> T peekAs(Class<T> type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getAs(type); return peek(index).getAs(type);
} }
@@ -303,14 +298,13 @@ public class ArgConsumer {
* *
* @param type The type to peek as * @param type The type to peek as
* @return An instance of the specified type * @return An instance of the specified type
* @throws CommandNoParserForTypeException If no parser exists for that type
* @throws CommandInvalidTypeException If the parsing failed * @throws CommandInvalidTypeException If the parsing failed
* @see ArgParser * @see ArgParser
* @see #peekAs(Class, int) * @see #peekAs(Class, int)
* @see #peekAsOrDefault(Class, Object) * @see #peekAsOrDefault(Class, Object)
* @see #peekAsOrNull(Class) * @see #peekAsOrNull(Class)
*/ */
public <T> T peekAs(Class<T> type) { public <T> T peekAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekAs(type, 0); return peekAs(type, 0);
} }
@@ -331,7 +325,7 @@ public class ArgConsumer {
* @see #peekAs(Class, int) * @see #peekAs(Class, int)
* @see #peekAsOrNull(Class, int) * @see #peekAsOrNull(Class, int)
*/ */
public <T> T peekAsOrDefault(Class<T> type, T def, int index) { public <T> T peekAsOrDefault(Class<T> type, T def, int index) throws CommandNotEnoughArgumentsException {
try { try {
return peekAs(type, index); return peekAs(type, index);
} catch (CommandInvalidTypeException e) { } catch (CommandInvalidTypeException e) {
@@ -354,7 +348,7 @@ public class ArgConsumer {
* @see #peekAs(Class) * @see #peekAs(Class)
* @see #peekAsOrNull(Class) * @see #peekAsOrNull(Class)
*/ */
public <T> T peekAsOrDefault(Class<T> type, T def) { public <T> T peekAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, def, 0); return peekAsOrDefault(type, def, 0);
} }
@@ -374,7 +368,7 @@ public class ArgConsumer {
* @see #peekAs(Class, int) * @see #peekAs(Class, int)
* @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrDefault(Class, Object, int)
*/ */
public <T> T peekAsOrNull(Class<T> type, int index) { public <T> T peekAsOrNull(Class<T> type, int index) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, null, index); return peekAsOrDefault(type, null, index);
} }
@@ -392,7 +386,7 @@ public class ArgConsumer {
* @see #peekAs(Class) * @see #peekAs(Class)
* @see #peekAsOrDefault(Class, Object) * @see #peekAsOrDefault(Class, Object)
*/ */
public <T> T peekAsOrNull(Class<T> type) { public <T> T peekAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return peekAsOrNull(type, 0); return peekAsOrNull(type, 0);
} }
@@ -409,7 +403,7 @@ public class ArgConsumer {
* @return The datatype instance * @return The datatype instance
* @see IDatatype * @see IDatatype
*/ */
public <T extends IDatatype> T peekDatatype(Class<T> datatype) { public <T extends IDatatype> T peekDatatype(Class<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatype(datatype); return copy().getDatatype(datatype);
} }
@@ -426,7 +420,7 @@ public class ArgConsumer {
* @return The datatype instance, or {@code null} if it throws an exception * @return The datatype instance, or {@code null} if it throws an exception
* @see IDatatype * @see IDatatype
*/ */
public <T extends IDatatype> T peekDatatypeOrNull(Class<T> datatype) { public <T extends IDatatype> T peekDatatypeOrNull(Class<T> datatype) throws CommandNotEnoughArgumentsException {
return copy().getDatatypeOrNull(datatype); return copy().getDatatypeOrNull(datatype);
} }
@@ -444,7 +438,7 @@ public class ArgConsumer {
* @see IDatatype * @see IDatatype
* @see IDatatypePost * @see IDatatypePost
*/ */
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePost(Class<D> datatype, O original) { public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePost(Class<D> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original); return copy().getDatatypePost(datatype, original);
} }
@@ -547,7 +541,7 @@ public class ArgConsumer {
* @return The next argument * @return The next argument
* @throws CommandNotEnoughArgumentsException If there's less than one argument left * @throws CommandNotEnoughArgumentsException If there's less than one argument left
*/ */
public CommandArgument get() { public CommandArgument get() throws CommandNotEnoughArgumentsException {
requireMin(1); requireMin(1);
CommandArgument arg = args.removeFirst(); CommandArgument arg = args.removeFirst();
consumed.add(arg); consumed.add(arg);
@@ -561,7 +555,7 @@ public class ArgConsumer {
* @return The value of the next argument * @return The value of the next argument
* @throws CommandNotEnoughArgumentsException If there's less than one argument left * @throws CommandNotEnoughArgumentsException If there's less than one argument left
*/ */
public String getString() { public String getString() throws CommandNotEnoughArgumentsException {
return get().value; return get().value;
} }
@@ -578,7 +572,7 @@ public class ArgConsumer {
* @see #getEnumOrNull(Class) * @see #getEnumOrNull(Class)
* @see CommandArgument#getEnum(Class) * @see CommandArgument#getEnum(Class)
*/ */
public <E extends Enum<?>> E getEnum(Class<E> enumClass) { public <E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getEnum(enumClass); return get().getEnum(enumClass);
} }
@@ -597,7 +591,7 @@ public class ArgConsumer {
* @see #peekEnumOrNull(Class) * @see #peekEnumOrNull(Class)
* @see CommandArgument#getEnum(Class) * @see CommandArgument#getEnum(Class)
*/ */
public <E extends Enum<?>> E getEnumOrDefault(Class<E> enumClass, E def) { public <E extends Enum<?>> E getEnumOrDefault(Class<E> enumClass, E def) throws CommandNotEnoughArgumentsException {
try { try {
peekEnum(enumClass); peekEnum(enumClass);
return getEnum(enumClass); return getEnum(enumClass);
@@ -620,7 +614,7 @@ public class ArgConsumer {
* @see #peekEnumOrNull(Class) * @see #peekEnumOrNull(Class)
* @see CommandArgument#getEnum(Class) * @see CommandArgument#getEnum(Class)
*/ */
public <E extends Enum<?>> E getEnumOrNull(Class<E> enumClass) { public <E extends Enum<?>> E getEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return getEnumOrDefault(enumClass, null); return getEnumOrDefault(enumClass, null);
} }
@@ -633,7 +627,6 @@ public class ArgConsumer {
* *
* @param type The type to peek as * @param type The type to peek as
* @return An instance of the specified type * @return An instance of the specified type
* @throws CommandNoParserForTypeException If no parser exists for that type
* @throws CommandInvalidTypeException If the parsing failed * @throws CommandInvalidTypeException If the parsing failed
* @see ArgParser * @see ArgParser
* @see #get() * @see #get()
@@ -643,7 +636,7 @@ public class ArgConsumer {
* @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrDefault(Class, Object, int)
* @see #peekAsOrNull(Class, int) * @see #peekAsOrNull(Class, int)
*/ */
public <T> T getAs(Class<T> type) { public <T> T getAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getAs(type); return get().getAs(type);
} }
@@ -665,7 +658,7 @@ public class ArgConsumer {
* @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrDefault(Class, Object, int)
* @see #peekAsOrNull(Class, int) * @see #peekAsOrNull(Class, int)
*/ */
public <T> T getAsOrDefault(Class<T> type, T def) { public <T> T getAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
try { try {
T val = peek().getAs(type); T val = peek().getAs(type);
get(); get();
@@ -692,7 +685,7 @@ public class ArgConsumer {
* @see #peekAsOrDefault(Class, Object, int) * @see #peekAsOrDefault(Class, Object, int)
* @see #peekAsOrNull(Class, int) * @see #peekAsOrNull(Class, int)
*/ */
public <T> T getAsOrNull(Class<T> type) { public <T> T getAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return getAsOrDefault(type, null); return getAsOrDefault(type, null);
} }
@@ -707,11 +700,11 @@ public class ArgConsumer {
* @return The datatype instance * @return The datatype instance
* @see IDatatype * @see IDatatype
*/ */
public <T extends IDatatype> T getDatatype(Class<T> datatype) { public <T extends IDatatype> T getDatatype(Class<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
try { try {
return datatype.getConstructor(ArgConsumer.class).newInstance(this); return datatype.getConstructor(ArgConsumer.class).newInstance(this);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
throw new CommandInvalidTypeException(has() ? peek() : consumed(), datatype.getSimpleName()); throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getSimpleName());
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) { } catch (NoSuchMethodException | IllegalAccessException | InstantiationException e) {
throw new CommandUnhandledException(e); throw new CommandUnhandledException(e);
} }
@@ -730,7 +723,7 @@ public class ArgConsumer {
* @return The datatype instance, or {@code null} if it throws an exception * @return The datatype instance, or {@code null} if it throws an exception
* @see IDatatype * @see IDatatype
*/ */
public <T extends IDatatype> T getDatatypeOrNull(Class<T> datatype) { public <T extends IDatatype> T getDatatypeOrNull(Class<T> datatype) throws CommandNotEnoughArgumentsException {
List<CommandArgument> argsSnapshot = new ArrayList<>(args); List<CommandArgument> argsSnapshot = new ArrayList<>(args);
List<CommandArgument> consumedSnapshot = new ArrayList<>(consumed); List<CommandArgument> consumedSnapshot = new ArrayList<>(consumed);
try { try {
@@ -756,7 +749,7 @@ public class ArgConsumer {
* @see IDatatype * @see IDatatype
* @see IDatatypePost * @see IDatatypePost
*/ */
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePost(Class<D> datatype, O original) { public <T, O, D extends IDatatypePost<T, O>> T getDatatypePost(Class<D> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return getDatatype(datatype).apply(original); return getDatatype(datatype).apply(original);
} }
@@ -819,7 +812,7 @@ public class ArgConsumer {
* @see IDatatype * @see IDatatype
* @see IDatatypeFor * @see IDatatypeFor
*/ */
public <T, D extends IDatatypeFor<T>> T getDatatypeFor(Class<D> datatype) { public <T, D extends IDatatypeFor<T>> T getDatatypeFor(Class<D> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return getDatatype(datatype).get(); return getDatatype(datatype).get();
} }
@@ -838,7 +831,7 @@ public class ArgConsumer {
* @see IDatatype * @see IDatatype
* @see IDatatypeFor * @see IDatatypeFor
*/ */
public <T, D extends IDatatypeFor<T>> T getDatatypeForOrDefault(Class<D> datatype, T def) { public <T, D extends IDatatypeFor<T>> T getDatatypeForOrDefault(Class<D> datatype, T def) throws CommandNotEnoughArgumentsException {
List<CommandArgument> argsSnapshot = new ArrayList<>(args); List<CommandArgument> argsSnapshot = new ArrayList<>(args);
List<CommandArgument> consumedSnapshot = new ArrayList<>(consumed); List<CommandArgument> consumedSnapshot = new ArrayList<>(consumed);
try { try {
@@ -866,7 +859,7 @@ public class ArgConsumer {
* @see IDatatype * @see IDatatype
* @see IDatatypeFor * @see IDatatypeFor
*/ */
public <T, D extends IDatatypeFor<T>> T getDatatypeForOrNull(Class<D> datatype) { public <T, D extends IDatatypeFor<T>> T getDatatypeForOrNull(Class<D> datatype) throws CommandNotEnoughArgumentsException {
return getDatatypeForOrDefault(datatype, null); return getDatatypeForOrDefault(datatype, null);
} }
@@ -915,7 +908,7 @@ public class ArgConsumer {
* @see #requireMax(int) * @see #requireMax(int)
* @see #requireExactly(int) * @see #requireExactly(int)
*/ */
public void requireMin(int min) { public void requireMin(int min) throws CommandNotEnoughArgumentsException {
if (args.size() < min) { if (args.size() < min) {
throw new CommandNotEnoughArgumentsException(min + consumed.size()); throw new CommandNotEnoughArgumentsException(min + consumed.size());
} }
@@ -923,11 +916,11 @@ public class ArgConsumer {
/** /**
* @param max The maximum amount of arguments allowed. * @param max The maximum amount of arguments allowed.
* @throws CommandNotEnoughArgumentsException If there are more than {@code max} arguments left. * @throws CommandTooManyArgumentsException If there are more than {@code max} arguments left.
* @see #requireMin(int) * @see #requireMin(int)
* @see #requireExactly(int) * @see #requireExactly(int)
*/ */
public void requireMax(int max) { public void requireMax(int max) throws CommandTooManyArgumentsException {
if (args.size() > max) { if (args.size() > max) {
throw new CommandTooManyArgumentsException(max + consumed.size()); throw new CommandTooManyArgumentsException(max + consumed.size());
} }
@@ -940,7 +933,7 @@ public class ArgConsumer {
* @see #requireMin(int) * @see #requireMin(int)
* @see #requireMax(int) * @see #requireMax(int)
*/ */
public void requireExactly(int args) { public void requireExactly(int args) throws CommandException {
requireMin(args); requireMin(args);
requireMax(args); requireMax(args);
} }
@@ -18,6 +18,7 @@
package baritone.api.utils.command.helpers.pagination; package baritone.api.utils.command.helpers.pagination;
import baritone.api.utils.Helper; import baritone.api.utils.Helper;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidTypeException; import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.ITextComponent;
@@ -114,10 +115,10 @@ public class Paginator<E> implements Helper {
display(transform, null); display(transform, null);
} }
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) { public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
int page = 1; int page = 1;
consumer.requireMax(1); consumer.requireMax(1);
if (consumer.has()) { if (consumer.hasAny()) {
page = consumer.getAs(Integer.class); page = consumer.getAs(Integer.class);
if (!pagi.validPage(page)) { if (!pagi.validPage(page)) {
throw new CommandInvalidTypeException( throw new CommandInvalidTypeException(
@@ -137,47 +138,47 @@ public class Paginator<E> implements Helper {
pagi.display(transform, commandPrefix); pagi.display(transform, commandPrefix);
} }
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) { public static <T> void paginate(ArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, new Paginator<>(elems), pre, transform, commandPrefix); 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) { public static <T> void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, Arrays.asList(elems), pre, transform, commandPrefix); paginate(consumer, Arrays.asList(elems), pre, transform, commandPrefix);
} }
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform, String commandPrefix) { public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, pagi, null, transform, commandPrefix); paginate(consumer, pagi, null, transform, commandPrefix);
} }
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform, String commandPrefix) { public static <T> void paginate(ArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, new Paginator<>(elems), null, transform, commandPrefix); paginate(consumer, new Paginator<>(elems), null, transform, commandPrefix);
} }
public static <T> void paginate(ArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform, String commandPrefix) { public static <T> void paginate(ArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, Arrays.asList(elems), null, transform, commandPrefix); paginate(consumer, Arrays.asList(elems), null, transform, commandPrefix);
} }
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform) { public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, pagi, pre, transform, null); paginate(consumer, pagi, pre, transform, null);
} }
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform) { public static <T> void paginate(ArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, new Paginator<>(elems), pre, transform, null); paginate(consumer, new Paginator<>(elems), pre, transform, null);
} }
public static <T> void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform) { public static <T> void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, Arrays.asList(elems), pre, transform, null); paginate(consumer, Arrays.asList(elems), pre, transform, null);
} }
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform) { public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, pagi, null, transform, null); paginate(consumer, pagi, null, transform, null);
} }
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform) { public static <T> void paginate(ArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, new Paginator<>(elems), null, transform, null); paginate(consumer, new Paginator<>(elems), null, transform, null);
} }
public static <T> void paginate(ArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform) { public static <T> void paginate(ArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, Arrays.asList(elems), null, transform, null); paginate(consumer, Arrays.asList(elems), null, transform, null);
} }
} }
@@ -22,6 +22,7 @@ import baritone.api.Settings;
import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalAxis; import baritone.api.pathing.goals.GoalAxis;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -35,7 +36,7 @@ public class AxisCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
Goal goal = new GoalAxis(); Goal goal = new GoalAxis();
baritone.getCustomGoalProcess().setGoal(goal); baritone.getCustomGoalProcess().setGoal(goal);
@@ -21,6 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.process.IGetToBlockProcess; import baritone.api.process.IGetToBlockProcess;
import baritone.api.utils.command.Command; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -35,7 +36,7 @@ public class BlacklistCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
IGetToBlockProcess proc = baritone.getGetToBlockProcess(); IGetToBlockProcess proc = baritone.getGetToBlockProcess();
if (!proc.isActive()) { if (!proc.isActive()) {
@@ -23,6 +23,7 @@ import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeBlockPos; import baritone.api.utils.command.datatypes.RelativeBlockPos;
import baritone.api.utils.command.datatypes.RelativeFile; 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.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
@@ -42,14 +43,14 @@ public class BuildCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
File file = args.getDatatypePost(RelativeFile.class, schematicsDir).getAbsoluteFile(); File file = args.getDatatypePost(RelativeFile.class, schematicsDir).getAbsoluteFile();
if (!file.getName().toLowerCase(Locale.US).endsWith(".schematic")) { if (!file.getName().toLowerCase(Locale.US).endsWith(".schematic")) {
file = new File(file.getAbsolutePath() + ".schematic"); file = new File(file.getAbsolutePath() + ".schematic");
} }
BetterBlockPos origin = ctx.playerFeet(); BetterBlockPos origin = ctx.playerFeet();
BetterBlockPos buildOrigin; BetterBlockPos buildOrigin;
if (args.has()) { if (args.hasAny()) {
args.requireMax(3); args.requireMax(3);
buildOrigin = args.getDatatype(RelativeBlockPos.class).apply(origin); buildOrigin = args.getDatatype(RelativeBlockPos.class).apply(origin);
} else { } else {
@@ -64,7 +65,7 @@ public class BuildCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.hasExactlyOne()) { if (args.hasExactlyOne()) {
return RelativeFile.tabComplete(args, schematicsDir); return RelativeFile.tabComplete(args, schematicsDir);
} else if (args.has(2)) { } else if (args.has(2)) {
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -33,7 +34,7 @@ public class CancelCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
baritone.getPathingBehavior().cancelEverything(); baritone.getPathingBehavior().cancelEverything();
logDirect("ok canceled"); logDirect("ok canceled");
@@ -22,6 +22,7 @@ import baritone.api.Settings;
import baritone.api.cache.IRememberedInventory; import baritone.api.cache.IRememberedInventory;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@@ -41,7 +42,7 @@ public class ChestsCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
Set<Map.Entry<BlockPos, IRememberedInventory>> entries = Set<Map.Entry<BlockPos, IRememberedInventory>> entries =
ctx.worldData().getContainerMemory().getRememberedInventories().entrySet(); ctx.worldData().getContainerMemory().getRememberedInventories().entrySet();
@@ -24,6 +24,7 @@ import baritone.api.pathing.goals.GoalBlock;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeBlockPos; 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.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -38,10 +39,10 @@ public class ClearareaCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
BetterBlockPos pos1 = ctx.playerFeet(); BetterBlockPos pos1 = ctx.playerFeet();
BetterBlockPos pos2; BetterBlockPos pos2;
if (args.has()) { if (args.hasAny()) {
args.requireMax(3); args.requireMax(3);
pos2 = args.getDatatype(RelativeBlockPos.class).apply(pos1); pos2 = args.getDatatype(RelativeBlockPos.class).apply(pos1);
} else { } else {
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -33,7 +34,7 @@ public class ClickCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
baritone.openClick(); baritone.openClick();
logDirect("aight dude"); logDirect("aight dude");
@@ -21,6 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.pathing.goals.GoalBlock; import baritone.api.pathing.goals.GoalBlock;
import baritone.api.utils.command.Command; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@@ -37,7 +38,7 @@ public class ComeCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
Entity entity = mc.getRenderViewEntity(); Entity entity = mc.getRenderViewEntity();
if (entity == null) { if (entity == null) {
@@ -1,58 +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.utils.command.defaults;
import baritone.api.IBaritone;
import baritone.api.Settings;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class EmptyCommand extends Command {
public EmptyCommand(IBaritone baritone) {
super(baritone, Arrays.asList("name1", "name2"));
}
@Override
protected void executed(String label, ArgConsumer args, Settings settings) {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) {
return Stream.empty();
}
@Override
public String getShortDesc() {
return "Short description";
}
@Override
public List<String> getLongDesc() {
return Arrays.asList(
"",
"",
"Usage:",
"> "
);
}
}
@@ -22,6 +22,7 @@ import baritone.api.Settings;
import baritone.api.pathing.goals.GoalXZ; import baritone.api.pathing.goals.GoalXZ;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeGoalXZ; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -35,13 +36,13 @@ public class ExploreCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.has()) { if (args.hasAny()) {
args.requireExactly(2); args.requireExactly(2);
} else { } else {
args.requireMax(0); args.requireMax(0);
} }
GoalXZ goal = args.has() GoalXZ goal = args.hasAny()
? args.getDatatypePost(RelativeGoalXZ.class, ctx.playerFeet()) ? args.getDatatypePost(RelativeGoalXZ.class, ctx.playerFeet())
: new GoalXZ(ctx.playerFeet()); : new GoalXZ(ctx.playerFeet());
baritone.getExploreProcess().explore(goal.getX(), goal.getZ()); baritone.getExploreProcess().explore(goal.getX(), goal.getZ());
@@ -21,6 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeFile; 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.CommandInvalidStateException;
import baritone.api.utils.command.exception.CommandInvalidTypeException; import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -39,11 +40,11 @@ public class ExploreFilterCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(2); args.requireMax(2);
File file = args.getDatatypePost(RelativeFile.class, mc.gameDir.getAbsoluteFile().getParentFile()); File file = args.getDatatypePost(RelativeFile.class, mc.gameDir.getAbsoluteFile().getParentFile());
boolean invert = false; boolean invert = false;
if (args.has()) { if (args.hasAny()) {
if (args.getString().equalsIgnoreCase("invert")) { if (args.getString().equalsIgnoreCase("invert")) {
invert = true; invert = true;
} else { } else {
@@ -63,7 +64,7 @@ public class ExploreFilterCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.hasExactlyOne()) { if (args.hasExactlyOne()) {
return RelativeFile.tabComplete(args, RelativeFile.gameDir()); return RelativeFile.tabComplete(args, RelativeFile.gameDir());
} }
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -33,7 +34,7 @@ public class FarmCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
baritone.getFarmProcess().farm(); baritone.getFarmProcess().farm();
logDirect("Farming"); logDirect("Farming");
@@ -22,6 +22,7 @@ import baritone.api.Settings;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.BlockById; 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.ArgConsumer;
import net.minecraft.block.Block; import net.minecraft.block.Block;
@@ -37,9 +38,9 @@ public class FindCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
List<Block> toFind = new ArrayList<>(); List<Block> toFind = new ArrayList<>();
while (args.has()) { while (args.hasAny()) {
toFind.add(args.getDatatypeFor(BlockById.class)); toFind.add(args.getDatatypeFor(BlockById.class));
} }
BetterBlockPos origin = ctx.playerFeet(); BetterBlockPos origin = ctx.playerFeet();
@@ -24,6 +24,7 @@ import baritone.api.utils.command.datatypes.EntityClassById;
import baritone.api.utils.command.datatypes.IDatatype; import baritone.api.utils.command.datatypes.IDatatype;
import baritone.api.utils.command.datatypes.IDatatypeFor; import baritone.api.utils.command.datatypes.IDatatypeFor;
import baritone.api.utils.command.datatypes.PlayerByUsername; import baritone.api.utils.command.datatypes.PlayerByUsername;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
@@ -43,7 +44,7 @@ public class FollowCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMin(1); args.requireMin(1);
FollowGroup group; FollowGroup group;
FollowList list; FollowList list;
@@ -55,7 +56,7 @@ public class FollowCommand extends Command {
args.requireMin(2); args.requireMin(2);
group = null; group = null;
list = args.getEnum(FollowList.class); list = args.getEnum(FollowList.class);
while (args.has()) { while (args.hasAny()) {
//noinspection unchecked //noinspection unchecked
Object gotten = args.getDatatypeFor(list.datatype); Object gotten = args.getDatatypeFor(list.datatype);
if (gotten instanceof Class) { if (gotten instanceof Class) {
@@ -90,7 +91,7 @@ public class FollowCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.hasExactlyOne()) { if (args.hasExactlyOne()) {
return new TabCompleteHelper() return new TabCompleteHelper()
.append(FollowGroup.class) .append(FollowGroup.class)
@@ -21,6 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.behavior.IPathingBehavior; import baritone.api.behavior.IPathingBehavior;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -34,7 +35,7 @@ public class ForceCancelCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
IPathingBehavior pathingBehavior = baritone.getPathingBehavior(); IPathingBehavior pathingBehavior = baritone.getPathingBehavior();
pathingBehavior.cancelEverything(); pathingBehavior.cancelEverything();
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -33,7 +34,7 @@ public class GcCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
System.gc(); System.gc();
logDirect("ok called System.gc()"); logDirect("ok called System.gc()");
@@ -25,6 +25,7 @@ import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeCoordinate; import baritone.api.utils.command.datatypes.RelativeCoordinate;
import baritone.api.utils.command.datatypes.RelativeGoal; 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.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
@@ -39,9 +40,9 @@ public class GoalCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
ICustomGoalProcess goalProcess = baritone.getCustomGoalProcess(); ICustomGoalProcess goalProcess = baritone.getCustomGoalProcess();
if (args.has() && Arrays.asList("reset", "clear", "none").contains(args.peekString())) { if (args.hasAny() && Arrays.asList("reset", "clear", "none").contains(args.peekString())) {
args.requireMax(1); args.requireMax(1);
if (goalProcess.getGoal() != null) { if (goalProcess.getGoal() != null) {
goalProcess.setGoal(null); goalProcess.setGoal(null);
@@ -59,7 +60,7 @@ public class GoalCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
TabCompleteHelper helper = new TabCompleteHelper(); TabCompleteHelper helper = new TabCompleteHelper();
if (args.hasExactlyOne()) { if (args.hasExactlyOne()) {
helper.append(Stream.of("reset", "clear", "none", "~")); helper.append(Stream.of("reset", "clear", "none", "~"));
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.exception.CommandNotFoundException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.pagination.Paginator; import baritone.api.utils.command.helpers.pagination.Paginator;
@@ -46,9 +47,9 @@ public class HelpCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(1); args.requireMax(1);
if (!args.has() || args.is(Integer.class)) { if (!args.hasAny() || args.is(Integer.class)) {
Paginator.paginate( Paginator.paginate(
args, new Paginator<>( args, new Paginator<>(
CommandManager.REGISTRY.descendingStream() CommandManager.REGISTRY.descendingStream()
@@ -99,7 +100,7 @@ public class HelpCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.hasExactlyOne()) { if (args.hasExactlyOne()) {
return new TabCompleteHelper().addCommands().filterPrefix(args.getString()).stream(); return new TabCompleteHelper().addCommands().filterPrefix(args.getString()).stream();
} }
@@ -23,6 +23,7 @@ import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalInverted; import baritone.api.pathing.goals.GoalInverted;
import baritone.api.process.ICustomGoalProcess; import baritone.api.process.ICustomGoalProcess;
import baritone.api.utils.command.Command; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -37,7 +38,7 @@ public class InvertCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess(); ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess();
Goal goal; Goal goal;
@@ -23,6 +23,7 @@ import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.BlockById; import baritone.api.utils.command.datatypes.BlockById;
import baritone.api.utils.command.datatypes.ForBlockOptionalMeta; 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.ArgConsumer;
import baritone.cache.WorldScanner; import baritone.cache.WorldScanner;
@@ -38,11 +39,11 @@ public class MineCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
int quantity = args.getAsOrDefault(Integer.class, 0); int quantity = args.getAsOrDefault(Integer.class, 0);
args.requireMin(1); args.requireMin(1);
List<BlockOptionalMeta> boms = new ArrayList<>(); List<BlockOptionalMeta> boms = new ArrayList<>();
while (args.has()) { while (args.hasAny()) {
boms.add(args.getDatatypeFor(ForBlockOptionalMeta.class)); boms.add(args.getDatatypeFor(ForBlockOptionalMeta.class));
} }
WorldScanner.INSTANCE.repack(ctx); WorldScanner.INSTANCE.repack(ctx);
@@ -24,6 +24,7 @@ import baritone.api.process.ICustomGoalProcess;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeCoordinate; import baritone.api.utils.command.datatypes.RelativeCoordinate;
import baritone.api.utils.command.datatypes.RelativeGoal; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
@@ -40,10 +41,10 @@ public class PathCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess(); ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess();
Goal goal; Goal goal;
if (args.has()) { if (args.hasAny()) {
args.requireMax(3); args.requireMax(3);
goal = args.getDatatype(RelativeGoal.class).apply(ctx.playerFeet()); goal = args.getDatatype(RelativeGoal.class).apply(ctx.playerFeet());
} else if ((goal = customGoalProcess.getGoal()) == null) { } else if ((goal = customGoalProcess.getGoal()) == null) {
@@ -56,8 +57,8 @@ public class PathCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.has() && !args.has(4)) { if (args.hasAny() && !args.has(4)) {
while (args.has(2)) { while (args.has(2)) {
if (args.peekDatatypeOrNull(RelativeCoordinate.class) == null) { if (args.peekDatatypeOrNull(RelativeCoordinate.class) == null) {
break; break;
@@ -23,6 +23,7 @@ import baritone.api.process.IBaritoneProcess;
import baritone.api.process.PathingCommand; import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType; import baritone.api.process.PathingCommandType;
import baritone.api.utils.command.Command; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -81,7 +82,7 @@ public class PauseResumeCommands {
); );
pauseCommand = new Command(baritone, "pause") { pauseCommand = new Command(baritone, "pause") {
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
if (paused[0]) { if (paused[0]) {
throw new CommandInvalidStateException("Already paused"); throw new CommandInvalidStateException("Already paused");
@@ -114,7 +115,7 @@ public class PauseResumeCommands {
}; };
resumeCommand = new Command(baritone, "resume") { resumeCommand = new Command(baritone, "resume") {
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
if (!paused[0]) { if (!paused[0]) {
throw new CommandInvalidStateException("Not paused"); throw new CommandInvalidStateException("Not paused");
@@ -145,7 +146,7 @@ public class PauseResumeCommands {
}; };
pausedCommand = new Command(baritone, "paused") { pausedCommand = new Command(baritone, "paused") {
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
logDirect(String.format("Baritone is %spaused", paused[0] ? "" : "not ")); logDirect(String.format("Baritone is %spaused", paused[0] ? "" : "not "));
} }
@@ -23,6 +23,7 @@ import baritone.api.pathing.calc.IPathingControlManager;
import baritone.api.process.IBaritoneProcess; import baritone.api.process.IBaritoneProcess;
import baritone.api.process.PathingCommand; import baritone.api.process.PathingCommand;
import baritone.api.utils.command.Command; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -37,7 +38,7 @@ public class ProcCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
IPathingControlManager pathingControlManager = baritone.getPathingControlManager(); IPathingControlManager pathingControlManager = baritone.getPathingControlManager();
IBaritoneProcess process = pathingControlManager.mostRecentInControl().orElse(null); IBaritoneProcess process = pathingControlManager.mostRecentInControl().orElse(null);
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -33,7 +34,7 @@ public class ReloadAllCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
ctx.worldData().getCachedWorld().reloadAllFromDisk(); ctx.worldData().getCachedWorld().reloadAllFromDisk();
logDirect("Reloaded"); logDirect("Reloaded");
@@ -21,6 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.BetterBlockPos; import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -34,7 +35,7 @@ public class RenderCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
BetterBlockPos origin = ctx.playerFeet(); BetterBlockPos origin = ctx.playerFeet();
int renderDistance = (mc.gameSettings.renderDistanceChunks + 1) * 16; int renderDistance = (mc.gameSettings.renderDistanceChunks + 1) * 16;
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import baritone.cache.WorldScanner; import baritone.cache.WorldScanner;
@@ -34,7 +35,7 @@ public class RepackCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
logDirect(String.format("Queued %d chunks for repacking", WorldScanner.INSTANCE.repack(ctx))); logDirect(String.format("Queued %d chunks for repacking", WorldScanner.INSTANCE.repack(ctx)));
} }
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -33,7 +34,7 @@ public class SaveAllCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
ctx.worldData().getCachedWorld().save(); ctx.worldData().getCachedWorld().save();
logDirect("Saved"); logDirect("Saved");
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -33,7 +34,7 @@ public class SchematicaCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
baritone.getBuilderProcess().buildOpenSchematic(); baritone.getBuilderProcess().buildOpenSchematic();
} }
@@ -33,6 +33,7 @@ import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.ForBlockOptionalMeta; import baritone.api.utils.command.datatypes.ForBlockOptionalMeta;
import baritone.api.utils.command.datatypes.ForEnumFacing; import baritone.api.utils.command.datatypes.ForEnumFacing;
import baritone.api.utils.command.datatypes.RelativeBlockPos; 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.CommandInvalidStateException;
import baritone.api.utils.command.exception.CommandInvalidTypeException; import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -75,7 +76,7 @@ public class SelCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
Action action = Action.getByName(args.getString()); Action action = Action.getByName(args.getString());
if (action == null) { if (action == null) {
throw new CommandInvalidTypeException(args.consumed(), "an action"); throw new CommandInvalidTypeException(args.consumed(), "an action");
@@ -85,7 +86,7 @@ public class SelCommand extends Command {
throw new CommandInvalidStateException("Set pos1 first before using pos2"); throw new CommandInvalidStateException("Set pos1 first before using pos2");
} }
BetterBlockPos playerPos = mc.getRenderViewEntity() != null ? BetterBlockPos.from(new BlockPos(mc.getRenderViewEntity())) : ctx.playerFeet(); BetterBlockPos playerPos = mc.getRenderViewEntity() != null ? BetterBlockPos.from(new BlockPos(mc.getRenderViewEntity())) : ctx.playerFeet();
BetterBlockPos pos = args.has() ? args.getDatatypePost(RelativeBlockPos.class, playerPos) : playerPos; BetterBlockPos pos = args.hasAny() ? args.getDatatypePost(RelativeBlockPos.class, playerPos) : playerPos;
args.requireMax(0); args.requireMax(0);
if (action == Action.POS1) { if (action == Action.POS1) {
pos1 = pos; pos1 = pos;
@@ -186,7 +187,7 @@ public class SelCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.hasExactlyOne()) { if (args.hasExactlyOne()) {
return new TabCompleteHelper() return new TabCompleteHelper()
.append(Action.getAllNames()) .append(Action.getAllNames())
@@ -21,6 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.SettingsUtil; import baritone.api.utils.SettingsUtil;
import baritone.api.utils.command.Command; 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.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.pagination.Paginator; import baritone.api.utils.command.helpers.pagination.Paginator;
@@ -48,8 +49,8 @@ public class SetCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
String arg = args.has() ? args.getString().toLowerCase(Locale.US) : "list"; String arg = args.hasAny() ? args.getString().toLowerCase(Locale.US) : "list";
if (Arrays.asList("s", "save").contains(arg)) { if (Arrays.asList("s", "save").contains(arg)) {
SettingsUtil.save(settings); SettingsUtil.save(settings);
logDirect("Settings saved"); logDirect("Settings saved");
@@ -59,7 +60,7 @@ public class SetCommand extends Command {
boolean viewAll = Arrays.asList("all", "l", "list").contains(arg); boolean viewAll = Arrays.asList("all", "l", "list").contains(arg);
boolean paginate = viewModified || viewAll; boolean paginate = viewModified || viewAll;
if (paginate) { if (paginate) {
String search = args.has() && args.peekAsOrNull(Integer.class) == null ? args.getString() : ""; String search = args.hasAny() && args.peekAsOrNull(Integer.class) == null ? args.getString() : "";
args.requireMax(1); args.requireMax(1);
List<? extends Settings.Setting> toPaginate = List<? extends Settings.Setting> toPaginate =
(viewModified ? SettingsUtil.modifiedSettings(settings) : settings.allSettings).stream() (viewModified ? SettingsUtil.modifiedSettings(settings) : settings.allSettings).stream()
@@ -104,7 +105,7 @@ public class SetCommand extends Command {
boolean toggling = arg.equalsIgnoreCase("toggle"); boolean toggling = arg.equalsIgnoreCase("toggle");
boolean doingSomething = resetting || toggling; boolean doingSomething = resetting || toggling;
if (resetting) { if (resetting) {
if (!args.has()) { if (!args.hasAny()) {
logDirect("Please specify 'all' as an argument to reset to confirm you'd really like to do this"); logDirect("Please specify 'all' as an argument to reset to confirm you'd really like to do this");
logDirect("ALL settings will be reset. Use the 'set modified' or 'modified' commands to see what will be reset."); logDirect("ALL settings will be reset. Use the 'set modified' or 'modified' commands to see what will be reset.");
logDirect("Specify a setting name instead of 'all' to only reset one setting"); logDirect("Specify a setting name instead of 'all' to only reset one setting");
@@ -126,7 +127,7 @@ public class SetCommand extends Command {
if (setting == null) { if (setting == null) {
throw new CommandInvalidTypeException(args.consumed(), "a valid setting"); throw new CommandInvalidTypeException(args.consumed(), "a valid setting");
} }
if (!doingSomething && !args.has()) { if (!doingSomething && !args.hasAny()) {
logDirect(String.format("Value of setting %s:", setting.getName())); logDirect(String.format("Value of setting %s:", setting.getName()));
logDirect(settingValueToString(setting)); logDirect(settingValueToString(setting));
} else { } else {
@@ -184,8 +185,8 @@ public class SetCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.has()) { if (args.hasAny()) {
String arg = args.getString(); String arg = args.getString();
if (args.hasExactlyOne() && !Arrays.asList("s", "save").contains(args.peekString().toLowerCase(Locale.US))) { if (args.hasExactlyOne() && !Arrays.asList("s", "save").contains(args.peekString().toLowerCase(Locale.US))) {
if (arg.equalsIgnoreCase("reset")) { if (arg.equalsIgnoreCase("reset")) {
@@ -214,7 +215,7 @@ public class SetCommand extends Command {
return Stream.of(settingValueToString(setting)); return Stream.of(settingValueToString(setting));
} }
} }
} else if (!args.has()) { } else if (!args.hasAny()) {
return new TabCompleteHelper() return new TabCompleteHelper()
.addSettings() .addSettings()
.sortAlphabetically() .sortAlphabetically()
@@ -21,6 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.pathing.goals.GoalXZ; import baritone.api.pathing.goals.GoalXZ;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -34,7 +35,7 @@ public class ThisWayCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireExactly(1); args.requireExactly(1);
GoalXZ goal = GoalXZ.fromDirection( GoalXZ goal = GoalXZ.fromDirection(
ctx.playerFeetAsVec(), ctx.playerFeetAsVec(),
@@ -22,6 +22,7 @@ import baritone.api.Settings;
import baritone.api.pathing.goals.Goal; import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalStrictDirection; import baritone.api.pathing.goals.GoalStrictDirection;
import baritone.api.utils.command.Command; 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.ArgConsumer;
import java.util.Arrays; import java.util.Arrays;
@@ -35,7 +36,7 @@ public class TunnelCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
Goal goal = new GoalStrictDirection( Goal goal = new GoalStrictDirection(
ctx.playerFeet(), ctx.playerFeet(),
@@ -20,6 +20,7 @@ package baritone.utils.command.defaults;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.Settings; import baritone.api.Settings;
import baritone.api.utils.command.Command; 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.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -34,7 +35,7 @@ public class VersionCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
args.requireMax(0); args.requireMax(0);
String version = getClass().getPackage().getImplementationVersion(); String version = getClass().getPackage().getImplementationVersion();
if (version == null) { if (version == null) {
@@ -27,6 +27,7 @@ import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.ForWaypoints; import baritone.api.utils.command.datatypes.ForWaypoints;
import baritone.api.utils.command.datatypes.RelativeBlockPos; 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.CommandInvalidStateException;
import baritone.api.utils.command.exception.CommandInvalidTypeException; import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
@@ -52,8 +53,8 @@ public class WaypointsCommand extends Command {
} }
@Override @Override
protected void executed(String label, ArgConsumer args, Settings settings) { protected void executed(String label, ArgConsumer args, Settings settings) throws CommandException {
Action action = args.has() ? Action.getByName(args.getString()) : Action.LIST; Action action = args.hasAny() ? Action.getByName(args.getString()) : Action.LIST;
if (action == null) { if (action == null) {
throw new CommandInvalidTypeException(args.consumed(), "an action"); throw new CommandInvalidTypeException(args.consumed(), "an action");
} }
@@ -90,7 +91,7 @@ public class WaypointsCommand extends Command {
Function<IWaypoint, ITextComponent> transform = waypoint -> Function<IWaypoint, ITextComponent> transform = waypoint ->
toComponent.apply(waypoint, action == Action.LIST ? Action.INFO : action); toComponent.apply(waypoint, action == Action.LIST ? Action.INFO : action);
if (action == Action.LIST) { if (action == Action.LIST) {
IWaypoint.Tag tag = args.has() ? IWaypoint.Tag.getByName(args.peekString()) : null; IWaypoint.Tag tag = args.hasAny() ? IWaypoint.Tag.getByName(args.peekString()) : null;
if (tag != null) { if (tag != null) {
args.get(); args.get();
} }
@@ -129,8 +130,8 @@ public class WaypointsCommand extends Command {
if (tag == null) { if (tag == null) {
throw new CommandInvalidStateException(String.format("'%s' is not a tag ", args.consumedString())); throw new CommandInvalidStateException(String.format("'%s' is not a tag ", args.consumedString()));
} }
String name = args.has() ? args.getString() : ""; String name = args.hasAny() ? args.getString() : "";
BetterBlockPos pos = args.has() BetterBlockPos pos = args.hasAny()
? args.getDatatypePost(RelativeBlockPos.class, ctx.playerFeet()) ? args.getDatatypePost(RelativeBlockPos.class, ctx.playerFeet())
: ctx.playerFeet(); : ctx.playerFeet();
args.requireMax(0); args.requireMax(0);
@@ -151,7 +152,7 @@ public class WaypointsCommand extends Command {
} else { } else {
IWaypoint[] waypoints = args.getDatatypeFor(ForWaypoints.class); IWaypoint[] waypoints = args.getDatatypeFor(ForWaypoints.class);
IWaypoint waypoint = null; IWaypoint waypoint = null;
if (args.has() && args.peekString().equals("@")) { if (args.hasAny() && args.peekString().equals("@")) {
args.requireExactly(2); args.requireExactly(2);
args.get(); args.get();
long timestamp = args.getAs(Long.class); long timestamp = args.getAs(Long.class);
@@ -241,8 +242,8 @@ public class WaypointsCommand extends Command {
} }
@Override @Override
protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) { protected Stream<String> tabCompleted(String label, ArgConsumer args, Settings settings) throws CommandException {
if (args.has()) { if (args.hasAny()) {
if (args.hasExactlyOne()) { if (args.hasExactlyOne()) {
return new TabCompleteHelper() return new TabCompleteHelper()
.append(Action.getAllNames()) .append(Action.getAllNames())