Merge branch 'master' into 1.13.2

This commit is contained in:
Leijurv
2019-10-06 15:51:27 -07:00
99 changed files with 1883 additions and 1468 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ import baritone.api.process.*;
import baritone.api.selection.ISelectionManager;
import baritone.api.utils.IInputOverrideHandler;
import baritone.api.utils.IPlayerContext;
import baritone.api.utils.command.manager.ICommandManager;
import baritone.api.command.manager.ICommandManager;
/**
* @author Brady
@@ -18,12 +18,15 @@
package baritone.api;
import baritone.api.cache.IWorldScanner;
import baritone.api.command.Command;
import baritone.api.command.ICommandSystem;
import net.minecraft.client.entity.EntityPlayerSP;
import java.util.List;
import java.util.Objects;
/**
* Provides the present {@link IBaritone} instances
* Provides the present {@link IBaritone} instances, as well as non-baritone instance related APIs.
*
* @author leijurv
*/
@@ -57,7 +60,7 @@ public interface IBaritoneProvider {
*/
default IBaritone getBaritoneForPlayer(EntityPlayerSP player) {
for (IBaritone baritone : getAllBaritones()) {
if (player.equals(baritone.getPlayerContext().player())) {
if (Objects.equals(player, baritone.getPlayerContext().player())) {
return baritone;
}
}
@@ -71,4 +74,12 @@ public interface IBaritoneProvider {
* @return The {@link IWorldScanner} instance.
*/
IWorldScanner getWorldScanner();
/**
* Returns the {@link ICommandSystem} instance. This is not bound to a specific {@link IBaritone}
* instance because {@link ICommandSystem} itself controls global behavior for {@link Command}s.
*
* @return The {@link ICommandSystem} instance.
*/
ICommandSystem getCommandSystem();
}
+8
View File
@@ -802,6 +802,14 @@ public final class Settings {
*/
public final Setting<Boolean> mineScanDroppedItems = new Setting<>(true);
/**
* While mining, wait this number of milliseconds after mining an ore to see if it will drop an item
* instead of immediately going onto the next one
* <p>
* Thanks Louca
*/
public final Setting<Long> mineDropLoiterDurationMSThanksLouca = new Setting<>(250L);
/**
* Trim incorrect positions too far away, helps performance but hurts reliability in very large schematics
*/
@@ -15,14 +15,13 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command;
package baritone.api.command;
import baritone.api.IBaritone;
import baritone.api.utils.Helper;
import baritone.api.utils.IPlayerContext;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.execution.ICommandExecution;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Collections;
import java.util.List;
@@ -38,60 +37,31 @@ public abstract class Command implements Helper {
/**
* The names of this command. This is what you put after the command prefix.
*/
public final List<String> names;
protected final List<String> names;
/**
* Creates a new Baritone control command.
*
* @param names The names of this command. This is what you put after the command prefix.
*/
protected Command(IBaritone baritone, List<String> names) {
this.names = names.stream()
protected Command(IBaritone baritone, String... names) {
this.names = Collections.unmodifiableList(Stream.of(names)
.map(s -> s.toLowerCase(Locale.US))
.collect(Collectors.toList());
.collect(Collectors.toList()));
this.baritone = baritone;
this.ctx = baritone.getPlayerContext();
}
protected Command(IBaritone baritone, String name) {
this(baritone, Collections.singletonList(name));
}
/**
* Executes this command with the specified arguments.
*
* @param execution The command execution to execute this command with
*/
public final void execute(ICommandExecution execution) throws CommandException {
this.executed(execution.getLabel(), execution.getArguments());
}
/**
* Tab completes this command with the specified arguments. Any exception that is thrown by
* {@link #tabCompleted(String, ArgConsumer)} will be caught by this method, and then {@link Stream#empty()}
* will be returned.
*
* @param execution The command execution to tab complete
* @return The list of completions.
*/
public final Stream<String> tabComplete(ICommandExecution execution) {
try {
return this.tabCompleted(execution.getLabel(), execution.getArguments());
} catch (Throwable t) {
return Stream.empty();
}
}
/**
* Called when this command is executed.
*/
protected abstract void executed(String label, ArgConsumer args) throws CommandException;
public abstract void execute(String label, IArgConsumer args) throws CommandException;
/**
* Called when the command needs to tab complete. Return a Stream representing the entries to put in the completions
* list.
*/
protected abstract Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException;
public abstract Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException;
/**
* @return A <b>single-line</b> string containing a short description of this command's purpose.
@@ -109,4 +79,8 @@ public abstract class Command implements Helper {
public boolean hiddenFromHelp() {
return false;
}
public final List<String> getNames() {
return this.names;
}
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command;
package baritone.api.command;
import baritone.api.Settings;
@@ -0,0 +1,29 @@
/*
* 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.command;
import baritone.api.command.argparser.IArgParserManager;
/**
* @author Brady
* @since 10/4/2019
*/
public interface ICommandSystem {
IArgParserManager getParserManager();
}
@@ -15,9 +15,9 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.argparser;
package baritone.api.command.argparser;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.command.argument.ICommandArgument;
public interface IArgParser<T> {
@@ -27,9 +27,7 @@ public interface IArgParser<T> {
Class<T> getTarget();
/**
* A stateless argument parser is just that. It takes a {@link CommandArgument} and outputs its type.
*
* @see ArgParserManager#REGISTRY
* A stateless argument parser is just that. It takes a {@link ICommandArgument} and outputs its type.
*/
interface Stateless<T> extends IArgParser<T> {
@@ -39,14 +37,12 @@ public interface IArgParser<T> {
* @throws RuntimeException if you want the parsing to fail. The exception will be caught and turned into an
* appropriate error.
*/
T parseArg(CommandArgument arg) throws Exception;
T parseArg(ICommandArgument arg) throws Exception;
}
/**
* A stated argument parser is similar to a stateless one. It also takes a {@link CommandArgument}, but it also
* A stated argument parser is similar to a stateless one. It also takes a {@link ICommandArgument}, but it also
* takes a second argument that can be any type, referred to as the state.
*
* @see ArgParserManager#REGISTRY
*/
interface Stated<T, S> extends IArgParser<T> {
@@ -59,6 +55,6 @@ public interface IArgParser<T> {
* @throws RuntimeException if you want the parsing to fail. The exception will be caught and turned into an
* appropriate error.
*/
T parseArg(CommandArgument arg, S state) throws Exception;
T parseArg(ICommandArgument arg, S state) throws Exception;
}
}
@@ -0,0 +1,65 @@
/*
* 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.command.argparser;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.registry.Registry;
/**
* @author Brady
* @since 10/4/2019
*/
public interface IArgParserManager {
/**
* @param type The type trying to be parsed
* @return A parser that can parse arguments into this class, if found.
*/
<T> IArgParser.Stateless<T> getParserStateless(Class<T> type);
/**
* @param type The type trying to be parsed
* @return A parser that can parse arguments into this class, if found.
*/
<T, S> IArgParser.Stated<T, S> getParserStated(Class<T> type, Class<S> stateKlass);
/**
* Attempt to parse the specified argument with a stateless {@link IArgParser} that outputs the specified class.
*
* @param type The type to try and parse the argument into.
* @param arg The argument to parse.
* @return An instance of the specified class.
* @throws CommandInvalidTypeException If the parsing failed
*/
<T> T parseStateless(Class<T> type, ICommandArgument arg) throws CommandInvalidTypeException;
/**
* Attempt to parse the specified argument with a stated {@link IArgParser} that outputs the specified class.
*
* @param type The type to try and parse the argument into.
* @param arg The argument to parse.
* @param state The state to pass to the {@link IArgParser.Stated}.
* @return An instance of the specified class.
* @throws CommandInvalidTypeException If the parsing failed
* @see IArgParser.Stated
*/
<T, S> T parseStated(Class<T> type, Class<S> stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException;
Registry<IArgParser> getRegistry();
}
@@ -0,0 +1,102 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.command.argument;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.argparser.IArgParser;
import baritone.api.command.exception.CommandInvalidTypeException;
import net.minecraft.util.EnumFacing;
/**
* A {@link ICommandArgument} is an immutable object representing one command argument. It contains data on the index of
* that argument, its value, and the rest of the string that argument was found in
* <p>
* You're recommended to use {@link IArgConsumer}}s to handle these.
*
* @author Brady
* @since 10/2/2019
*/
public interface ICommandArgument {
/**
* @return The index of this command argument in the list of command arguments generated
*/
int getIndex();
/**
* @return The raw value of just this argument
*/
String getValue();
/**
* @return The raw value of the remaining arguments after this one was captured
*/
String getRawRest();
/**
* Gets an enum value from the enum class with the same name as this argument's value
* <p>
* For example if you getEnum as an {@link EnumFacing}, and this argument's value is "up", it will return {@link
* EnumFacing#UP}
*
* @param enumClass The enum class to search
* @return An enum constant of that class with the same name as this argument's value
* @throws CommandInvalidTypeException If the constant couldn't be found
* @see IArgConsumer#peekEnum(Class)
* @see IArgConsumer#peekEnum(Class, int)
* @see IArgConsumer#peekEnumOrNull(Class)
* @see IArgConsumer#peekEnumOrNull(Class, int)
* @see IArgConsumer#getEnum(Class)
* @see IArgConsumer#getEnumOrNull(Class)
*/
<E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return An instance of the specified type
* @throws CommandInvalidTypeException If the parsing failed
*/
<T> T getAs(Class<T> type) throws CommandInvalidTypeException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return If the parser succeeded
*/
<T> boolean is(Class<T> type);
/**
* Tries to use a <b>stated</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return An instance of the specified type
* @throws CommandInvalidTypeException If the parsing failed
*/
<T, S> T getAs(Class<T> type, Class<S> stateType, S state) throws CommandInvalidTypeException;
/**
* Tries to use a <b>stated</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return If the parser succeeded
*/
<T, S> boolean is(Class<T> type, Class<S> stateType, S state);
}
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.exception.CommandException;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.util.ResourceLocation;
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.entity.EntityType;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.IRegistry;
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.command.exception.CommandException;
import java.util.stream.Stream;
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.exception.CommandException;
import net.minecraft.util.EnumFacing;
import java.util.Locale;
@@ -15,13 +15,13 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.IBaritone;
import baritone.api.cache.IWaypoint;
import baritone.api.cache.IWaypointCollection;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.exception.CommandException;
import java.util.Comparator;
import java.util.stream.Stream;
@@ -15,11 +15,11 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.argparser.IArgParser;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.argparser.IArgParser;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.exception.CommandException;
import java.util.stream.Stream;
@@ -41,7 +41,7 @@ import java.util.stream.Stream;
public interface IDatatype {
/**
* Attempts to complete missing or partial input provided through the {@link ArgConsumer} provided by
* Attempts to complete missing or partial input provided through the {@link IArgConsumer}} provided by
* {@link IDatatypeContext#getConsumer()} in order to aide the user in executing commands.
* <p>
* One benefit over datatypes over {@link IArgParser}s is that instead of each command trying to guess what values
@@ -50,7 +50,7 @@ public interface IDatatype {
*
* @param ctx The argument consumer to tab complete
* @return A stream representing the strings that can be tab completed. DO NOT INCLUDE SPACES IN ANY STRINGS.
* @see ArgConsumer#tabCompleteDatatype(IDatatype)
* @see IArgConsumer#tabCompleteDatatype(IDatatype)
*/
Stream<String> tabComplete(IDatatypeContext ctx) throws CommandException;
}
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.IBaritone;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.helpers.arguments.IArgConsumer;
/**
* Provides an {@link IDatatype} with contextual information so
@@ -39,9 +39,9 @@ public interface IDatatypeContext {
IBaritone getBaritone();
/**
* Provides the {@link ArgConsumer} to fetch input information from.
* Provides the {@link IArgConsumer}} to fetch input information from.
*
* @return The context {@link ArgConsumer}.
* @return The context {@link IArgConsumer}}.
*/
ArgConsumer getConsumer();
IArgConsumer getConsumer();
}
@@ -15,9 +15,9 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.command.exception.CommandException;
import java.util.function.Supplier;
@@ -15,9 +15,9 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.command.exception.CommandException;
import java.util.function.Function;
@@ -15,9 +15,9 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.command.exception.CommandException;
/**
* @author Brady
@@ -15,11 +15,11 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.IBaritone;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.exception.CommandException;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.ITextComponent;
@@ -15,11 +15,11 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.exception.CommandException;
import java.util.stream.Stream;
@@ -32,7 +32,7 @@ public enum RelativeBlockPos implements IDatatypePost<BetterBlockPos, BetterBloc
origin = BetterBlockPos.ORIGIN;
}
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
return new BetterBlockPos(
consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.x),
consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.y),
@@ -42,7 +42,7 @@ public enum RelativeBlockPos implements IDatatypePost<BetterBlockPos, BetterBloc
@Override
public Stream<String> tabComplete(IDatatypeContext ctx) throws CommandException {
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
if (consumer.hasAny() && !consumer.has(4)) {
while (consumer.has(2)) {
if (consumer.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) == null) {
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.exception.CommandException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -26,8 +26,7 @@ import java.util.stream.Stream;
public enum RelativeCoordinate implements IDatatypePost<Double, Double> {
INSTANCE;
private static Pattern PATTERN = Pattern.compile("^(~?)([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)|)$");
private static Pattern PATTERN = Pattern.compile("^(~?)([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)([k-k]?)|)$");
@Override
public Double apply(IDatatypeContext ctx, Double origin) throws CommandException {
@@ -41,7 +40,12 @@ public enum RelativeCoordinate implements IDatatypePost<Double, Double> {
}
boolean isRelative = !matcher.group(1).isEmpty();
double offset = matcher.group(2).isEmpty() ? 0 : Double.parseDouble(matcher.group(2));
double offset = matcher.group(2).isEmpty() ? 0 : Double.parseDouble(matcher.group(2).replaceAll("k", ""));
if (matcher.group(2).contains("k")) {
offset *= 1000;
}
if (isRelative) {
return origin + offset;
@@ -51,7 +55,7 @@ public enum RelativeCoordinate implements IDatatypePost<Double, Double> {
@Override
public Stream<String> tabComplete(IDatatypeContext ctx) throws CommandException {
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
if (!consumer.has(2) && consumer.getString().matches("^(~|$)")) {
return Stream.of("~");
}
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.exception.CommandException;
import java.io.File;
import java.io.IOException;
@@ -70,7 +70,7 @@ public enum RelativeFile implements IDatatypePost<File, File> {
}
}
public static Stream<String> tabComplete(ArgConsumer consumer, File base0) throws CommandException {
public static Stream<String> tabComplete(IArgConsumer consumer, File base0) throws CommandException {
// I will not make the caller deal with this, seriously
// Tab complete code is beautiful and I'm not going to bloat it with dumb ass checked exception bullshit -LoganDark
@@ -15,15 +15,15 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalBlock;
import baritone.api.pathing.goals.GoalXZ;
import baritone.api.pathing.goals.GoalYLevel;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.exception.CommandException;
import net.minecraft.util.math.MathHelper;
import java.util.ArrayList;
@@ -38,10 +38,10 @@ public enum RelativeGoal implements IDatatypePost<Goal, BetterBlockPos> {
if (origin == null) {
origin = BetterBlockPos.ORIGIN;
}
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
List<IDatatypePostFunction<Double, Double>> coords = new ArrayList<>();
final ArgConsumer copy = consumer.copy(); // This is a hack and should be fixed in the future probably
final IArgConsumer copy = consumer.copy(); // This is a hack and should be fixed in the future probably
for (int i = 0; i < 3; i++) {
if (copy.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) != null) {
coords.add(o -> consumer.getDatatypePost(RelativeCoordinate.INSTANCE, o));
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.pathing.goals.GoalBlock;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.exception.CommandException;
import net.minecraft.util.math.MathHelper;
import java.util.stream.Stream;
@@ -34,7 +34,7 @@ public enum RelativeGoalBlock implements IDatatypePost<GoalBlock, BetterBlockPos
origin = BetterBlockPos.ORIGIN;
}
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
return new GoalBlock(
MathHelper.floor(consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.x)),
MathHelper.floor(consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.y)),
@@ -44,7 +44,7 @@ public enum RelativeGoalBlock implements IDatatypePost<GoalBlock, BetterBlockPos
@Override
public Stream<String> tabComplete(IDatatypeContext ctx) {
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
if (consumer.hasAtMost(3)) {
return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE);
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.pathing.goals.GoalXZ;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.exception.CommandException;
import net.minecraft.util.math.MathHelper;
import java.util.stream.Stream;
@@ -34,7 +34,7 @@ public enum RelativeGoalXZ implements IDatatypePost<GoalXZ, BetterBlockPos> {
origin = BetterBlockPos.ORIGIN;
}
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
return new GoalXZ(
MathHelper.floor(consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.x)),
MathHelper.floor(consumer.getDatatypePost(RelativeCoordinate.INSTANCE, (double) origin.y))
@@ -43,7 +43,7 @@ public enum RelativeGoalXZ implements IDatatypePost<GoalXZ, BetterBlockPos> {
@Override
public Stream<String> tabComplete(IDatatypeContext ctx) {
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
if (consumer.hasAtMost(2)) {
return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE);
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.datatypes;
package baritone.api.command.datatypes;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.pathing.goals.GoalYLevel;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.exception.CommandException;
import net.minecraft.util.math.MathHelper;
import java.util.stream.Stream;
@@ -41,7 +41,7 @@ public enum RelativeGoalYLevel implements IDatatypePost<GoalYLevel, BetterBlockP
@Override
public Stream<String> tabComplete(IDatatypeContext ctx) {
final ArgConsumer consumer = ctx.getConsumer();
final IArgConsumer consumer = ctx.getConsumer();
if (consumer.hasAtMost(1)) {
return consumer.tabCompleteDatatype(RelativeCoordinate.INSTANCE);
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
public abstract class CommandErrorMessageException extends CommandException {
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
public abstract class CommandException extends Exception implements ICommandException {
@@ -15,18 +15,18 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.command.argument.ICommandArgument;
public abstract class CommandInvalidArgumentException extends CommandErrorMessageException {
public final CommandArgument arg;
public final ICommandArgument arg;
protected CommandInvalidArgumentException(CommandArgument arg, String reason) {
protected CommandInvalidArgumentException(ICommandArgument arg, String reason) {
super(String.format(
"Error at argument #%s: %s",
arg.index == -1 ? "<unknown>" : Integer.toString(arg.index + 1),
arg.getIndex() == -1 ? "<unknown>" : Integer.toString(arg.getIndex() + 1),
reason
));
this.arg = arg;
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
public class CommandInvalidStateException extends CommandErrorMessageException {
@@ -15,25 +15,25 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.command.argument.ICommandArgument;
public class CommandInvalidTypeException extends CommandInvalidArgumentException {
public CommandInvalidTypeException(CommandArgument arg, String expected) {
public CommandInvalidTypeException(ICommandArgument arg, String expected) {
super(arg, String.format("Expected %s", expected));
}
public CommandInvalidTypeException(CommandArgument arg, String expected, Throwable cause) {
public CommandInvalidTypeException(ICommandArgument arg, String expected, Throwable cause) {
super(arg, String.format("Expected %s.\nMore details: %s", expected, cause.getMessage()));
}
public CommandInvalidTypeException(CommandArgument arg, String expected, String got) {
public CommandInvalidTypeException(ICommandArgument arg, String expected, String got) {
super(arg, String.format("Expected %s, but got %s instead", expected, got));
}
public CommandInvalidTypeException(CommandArgument arg, String expected, String got, Throwable cause) {
public CommandInvalidTypeException(ICommandArgument arg, String expected, String got, Throwable cause) {
super(arg, String.format("Expected %s, but got %s instead.\nMore details: %s", expected, got, cause.getMessage()));
}
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
public class CommandNoParserForTypeException extends CommandUnhandledException {
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
public class CommandNotEnoughArgumentsException extends CommandErrorMessageException {
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.command.Command;
import baritone.api.command.argument.ICommandArgument;
import java.util.List;
@@ -34,7 +34,7 @@ public class CommandNotFoundException extends CommandException {
}
@Override
public void handle(Command command, List<CommandArgument> args) {
public void handle(Command command, List<ICommandArgument> args) {
HELPER.logDirect(getMessage());
}
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
public class CommandTooManyArgumentsException extends CommandErrorMessageException {
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.command.Command;
import baritone.api.command.argument.ICommandArgument;
import net.minecraft.util.text.TextFormatting;
import java.util.List;
@@ -36,7 +36,7 @@ public class CommandUnhandledException extends RuntimeException implements IComm
}
@Override
public void handle(Command command, List<CommandArgument> args) {
public void handle(Command command, List<ICommandArgument> args) {
HELPER.logDirect("An unhandled exception occurred." +
"The error is in your game's log, please report this at https://github.com/cabaletta/baritone/issues",
TextFormatting.RED);
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.exception;
package baritone.api.command.exception;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.command.Command;
import baritone.api.command.argument.ICommandArgument;
import net.minecraft.util.text.TextFormatting;
import java.util.List;
@@ -49,7 +49,7 @@ public interface ICommandException {
* @param command The command that threw it.
* @param args The arguments the command was called with.
*/
default void handle(Command command, List<CommandArgument> args) {
default void handle(Command command, List<ICommandArgument> args) {
HELPER.logDirect(this.getMessage(), TextFormatting.RED);
}
}
@@ -15,173 +15,120 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.helpers.arguments;
package baritone.api.command.helpers.arguments;
import baritone.api.IBaritone;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandTooManyArgumentsException;
import baritone.api.utils.Helper;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argparser.IArgParser;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.datatypes.IDatatype;
import baritone.api.utils.command.datatypes.IDatatypeContext;
import baritone.api.utils.command.datatypes.IDatatypeFor;
import baritone.api.utils.command.datatypes.IDatatypePost;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.exception.CommandTooManyArgumentsException;
import baritone.api.utils.command.manager.ICommandManager;
import baritone.api.command.argparser.IArgParser;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.datatypes.IDatatype;
import baritone.api.command.datatypes.IDatatypeFor;
import baritone.api.command.datatypes.IDatatypePost;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.exception.CommandNotEnoughArgumentsException;
import net.minecraft.util.EnumFacing;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;
/**
* The {@link ArgConsumer} is how {@link Command}s read the arguments passed to them. This class has many benefits:
* The {@link IArgConsumer} is how {@link Command}s read the arguments passed to them. This class has many benefits:
*
* <ul>
* <li>Mutability. The whole concept of the {@link ArgConsumer} is to let you gradually consume arguments in any way
* <li>Mutability. The whole concept of the {@link IArgConsumer}} is to let you gradually consume arguments in any way
* you'd like. You can change your consumption based on earlier arguments, for subcommands for example.</li>
* <li>You don't need to keep track of your consumption. The {@link ArgConsumer} keeps track of the arguments you
* <li>You don't need to keep track of your consumption. The {@link IArgConsumer}} keeps track of the arguments you
* consume so that it can throw detailed exceptions whenever something is out of the ordinary. Additionally, if you
* need to retrieve an argument after you've already consumed it - look no further than {@link #consumed()}!</li>
* <li>Easy retrieval of many different types. If you need to retrieve an instance of an int or float for example,
* look no further than {@link #getAs(Class)}. If you need a more powerful way of retrieving data, try out the many
* {@code getDatatype...} methods.</li>
* <li>It's very easy to throw detailed exceptions. The {@link ArgConsumer} has many different methods that can
* <li>It's very easy to throw detailed exceptions. The {@link IArgConsumer}} has many different methods that can
* enforce the number of arguments, the type of arguments, and more, throwing different types of
* {@link CommandException}s if something seems off. You're recommended to do all validation and store all needed
* data in variables BEFORE logging any data to chat via {@link Helper#logDirect(String)}, so that the error
* handlers can do their job and log the error to chat.</li>
* </ul>
*/
public class ArgConsumer {
public interface IArgConsumer {
/**
* The parent {@link ICommandManager} for this {@link ArgConsumer}. Used by {@link #context}.
*/
private final ICommandManager manager;
LinkedList<ICommandArgument> getArgs();
/**
* The {@link IDatatypeContext} instance for this {@link ArgConsumer}, passed to
* datatypes when an operation is performed upon them.
*
* @see IDatatype
* @see IDatatypeContext
*/
private final IDatatypeContext context;
/**
* The list of arguments in this ArgConsumer
*/
public final LinkedList<CommandArgument> args;
/**
* The list of consumed arguments for this ArgConsumer. The most recently consumed argument is the last one
*/
public final Deque<CommandArgument> consumed;
private ArgConsumer(ICommandManager manager, Deque<CommandArgument> args, Deque<CommandArgument> consumed) {
this.manager = manager;
this.context = this.new Context();
this.args = new LinkedList<>(args);
this.consumed = new LinkedList<>(consumed);
}
public ArgConsumer(ICommandManager manager, List<CommandArgument> args) {
this(manager, new LinkedList<>(args), new LinkedList<>());
}
Deque<ICommandArgument> getConsumed();
/**
* @param num The number of arguments to check for
* @return {@code true} if there are <i>at least</i> {@code num} arguments left in this {@link ArgConsumer}
* @return {@code true} if there are <i>at least</i> {@code num} arguments left in this {@link IArgConsumer}}
* @see #hasAny()
* @see #hasAtMost(int)
* @see #hasExactly(int)
*/
public boolean has(int num) {
return args.size() >= num;
}
boolean has(int num);
/**
* @return {@code true} if there is <i>at least</i> 1 argument left in this {@link ArgConsumer}
* @return {@code true} if there is <i>at least</i> 1 argument left in this {@link IArgConsumer}}
* @see #has(int)
* @see #hasAtMostOne()
* @see #hasExactlyOne()
*/
public boolean hasAny() {
return has(1);
}
boolean hasAny();
/**
* @param num The number of arguments to check for
* @return {@code true} if there are <i>at most</i> {@code num} arguments left in this {@link ArgConsumer}
* @return {@code true} if there are <i>at most</i> {@code num} arguments left in this {@link IArgConsumer}}
* @see #has(int)
* @see #hasAtMost(int)
* @see #hasExactly(int)
*/
public boolean hasAtMost(int num) {
return args.size() <= num;
}
boolean hasAtMost(int num);
/**
* @return {@code true} if there is <i>at most</i> 1 argument left in this {@link ArgConsumer}
* @return {@code true} if there is <i>at most</i> 1 argument left in this {@link IArgConsumer}}
* @see #hasAny()
* @see #hasAtMostOne()
* @see #hasExactlyOne()
*/
public boolean hasAtMostOne() {
return hasAtMost(1);
}
boolean hasAtMostOne();
/**
* @param num The number of arguments to check for
* @return {@code true} if there are <i>exactly</i> {@code num} arguments left in this {@link ArgConsumer}
* @return {@code true} if there are <i>exactly</i> {@code num} arguments left in this {@link IArgConsumer}}
* @see #has(int)
* @see #hasAtMost(int)
*/
public boolean hasExactly(int num) {
return args.size() == num;
}
boolean hasExactly(int num);
/**
* @return {@code true} if there is <i>exactly</i> 1 argument left in this {@link ArgConsumer}
* @return {@code true} if there is <i>exactly</i> 1 argument left in this {@link IArgConsumer}}
* @see #hasAny()
* @see #hasAtMostOne()
*/
public boolean hasExactlyOne() {
return hasExactly(1);
}
boolean hasExactlyOne();
/**
* @param index The index to peek
* @return The argument at index {@code index} in this {@link ArgConsumer}, with 0 being the next one. This does not
* mutate the {@link ArgConsumer}
* @return The argument at index {@code index} in this {@link IArgConsumer}}, with 0 being the next one. This does not
* mutate the {@link IArgConsumer}}
* @throws CommandNotEnoughArgumentsException If there is less than {@code index + 1} arguments left
* @see #peek()
* @see #peekString(int)
* @see #peekAs(Class, int)
* @see #get()
*/
public CommandArgument peek(int index) throws CommandNotEnoughArgumentsException {
requireMin(index + 1);
return args.get(index);
}
ICommandArgument peek(int index) throws CommandNotEnoughArgumentsException;
/**
* @return The next argument in this {@link ArgConsumer}. This does not mutate the {@link ArgConsumer}
* @return The next argument in this {@link IArgConsumer}}. This does not mutate the {@link IArgConsumer}}
* @throws CommandNotEnoughArgumentsException If there is less than one argument left
* @see #peek(int)
* @see #peekString()
* @see #peekAs(Class)
* @see #get()
*/
public CommandArgument peek() throws CommandNotEnoughArgumentsException {
return peek(0);
}
ICommandArgument peek() throws CommandNotEnoughArgumentsException;
/**
* @param index The index to peek
@@ -192,9 +139,7 @@ public class ArgConsumer {
* @see #peek()
* @see #getAs(Class)
*/
public boolean is(Class<?> type, int index) throws CommandNotEnoughArgumentsException {
return peek(index).is(type);
}
boolean is(Class<?> type, int index) throws CommandNotEnoughArgumentsException;
/**
* @param type The type to check for
@@ -204,31 +149,25 @@ public class ArgConsumer {
* @see #peek()
* @see #getAs(Class)
*/
public boolean is(Class<?> type) throws CommandNotEnoughArgumentsException {
return is(type, 0);
}
boolean is(Class<?> type) throws CommandNotEnoughArgumentsException;
/**
* @param index The index to peek
* @return The value of the argument at index {@code index} in this {@link ArgConsumer}, with 0 being the next one
* This does not mutate the {@link ArgConsumer}
* @return The value of the argument at index {@code index} in this {@link IArgConsumer}}, with 0 being the next one
* This does not mutate the {@link IArgConsumer}}
* @throws CommandNotEnoughArgumentsException If there is less than {@code index + 1} arguments left
* @see #peek()
* @see #peekString()
*/
public String peekString(int index) throws CommandNotEnoughArgumentsException {
return peek(index).value;
}
String peekString(int index) throws CommandNotEnoughArgumentsException;
/**
* @return The value of the next argument in this {@link ArgConsumer}. This does not mutate the {@link ArgConsumer}
* @return The value of the next argument in this {@link IArgConsumer}}. This does not mutate the {@link IArgConsumer}}
* @throws CommandNotEnoughArgumentsException If there is less than one argument left
* @see #peekString(int)
* @see #getString()
*/
public String peekString() throws CommandNotEnoughArgumentsException {
return peekString(0);
}
String peekString() throws CommandNotEnoughArgumentsException;
/**
* @param index The index to peek
@@ -238,11 +177,9 @@ public class ArgConsumer {
* @throws java.util.NoSuchElementException If the constant couldn't be found
* @see #peekEnumOrNull(Class)
* @see #getEnum(Class)
* @see CommandArgument#getEnum(Class)
* @see ICommandArgument#getEnum(Class)
*/
public <E extends Enum<?>> E peekEnum(Class<E> enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getEnum(enumClass);
}
<E extends Enum<?>> E peekEnum(Class<E> enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* @param enumClass The class to search
@@ -251,11 +188,9 @@ public class ArgConsumer {
* @throws CommandInvalidTypeException If the constant couldn't be found
* @see #peekEnumOrNull(Class)
* @see #getEnum(Class)
* @see CommandArgument#getEnum(Class)
* @see ICommandArgument#getEnum(Class)
*/
public <E extends Enum<?>> E peekEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekEnum(enumClass, 0);
}
<E extends Enum<?>> E peekEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* @param index The index to peek
@@ -264,15 +199,9 @@ public class ArgConsumer {
* next argument's value. If no constant could be found, null
* @see #peekEnum(Class)
* @see #getEnumOrNull(Class)
* @see CommandArgument#getEnum(Class)
* @see ICommandArgument#getEnum(Class)
*/
public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass, int index) throws CommandNotEnoughArgumentsException {
try {
return peekEnum(enumClass, index);
} catch (CommandInvalidTypeException e) {
return null;
}
}
<E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass, int index) throws CommandNotEnoughArgumentsException;
/**
* @param enumClass The class to search
@@ -280,11 +209,9 @@ public class ArgConsumer {
* next argument's value. If no constant could be found, null
* @see #peekEnum(Class)
* @see #getEnumOrNull(Class)
* @see CommandArgument#getEnum(Class)
* @see ICommandArgument#getEnum(Class)
*/
public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return peekEnumOrNull(enumClass, 0);
}
<E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the argument at the specified index into the specified
@@ -292,7 +219,7 @@ public class ArgConsumer {
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @param index The index to peek
@@ -303,16 +230,14 @@ public class ArgConsumer {
* @see #peekAsOrDefault(Class, Object, int)
* @see #peekAsOrNull(Class, int)
*/
public <T> T peekAs(Class<T> type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getAs(type);
}
<T> T peekAs(Class<T> type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @return An instance of the specified type
@@ -322,9 +247,7 @@ public class ArgConsumer {
* @see #peekAsOrDefault(Class, Object)
* @see #peekAsOrNull(Class)
*/
public <T> T peekAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekAs(type, 0);
}
<T> T peekAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the argument at the specified index into the specified
@@ -332,7 +255,7 @@ public class ArgConsumer {
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @param def The value to return if the argument can't be parsed
@@ -343,20 +266,14 @@ public class ArgConsumer {
* @see #peekAs(Class, int)
* @see #peekAsOrNull(Class, int)
*/
public <T> T peekAsOrDefault(Class<T> type, T def, int index) throws CommandNotEnoughArgumentsException {
try {
return peekAs(type, index);
} catch (CommandInvalidTypeException e) {
return def;
}
}
<T> T peekAsOrDefault(Class<T> type, T def, int index) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @param def The value to return if the argument can't be parsed
@@ -366,9 +283,7 @@ public class ArgConsumer {
* @see #peekAs(Class)
* @see #peekAsOrNull(Class)
*/
public <T> T peekAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, def, 0);
}
<T> T peekAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the argument at the specified index into the specified
@@ -376,7 +291,7 @@ public class ArgConsumer {
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @param index The index to peek
@@ -386,16 +301,14 @@ public class ArgConsumer {
* @see #peekAs(Class, int)
* @see #peekAsOrDefault(Class, Object, int)
*/
public <T> T peekAsOrNull(Class<T> type, int index) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, null, index);
}
<T> T peekAsOrNull(Class<T> type, int index) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @return An instance of the specified type, or {@code null} if it couldn't be parsed
@@ -404,48 +317,30 @@ public class ArgConsumer {
* @see #peekAs(Class)
* @see #peekAsOrDefault(Class, Object)
*/
public <T> T peekAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return peekAsOrNull(type, 0);
}
<T> T peekAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException;
public <T> T peekDatatype(IDatatypeFor<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypeFor(datatype);
}
<T> T peekDatatype(IDatatypeFor<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, O> T peekDatatype(IDatatypePost<T, O> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return this.peekDatatype(datatype, null);
}
<T, O> T peekDatatype(IDatatypePost<T, O> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, O> T peekDatatype(IDatatypePost<T, O> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original);
}
<T, O> T peekDatatype(IDatatypePost<T, O> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T> T peekDatatypeOrNull(IDatatypeFor<T> datatype) {
return copy().getDatatypeForOrNull(datatype);
}
<T> T peekDatatypeOrNull(IDatatypeFor<T> datatype);
public <T, O> T peekDatatypeOrNull(IDatatypePost<T, O> datatype) {
return copy().getDatatypePostOrNull(datatype, null);
}
<T, O> T peekDatatypeOrNull(IDatatypePost<T, O> datatype);
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original);
}
<T, O, D extends IDatatypePost<T, O>> T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrDefault(D datatype, O original, T def) {
return copy().getDatatypePostOrDefault(datatype, original, def);
}
<T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrDefault(D datatype, O original, T def);
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrNull(D datatype, O original) {
return peekDatatypePostOrDefault(datatype, original, null);
}
<T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrNull(D datatype, O original);
/**
* Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
* <p>
* Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method.
*
@@ -454,16 +349,14 @@ public class ArgConsumer {
* @see IDatatype
* @see IDatatypeFor
*/
public <T, D extends IDatatypeFor<T>> T peekDatatypeFor(Class<D> datatype) {
return copy().peekDatatypeFor(datatype);
}
<T, D extends IDatatypeFor<T>> T peekDatatypeFor(Class<D> datatype);
/**
* Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
* <p>
* Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method.
*
@@ -473,16 +366,14 @@ public class ArgConsumer {
* @see IDatatype
* @see IDatatypeFor
*/
public <T, D extends IDatatypeFor<T>> T peekDatatypeForOrDefault(Class<D> datatype, T def) {
return copy().peekDatatypeForOrDefault(datatype, def);
}
<T, D extends IDatatypeFor<T>> T peekDatatypeForOrDefault(Class<D> datatype, T def);
/**
* Attempts to get the specified {@link IDatatypeFor} from this ArgConsumer
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
* <p>
* Since this is a peek operation, this ArgConsumer will not be mutated by any call to this method.
*
@@ -491,9 +382,7 @@ public class ArgConsumer {
* @see IDatatype
* @see IDatatypeFor
*/
public <T, D extends IDatatypeFor<T>> T peekDatatypeForOrNull(Class<D> datatype) {
return peekDatatypeForOrDefault(datatype, null);
}
<T, D extends IDatatypeFor<T>> T peekDatatypeForOrNull(Class<D> datatype);
/**
* Gets the next argument and returns it. This consumes the first argument so that subsequent calls will return
@@ -502,12 +391,7 @@ public class ArgConsumer {
* @return The next argument
* @throws CommandNotEnoughArgumentsException If there's less than one argument left
*/
public CommandArgument get() throws CommandNotEnoughArgumentsException {
requireMin(1);
CommandArgument arg = args.removeFirst();
consumed.add(arg);
return arg;
}
ICommandArgument get() throws CommandNotEnoughArgumentsException;
/**
* Gets the value of the next argument and returns it. This consumes the first argument so that subsequent calls
@@ -516,9 +400,7 @@ public class ArgConsumer {
* @return The value of the next argument
* @throws CommandNotEnoughArgumentsException If there's less than one argument left
*/
public String getString() throws CommandNotEnoughArgumentsException {
return get().value;
}
String getString() throws CommandNotEnoughArgumentsException;
/**
* Gets an enum value from the enum class with the same name as the next argument's value
@@ -531,11 +413,9 @@ public class ArgConsumer {
* @throws CommandInvalidTypeException If the constant couldn't be found
* @see #peekEnum(Class)
* @see #getEnumOrNull(Class)
* @see CommandArgument#getEnum(Class)
* @see ICommandArgument#getEnum(Class)
*/
public <E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getEnum(enumClass);
}
<E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* Gets an enum value from the enum class with the same name as the next argument's value
@@ -550,16 +430,9 @@ public class ArgConsumer {
* @see #getEnum(Class)
* @see #getEnumOrNull(Class)
* @see #peekEnumOrNull(Class)
* @see CommandArgument#getEnum(Class)
* @see ICommandArgument#getEnum(Class)
*/
public <E extends Enum<?>> E getEnumOrDefault(Class<E> enumClass, E def) throws CommandNotEnoughArgumentsException {
try {
peekEnum(enumClass);
return getEnum(enumClass);
} catch (CommandInvalidTypeException e) {
return def;
}
}
<E extends Enum<?>> E getEnumOrDefault(Class<E> enumClass, E def) throws CommandNotEnoughArgumentsException;
/**
* Gets an enum value from the enum class with the same name as the next argument's value
@@ -573,18 +446,16 @@ public class ArgConsumer {
* @see #getEnum(Class)
* @see #getEnumOrDefault(Class, Enum)
* @see #peekEnumOrNull(Class)
* @see CommandArgument#getEnum(Class)
* @see ICommandArgument#getEnum(Class)
*/
public <E extends Enum<?>> E getEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return getEnumOrDefault(enumClass, null);
}
<E extends Enum<?>> E getEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @return An instance of the specified type
@@ -597,16 +468,14 @@ public class ArgConsumer {
* @see #peekAsOrDefault(Class, Object, int)
* @see #peekAsOrNull(Class, int)
*/
public <T> T getAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getAs(type);
}
<T> T getAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @param def The default value
@@ -619,22 +488,14 @@ public class ArgConsumer {
* @see #peekAsOrDefault(Class, Object, int)
* @see #peekAsOrNull(Class, int)
*/
public <T> T getAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
try {
T val = peek().getAs(type);
get();
return val;
} catch (CommandInvalidTypeException e) {
return def;
}
}
<T> T getAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException;
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse the next argument into the specified class
* <p>
* A critical difference between {@link IDatatype}s and {@link IArgParser}s is how many arguments they can take.
* While {@link IArgParser}s always operate on a single argument's value, {@link IDatatype}s get access to the entire
* {@link ArgConsumer}.
* {@link IArgConsumer}}.
*
* @param type The type to peek as
* @return An instance of the specified type, or {@code null} if it couldn't be parsed
@@ -646,75 +507,25 @@ public class ArgConsumer {
* @see #peekAsOrDefault(Class, Object, int)
* @see #peekAsOrNull(Class, int)
*/
public <T> T getAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return getAsOrDefault(type, null);
}
<T> T getAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException;
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
try {
return datatype.apply(this.context, original);
} catch (Exception e) {
e.printStackTrace();
throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getClass().getSimpleName());
}
}
<T, O, D extends IDatatypePost<T, O>> T getDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrDefault(D datatype, O original, T _default) {
final List<CommandArgument> argsSnapshot = new ArrayList<>(this.args);
final List<CommandArgument> consumedSnapshot = new ArrayList<>(this.consumed);
try {
return this.getDatatypePost(datatype, original);
} catch (Exception e) {
this.args.clear();
this.args.addAll(argsSnapshot);
this.consumed.clear();
this.consumed.addAll(consumedSnapshot);
return _default;
}
}
<T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrDefault(D datatype, O original, T _default);
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrNull(D datatype, O original) {
return this.getDatatypePostOrDefault(datatype, original, null);
}
<T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrNull(D datatype, O original);
public <T, D extends IDatatypeFor<T>> T getDatatypeFor(D datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
try {
return datatype.get(this.context);
} catch (Exception e) {
throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getClass().getSimpleName());
}
}
<T, D extends IDatatypeFor<T>> T getDatatypeFor(D datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException;
public <T, D extends IDatatypeFor<T>> T getDatatypeForOrDefault(D datatype, T def) {
final List<CommandArgument> argsSnapshot = new ArrayList<>(this.args);
final List<CommandArgument> consumedSnapshot = new ArrayList<>(this.consumed);
try {
return this.getDatatypeFor(datatype);
} catch (Exception e) {
this.args.clear();
this.args.addAll(argsSnapshot);
this.consumed.clear();
this.consumed.addAll(consumedSnapshot);
return def;
}
}
<T, D extends IDatatypeFor<T>> T getDatatypeForOrDefault(D datatype, T def);
public <T, D extends IDatatypeFor<T>> T getDatatypeForOrNull(D datatype) {
return this.getDatatypeForOrDefault(datatype, null);
}
<T, D extends IDatatypeFor<T>> T getDatatypeForOrNull(D datatype);
public <T extends IDatatype> Stream<String> tabCompleteDatatype(T datatype) {
try {
return datatype.tabComplete(this.context);
} catch (Exception e) {
e.printStackTrace();
}
return Stream.empty();
}
<T extends IDatatype> Stream<String> tabCompleteDatatype(T datatype);
/**
* Returns the "raw rest" of the string. For example, from a string <code>arg1 arg2&nbsp;&nbsp;arg3</code>, split
* into three {@link CommandArgument}s {@code "arg1"}, {@code "arg2"}, and {@code "arg3"}:
* into three {@link ICommandArgument}s {@code "arg1"}, {@code "arg2"}, and {@code "arg3"}:
*
* <ul>
* <li>{@code rawRest()} would return <code>arg1 arg2&nbsp;&nbsp;arg3</code></li>
@@ -726,9 +537,7 @@ public class ArgConsumer {
*
* @return The "raw rest" of the string.
*/
public String rawRest() {
return args.size() > 0 ? args.getFirst().rawRest : "";
}
String rawRest();
/**
* @param min The minimum amount of arguments to require.
@@ -736,11 +545,7 @@ public class ArgConsumer {
* @see #requireMax(int)
* @see #requireExactly(int)
*/
public void requireMin(int min) throws CommandNotEnoughArgumentsException {
if (args.size() < min) {
throw new CommandNotEnoughArgumentsException(min + consumed.size());
}
}
void requireMin(int min) throws CommandNotEnoughArgumentsException;
/**
* @param max The maximum amount of arguments allowed.
@@ -748,11 +553,7 @@ public class ArgConsumer {
* @see #requireMin(int)
* @see #requireExactly(int)
*/
public void requireMax(int max) throws CommandTooManyArgumentsException {
if (args.size() > max) {
throw new CommandTooManyArgumentsException(max + consumed.size());
}
}
void requireMax(int max) throws CommandTooManyArgumentsException;
/**
* @param args The exact amount of arguments to require.
@@ -761,58 +562,34 @@ public class ArgConsumer {
* @see #requireMin(int)
* @see #requireMax(int)
*/
public void requireExactly(int args) throws CommandException {
requireMin(args);
requireMax(args);
}
void requireExactly(int args) throws CommandException;
/**
* @return If this {@link ArgConsumer} has consumed at least one argument.
* @return If this {@link IArgConsumer}} has consumed at least one argument.
* @see #consumed()
* @see #consumedString()
*/
public boolean hasConsumed() {
return !consumed.isEmpty();
}
boolean hasConsumed();
/**
* @return The last argument this {@link ArgConsumer} has consumed, or the {@link CommandArgument#unknown() unknown}
* argument if no arguments have been consumed yet.
* @return The last argument this {@link IArgConsumer}} has consumed, or an "unknown" argument, indicated by a
* comamnd argument index that has a value of {@code -1}, if no arguments have been consumed yet.
* @see #consumedString()
* @see #hasConsumed()
*/
public CommandArgument consumed() {
return consumed.size() > 0 ? consumed.getLast() : CommandArgument.unknown();
}
ICommandArgument consumed();
/**
* @return The value of thelast argument this {@link ArgConsumer} has consumed, or an empty string if no arguments
* @return The value of thelast argument this {@link IArgConsumer}} has consumed, or an empty string if no arguments
* have been consumed yet
* @see #consumed()
* @see #hasConsumed()
*/
public String consumedString() {
return consumed().value;
}
String consumedString();
/**
* @return A copy of this {@link ArgConsumer}. It has the same arguments (both consumed and not), but does not
* @return A copy of this {@link IArgConsumer}}. It has the same arguments (both consumed and not), but does not
* affect or mutate this instance. Useful for the various {@code peek} functions
*/
public ArgConsumer copy() {
return new ArgConsumer(manager, args, consumed);
}
private final class Context implements IDatatypeContext {
@Override
public final IBaritone getBaritone() {
return ArgConsumer.this.manager.getBaritone();
}
@Override
public final ArgConsumer getConsumer() {
return ArgConsumer.this;
}
}
IArgConsumer copy();
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.helpers.pagination;
package baritone.api.command.helpers.pagination;
import baritone.api.utils.Helper;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
@@ -115,7 +115,7 @@ public class Paginator<E> implements Helper {
display(transform, null);
}
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
int page = 1;
consumer.requireMax(1);
if (consumer.hasAny()) {
@@ -127,7 +127,7 @@ public class Paginator<E> implements Helper {
"a valid page (1-%d)",
pagi.getMaxPage()
),
consumer.consumed().value
consumer.consumed().getValue()
);
}
}
@@ -138,47 +138,47 @@ public class Paginator<E> implements Helper {
pagi.display(transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, new Paginator<>(elems), pre, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, Arrays.asList(elems), pre, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, pagi, null, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, new Paginator<>(elems), null, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform, String commandPrefix) throws CommandException {
paginate(consumer, Arrays.asList(elems), null, transform, commandPrefix);
}
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, Paginator<T> pagi, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, pagi, pre, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, List<T> elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, new Paginator<>(elems), pre, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, T[] elems, Runnable pre, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, Arrays.asList(elems), pre, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, Paginator<T> pagi, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, pagi, null, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, List<T> elems, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, new Paginator<>(elems), null, transform, null);
}
public static <T> void paginate(ArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform) throws CommandException {
public static <T> void paginate(IArgConsumer consumer, T[] elems, Function<T, ITextComponent> transform) throws CommandException {
paginate(consumer, Arrays.asList(elems), null, transform, null);
}
}
@@ -15,15 +15,14 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.helpers.tabcomplete;
package baritone.api.command.helpers.tabcomplete;
import baritone.api.BaritoneAPI;
import baritone.api.Settings;
import baritone.api.event.events.TabCompleteEvent;
import baritone.api.utils.SettingsUtil;
import baritone.api.utils.command.datatypes.IDatatype;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.manager.ICommandManager;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.manager.ICommandManager;
import net.minecraft.util.ResourceLocation;
import java.util.Comparator;
@@ -45,7 +44,7 @@ import java.util.stream.Stream;
* {@link #filterPrefix(String)}</li>
* <li>Get the stream using {@link #stream()}</li>
* <li>Pass it up to whatever's calling your tab complete function (i.e.
* {@link ICommandManager#tabComplete(String)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)})</li>
* {@link ICommandManager#tabComplete(String)} or {@link IArgConsumer}#tabCompleteDatatype(IDatatype)})</li>
* </ul>
* <p>
* For advanced users: if you're intercepting {@link TabCompleteEvent}s directly, use {@link #build()} instead for an
@@ -242,7 +241,7 @@ public class TabCompleteHelper {
*/
public TabCompleteHelper addCommands(ICommandManager manager) {
return append(manager.getRegistry().descendingStream()
.flatMap(command -> command.names.stream())
.flatMap(command -> command.getNames().stream())
.distinct()
);
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.manager;
package baritone.api.command.manager;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.registry.Registry;
import baritone.api.command.Command;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.registry.Registry;
import net.minecraft.util.Tuple;
import java.util.List;
@@ -44,9 +44,9 @@ public interface ICommandManager {
boolean execute(String string);
boolean execute(Tuple<String, List<CommandArgument>> expanded);
boolean execute(Tuple<String, List<ICommandArgument>> expanded);
Stream<String> tabComplete(Tuple<String, List<CommandArgument>> expanded);
Stream<String> tabComplete(Tuple<String, List<ICommandArgument>> expanded);
Stream<String> tabComplete(String prefix);
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.registry;
package baritone.api.command.registry;
import java.util.*;
import java.util.function.Consumer;
@@ -17,6 +17,7 @@
package baritone.api.process;
import baritone.api.utils.BlockOptionalMeta;
import net.minecraft.block.Block;
/**
@@ -24,7 +25,11 @@ import net.minecraft.block.Block;
*/
public interface IGetToBlockProcess extends IBaritoneProcess {
void getToBlock(Block block);
void getToBlock(BlockOptionalMeta block);
default void getToBlock(Block block) {
getToBlock(new BlockOptionalMeta(block));
}
boolean blacklistClosest();
}
@@ -1,105 +0,0 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.argparser;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.exception.CommandNoParserForTypeException;
import baritone.api.utils.command.exception.CommandUnhandledException;
import baritone.api.utils.command.registry.Registry;
public class ArgParserManager {
public static final Registry<IArgParser> REGISTRY = new Registry<>();
static {
DefaultArgParsers.ALL.forEach(REGISTRY::register);
}
/**
* @param type The type trying to be parsed
* @return A parser that can parse arguments into this class, if found.
*/
public static <T> IArgParser.Stateless<T> getParserStateless(Class<T> type) {
//noinspection unchecked
return REGISTRY.descendingStream()
.filter(IArgParser.Stateless.class::isInstance)
.map(IArgParser.Stateless.class::cast)
.filter(parser -> parser.getTarget().isAssignableFrom(type))
.findFirst()
.orElse(null);
}
/**
* @param type The type trying to be parsed
* @return A parser that can parse arguments into this class, if found.
*/
public static <T, S> IArgParser.Stated<T, S> getParserStated(Class<T> type, Class<S> stateKlass) {
//noinspection unchecked
return REGISTRY.descendingStream()
.filter(IArgParser.Stated.class::isInstance)
.map(IArgParser.Stated.class::cast)
.filter(parser -> parser.getTarget().isAssignableFrom(type))
.filter(parser -> parser.getStateType().isAssignableFrom(stateKlass))
.map(IArgParser.Stated.class::cast)
.findFirst()
.orElse(null);
}
/**
* Attempt to parse the specified argument with a stateless {@link IArgParser} that outputs the specified class.
*
* @param type The type to try and parse the argument into.
* @param arg The argument to parse.
* @return An instance of the specified class.
* @throws CommandInvalidTypeException If the parsing failed
*/
public static <T> T parseStateless(Class<T> type, CommandArgument arg) throws CommandInvalidTypeException {
IArgParser.Stateless<T> parser = getParserStateless(type);
if (parser == null) {
throw new CommandNoParserForTypeException(type);
}
try {
return parser.parseArg(arg);
} catch (Exception exc) {
throw new CommandInvalidTypeException(arg, type.getSimpleName());
}
}
/**
* Attempt to parse the specified argument with a stated {@link IArgParser} that outputs the specified class.
*
* @param type The type to try and parse the argument into.
* @param arg The argument to parse.
* @param state The state to pass to the {@link IArgParser.Stated}.
* @return An instance of the specified class.
* @throws CommandInvalidTypeException If the parsing failed
* @see IArgParser.Stated
*/
public static <T, S> T parseStated(Class<T> type, Class<S> stateKlass, CommandArgument arg, S state) throws CommandInvalidTypeException {
IArgParser.Stated<T, S> parser = getParserStated(type, stateKlass);
if (parser == null) {
throw new CommandNoParserForTypeException(type);
}
try {
return parser.parseArg(arg, state);
} catch (Exception exc) {
throw new CommandInvalidTypeException(arg, type.getSimpleName());
}
}
}
@@ -1,170 +0,0 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.argument;
import baritone.api.utils.command.argparser.ArgParserManager;
import baritone.api.utils.command.argparser.IArgParser;
import baritone.api.utils.command.exception.CommandInvalidArgumentException;
import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import net.minecraft.util.EnumFacing;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
/**
* A {@link CommandArgument} is an immutable object representing one command argument. It contains data on the index of
* that argument, its value, and the rest of the string that argument was found in
* <p>
* You're recommended to use {@link ArgConsumer}s to handle these.
*/
public class CommandArgument {
public final int index;
public final String value;
public final String rawRest;
public final static Pattern argPattern = Pattern.compile("\\S+");
private CommandArgument(int index, String value, String rawRest) {
this.index = index;
this.value = value;
this.rawRest = rawRest;
}
/**
* Gets an enum value from the enum class with the same name as this argument's value
* <p>
* For example if you getEnum as an {@link EnumFacing}, and this argument's value is "up", it will return {@link
* EnumFacing#UP}
*
* @param enumClass The enum class to search
* @return An enum constant of that class with the same name as this argument's value
* @throws CommandInvalidTypeException If the constant couldn't be found
* @see ArgConsumer#peekEnum(Class)
* @see ArgConsumer#peekEnum(Class, int)
* @see ArgConsumer#peekEnumOrNull(Class)
* @see ArgConsumer#peekEnumOrNull(Class, int)
* @see ArgConsumer#getEnum(Class)
* @see ArgConsumer#getEnumOrNull(Class)
*/
public <E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException {
return Stream.of(enumClass.getEnumConstants())
.filter(e -> e.name().equalsIgnoreCase(value))
.findFirst()
.orElseThrow(() -> new CommandInvalidTypeException(this, enumClass.getSimpleName()));
}
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return An instance of the specified type
* @throws CommandInvalidTypeException If the parsing failed
*/
public <T> T getAs(Class<T> type) throws CommandInvalidTypeException {
return ArgParserManager.parseStateless(type, this);
}
/**
* Tries to use a <b>stateless</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return If the parser succeeded
*/
public <T> boolean is(Class<T> type) {
try {
getAs(type);
return true;
} catch (Throwable t) {
return false;
}
}
/**
* Tries to use a <b>stated</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return An instance of the specified type
* @throws CommandInvalidTypeException If the parsing failed
*/
@SuppressWarnings("UnusedReturnValue")
public <T, S> T getAs(Class<T> type, Class<S> stateType, S state) throws CommandInvalidTypeException {
return ArgParserManager.parseStated(type, stateType, this, state);
}
/**
* Tries to use a <b>stated</b> {@link IArgParser} to parse this argument into the specified class
*
* @param type The class to parse this argument into
* @return If the parser succeeded
*/
public <T, S> boolean is(Class<T> type, Class<S> stateType, S state) {
try {
getAs(type, stateType, state);
return true;
} catch (Throwable t) {
return false;
}
}
/**
* Turn a string into a list of {@link CommandArgument}s. This is needed because of {@link CommandArgument#rawRest}
*
* @param string The string to convert
* @param preserveEmptyLast If the string ends with whitespace, add an empty {@link CommandArgument} to the end This
* is useful for tab completion
* @return A list of {@link CommandArgument}s
*/
public static List<CommandArgument> from(String string, boolean preserveEmptyLast) {
List<CommandArgument> args = new ArrayList<>();
Matcher argMatcher = argPattern.matcher(string);
int lastEnd = -1;
while (argMatcher.find()) {
args.add(new CommandArgument(
args.size(),
argMatcher.group(),
string.substring(argMatcher.start())
));
lastEnd = argMatcher.end();
}
if (preserveEmptyLast && lastEnd < string.length()) {
args.add(new CommandArgument(args.size(), "", ""));
}
return args;
}
/**
* @see #from(String, boolean)
*/
public static List<CommandArgument> from(String string) {
return from(string, false);
}
/**
* Returns an "unknown" {@link CommandArgument}. This shouldn't be used unless you absolutely have no information -
* ESPECIALLY not with {@link CommandInvalidArgumentException}s
*
* @return The unknown {@link CommandArgument}
*/
public static CommandArgument unknown() {
return new CommandArgument(-1, "<unknown>", "");
}
}
@@ -1,68 +0,0 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.execution;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import net.minecraft.util.Tuple;
import java.util.List;
import java.util.stream.Stream;
/**
* @author Brady
* @since 9/28/2019
*/
public interface ICommandExecution {
/**
* @return The label that was used to target the {@link Command}
*/
String getLabel();
/**
* @return The arguments to be passed to the {@link Command}
*/
ArgConsumer getArguments();
/**
* Executes the target command for this {@link ICommandExecution}. This method should never
* {@code throw} any exception, anything that is thrown during the target command execution
* should be safely handled.
*/
void execute();
/**
* Forwards this {@link ICommandExecution} to the target {@link Command} to perform a tab-completion.
* If the tab-completion operation is a failure, then {@link Stream#empty()} will be returned.
*
* @return The tab-completed arguments, if possible.
*/
Stream<String> tabComplete();
static Tuple<String, List<CommandArgument>> expand(String string, boolean preserveEmptyLast) {
String label = string.split("\\s", 2)[0];
List<CommandArgument> args = CommandArgument.from(string.substring(label.length()), preserveEmptyLast);
return new Tuple<>(label, args);
}
static Tuple<String, List<CommandArgument>> expand(String string) {
return expand(string, false);
}
}
+1 -1
View File
@@ -29,7 +29,7 @@ import baritone.event.GameEventHandler;
import baritone.process.*;
import baritone.selection.SelectionManager;
import baritone.utils.*;
import baritone.utils.command.manager.CommandManager;
import baritone.command.manager.CommandManager;
import baritone.utils.player.PrimaryPlayerContext;
import net.minecraft.client.Minecraft;
+8 -1
View File
@@ -20,8 +20,10 @@ package baritone;
import baritone.api.IBaritone;
import baritone.api.IBaritoneProvider;
import baritone.api.cache.IWorldScanner;
import baritone.utils.command.BaritoneChatControl;
import baritone.api.command.ICommandSystem;
import baritone.command.BaritoneChatControl;
import baritone.cache.WorldScanner;
import baritone.command.CommandSystem;
import java.util.Collections;
import java.util.List;
@@ -57,4 +59,9 @@ public final class BaritoneProvider implements IBaritoneProvider {
public IWorldScanner getWorldScanner() {
return WorldScanner.INSTANCE;
}
@Override
public ICommandSystem getCommandSystem() {
return CommandSystem.INSTANCE;
}
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command;
package baritone.command;
import baritone.api.BaritoneAPI;
import baritone.api.IBaritone;
@@ -26,13 +26,14 @@ import baritone.api.event.events.TabCompleteEvent;
import baritone.api.event.listener.AbstractGameEventListener;
import baritone.api.utils.Helper;
import baritone.api.utils.SettingsUtil;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.exception.CommandNotFoundException;
import baritone.api.utils.command.execution.ICommandExecution;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.utils.command.manager.ICommandManager;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.command.exception.CommandNotFoundException;
import baritone.command.helpers.arguments.ArgConsumer;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.manager.ICommandManager;
import baritone.command.argument.CommandArguments;
import baritone.command.manager.CommandManager;
import net.minecraft.util.Tuple;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
@@ -46,7 +47,7 @@ import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;
import static baritone.api.utils.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
public class BaritoneChatControl implements Helper, AbstractGameEventListener {
@@ -67,7 +68,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
event.cancel();
String commandStr = msg.substring(forceRun ? FORCE_COMMAND_PREFIX.length() : prefix.length());
if (!runCommand(commandStr) && !commandStr.trim().isEmpty()) {
new CommandNotFoundException(ICommandExecution.expand(commandStr).getA()).handle(null, null);
new CommandNotFoundException(CommandManager.expand(commandStr).getA()).handle(null, null);
}
} else if ((settings.chatControl.value || settings.chatControlAnyway.value) && runCommand(msg)) {
event.cancel();
@@ -106,7 +107,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
if (msg.isEmpty()) {
return this.runCommand("help");
}
Tuple<String, List<CommandArgument>> pair = ICommandExecution.expand(msg);
Tuple<String, List<ICommandArgument>> pair = CommandManager.expand(msg);
String command = pair.getA();
String rest = msg.substring(pair.getA().length());
ArgConsumer argc = new ArgConsumer(this.manager, pair.getB());
@@ -155,7 +156,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
return;
}
String msg = prefix.substring(commandPrefix.length());
List<CommandArgument> args = CommandArgument.from(msg, true);
List<ICommandArgument> args = CommandArguments.from(msg, true);
Stream<String> stream = tabComplete(msg);
if (args.size() == 1) {
stream = stream.map(x -> commandPrefix + x);
@@ -165,7 +166,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
public Stream<String> tabComplete(String msg) {
try {
List<CommandArgument> args = CommandArgument.from(msg, true);
List<ICommandArgument> args = CommandArguments.from(msg, true);
ArgConsumer argc = new ArgConsumer(this.manager, args);
if (argc.hasAtMost(2)) {
if (argc.hasExactly(1)) {
@@ -0,0 +1,35 @@
/*
* 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.command;
import baritone.api.command.ICommandSystem;
import baritone.command.argparser.ArgParserManager;
import baritone.api.command.argparser.IArgParserManager;
/**
* @author Brady
* @since 10/4/2019
*/
public enum CommandSystem implements ICommandSystem {
INSTANCE;
@Override
public IArgParserManager getParserManager() {
return ArgParserManager.INSTANCE;
}
}
@@ -0,0 +1,90 @@
/*
* 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.command.argparser;
import baritone.api.command.argparser.IArgParser;
import baritone.api.command.argparser.IArgParserManager;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.exception.CommandNoParserForTypeException;
import baritone.api.command.registry.Registry;
public enum ArgParserManager implements IArgParserManager {
INSTANCE;
public final Registry<IArgParser> registry = new Registry<>();
ArgParserManager() {
DefaultArgParsers.ALL.forEach(this.registry::register);
}
@Override
public <T> IArgParser.Stateless<T> getParserStateless(Class<T> type) {
//noinspection unchecked
return this.registry.descendingStream()
.filter(IArgParser.Stateless.class::isInstance)
.map(IArgParser.Stateless.class::cast)
.filter(parser -> parser.getTarget().isAssignableFrom(type))
.findFirst()
.orElse(null);
}
@Override
public <T, S> IArgParser.Stated<T, S> getParserStated(Class<T> type, Class<S> stateKlass) {
//noinspection unchecked
return this.registry.descendingStream()
.filter(IArgParser.Stated.class::isInstance)
.map(IArgParser.Stated.class::cast)
.filter(parser -> parser.getTarget().isAssignableFrom(type))
.filter(parser -> parser.getStateType().isAssignableFrom(stateKlass))
.map(IArgParser.Stated.class::cast)
.findFirst()
.orElse(null);
}
@Override
public <T> T parseStateless(Class<T> type, ICommandArgument arg) throws CommandInvalidTypeException {
IArgParser.Stateless<T> parser = this.getParserStateless(type);
if (parser == null) {
throw new CommandNoParserForTypeException(type);
}
try {
return parser.parseArg(arg);
} catch (Exception exc) {
throw new CommandInvalidTypeException(arg, type.getSimpleName());
}
}
@Override
public <T, S> T parseStated(Class<T> type, Class<S> stateKlass, ICommandArgument arg, S state) throws CommandInvalidTypeException {
IArgParser.Stated<T, S> parser = this.getParserStated(type, stateKlass);
if (parser == null) {
throw new CommandNoParserForTypeException(type);
}
try {
return parser.parseArg(arg, state);
} catch (Exception exc) {
throw new CommandInvalidTypeException(arg, type.getSimpleName());
}
}
@Override
public Registry<IArgParser> getRegistry() {
return this.registry;
}
}
@@ -15,9 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.api.utils.command.argparser;
package baritone.command.argparser;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.command.argparser.IArgParser;
import baritone.api.command.argument.ICommandArgument;
import java.util.Arrays;
import java.util.List;
@@ -34,8 +35,8 @@ public class DefaultArgParsers {
}
@Override
public Integer parseArg(CommandArgument arg) throws RuntimeException {
return Integer.parseInt(arg.value);
public Integer parseArg(ICommandArgument arg) throws RuntimeException {
return Integer.parseInt(arg.getValue());
}
}
@@ -48,8 +49,8 @@ public class DefaultArgParsers {
}
@Override
public Long parseArg(CommandArgument arg) throws RuntimeException {
return Long.parseLong(arg.value);
public Long parseArg(ICommandArgument arg) throws RuntimeException {
return Long.parseLong(arg.getValue());
}
}
@@ -62,8 +63,8 @@ public class DefaultArgParsers {
}
@Override
public Float parseArg(CommandArgument arg) throws RuntimeException {
String value = arg.value;
public Float parseArg(ICommandArgument arg) throws RuntimeException {
String value = arg.getValue();
if (!value.matches("^([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)|)$")) {
throw new IllegalArgumentException("failed float format check");
}
@@ -80,8 +81,8 @@ public class DefaultArgParsers {
}
@Override
public Double parseArg(CommandArgument arg) throws RuntimeException {
String value = arg.value;
public Double parseArg(ICommandArgument arg) throws RuntimeException {
String value = arg.getValue();
if (!value.matches("^([+-]?(?:\\d+(?:\\.\\d*)?|\\.\\d+)|)$")) {
throw new IllegalArgumentException("failed double format check");
}
@@ -101,8 +102,8 @@ public class DefaultArgParsers {
}
@Override
public Boolean parseArg(CommandArgument arg) throws RuntimeException {
String value = arg.value;
public Boolean parseArg(ICommandArgument arg) throws RuntimeException {
String value = arg.getValue();
if (TRUTHY_VALUES.contains(value.toLowerCase(Locale.US))) {
return true;
} else if (FALSY_VALUES.contains(value.toLowerCase(Locale.US))) {
@@ -0,0 +1,96 @@
/*
* 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.command.argument;
import baritone.command.argparser.ArgParserManager;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.exception.CommandInvalidTypeException;
import java.util.stream.Stream;
/**
* The default implementation of {@link ICommandArgument}
*
* @author LoganDark
*/
class CommandArgument implements ICommandArgument {
private final int index;
private final String value;
private final String rawRest;
CommandArgument(int index, String value, String rawRest) {
this.index = index;
this.value = value;
this.rawRest = rawRest;
}
@Override
public int getIndex() {
return this.index;
}
@Override
public String getValue() {
return this.value;
}
@Override
public String getRawRest() {
return this.rawRest;
}
@Override
public <E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException {
return Stream.of(enumClass.getEnumConstants())
.filter(e -> e.name().equalsIgnoreCase(value))
.findFirst()
.orElseThrow(() -> new CommandInvalidTypeException(this, enumClass.getSimpleName()));
}
@Override
public <T> T getAs(Class<T> type) throws CommandInvalidTypeException {
return ArgParserManager.INSTANCE.parseStateless(type, this);
}
@Override
public <T> boolean is(Class<T> type) {
try {
getAs(type);
return true;
} catch (Throwable t) {
return false;
}
}
@SuppressWarnings("UnusedReturnValue")
@Override
public <T, S> T getAs(Class<T> type, Class<S> stateType, S state) throws CommandInvalidTypeException {
return ArgParserManager.INSTANCE.parseStated(type, stateType, this, state);
}
@Override
public <T, S> boolean is(Class<T> type, Class<S> stateType, S state) {
try {
getAs(type, stateType, state);
return true;
} catch (Throwable t) {
return false;
}
}
}
@@ -0,0 +1,79 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.command.argument;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.exception.CommandInvalidArgumentException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author LoganDark
*/
public final class CommandArguments {
private CommandArguments() {}
private static final Pattern ARG_PATTERN = Pattern.compile("\\S+");
/**
* Turn a string into a list of {@link ICommandArgument}s. This is needed because of {@link ICommandArgument#getRawRest()}
*
* @param string The string to convert
* @param preserveEmptyLast If the string ends with whitespace, add an empty {@link ICommandArgument} to the end This
* is useful for tab completion
* @return A list of {@link ICommandArgument}s
*/
public static List<ICommandArgument> from(String string, boolean preserveEmptyLast) {
List<ICommandArgument> args = new ArrayList<>();
Matcher argMatcher = ARG_PATTERN.matcher(string);
int lastEnd = -1;
while (argMatcher.find()) {
args.add(new CommandArgument(
args.size(),
argMatcher.group(),
string.substring(argMatcher.start())
));
lastEnd = argMatcher.end();
}
if (preserveEmptyLast && lastEnd < string.length()) {
args.add(new CommandArgument(args.size(), "", ""));
}
return args;
}
/**
* @see #from(String, boolean)
*/
public static List<ICommandArgument> from(String string) {
return from(string, false);
}
/**
* Returns an "unknown" {@link CommandArgument}. This shouldn't be used unless you absolutely have no information -
* ESPECIALLY not with {@link CommandInvalidArgumentException}s
*
* @return The unknown {@link CommandArgument}
*/
public static CommandArgument unknown() {
return new CommandArgument(-1, "<unknown>", "");
}
}
@@ -15,14 +15,14 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalAxis;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -31,11 +31,11 @@ import java.util.stream.Stream;
public class AxisCommand extends Command {
public AxisCommand(IBaritone baritone) {
super(baritone, Arrays.asList("axis", "highway"));
super(baritone, "axis", "highway");
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
Goal goal = new GoalAxis();
baritone.getCustomGoalProcess().setGoal(goal);
@@ -43,7 +43,7 @@ public class AxisCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,14 +15,14 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.process.IGetToBlockProcess;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -35,7 +35,7 @@ public class BlacklistCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
IGetToBlockProcess proc = baritone.getGetToBlockProcess();
if (!proc.isActive()) {
@@ -49,7 +49,7 @@ public class BlacklistCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,16 +15,16 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeBlockPos;
import baritone.api.utils.command.datatypes.RelativeFile;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.datatypes.RelativeBlockPos;
import baritone.api.command.datatypes.RelativeFile;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import net.minecraft.client.Minecraft;
import java.io.File;
@@ -42,7 +42,7 @@ public class BuildCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
File file = args.getDatatypePost(RelativeFile.INSTANCE, schematicsDir).getAbsoluteFile();
if (!file.getName().toLowerCase(Locale.US).endsWith(".schematic")) {
file = new File(file.getAbsolutePath() + ".schematic");
@@ -64,7 +64,7 @@ public class BuildCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return RelativeFile.tabComplete(args, schematicsDir);
} else if (args.has(2)) {
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -29,18 +29,18 @@ import java.util.stream.Stream;
public class CancelCommand extends Command {
public CancelCommand(IBaritone baritone) {
super(baritone, Arrays.asList("cancel", "stop"));
super(baritone, "cancel", "stop");
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
baritone.getPathingBehavior().cancelEverything();
logDirect("ok canceled");
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,15 +15,15 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.cache.IRememberedInventory;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
@@ -41,7 +41,7 @@ public class ChestsCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
Set<Map.Entry<BlockPos, IRememberedInventory>> entries =
ctx.worldData().getContainerMemory().getRememberedInventories().entrySet();
@@ -62,7 +62,7 @@ public class ChestsCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -33,14 +33,14 @@ public class ClickCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
baritone.openClick();
logDirect("aight dude");
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,14 +15,14 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.pathing.goals.GoalBlock;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import net.minecraft.entity.Entity;
import net.minecraft.util.math.BlockPos;
@@ -37,7 +37,7 @@ public class ComeCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
Entity entity = mc.getRenderViewEntity();
if (entity == null) {
@@ -48,7 +48,7 @@ public class ComeCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,11 +15,11 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Collections;
import java.util.List;
@@ -31,7 +31,7 @@ public class CommandAlias extends Command {
public final String target;
public CommandAlias(IBaritone baritone, List<String> names, String shortDesc, String target) {
super(baritone, names);
super(baritone, names.toArray(new String[0]));
this.shortDesc = shortDesc;
this.target = target;
}
@@ -43,12 +43,12 @@ public class CommandAlias extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) {
public void execute(String label, IArgConsumer args) {
this.baritone.getCommandManager().execute(String.format("%s %s", target, args.rawRest()));
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return this.baritone.getCommandManager().tabComplete(String.format("%s %s", target, args.rawRest()));
}
@@ -15,10 +15,10 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.command.Command;
import java.util.*;
@@ -34,6 +34,7 @@ public final class DefaultCommands {
new CommandAlias(baritone, Arrays.asList("modified", "mod", "baritone", "modifiedsettings"), "List modified settings", "set modified"),
new CommandAlias(baritone, "reset", "Reset all settings or just one", "set reset"),
new GoalCommand(baritone),
new GotoCommand(baritone),
new PathCommand(baritone),
new ProcCommand(baritone),
new VersionCommand(baritone),
@@ -15,14 +15,14 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.pathing.goals.GoalXZ;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeGoalXZ;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.datatypes.RelativeGoalXZ;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -35,7 +35,7 @@ public class ExploreCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
if (args.hasAny()) {
args.requireExactly(2);
} else {
@@ -49,7 +49,7 @@ public class ExploreCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
if (args.hasAtMost(2)) {
return args.tabCompleteDatatype(RelativeGoalXZ.INSTANCE);
}
@@ -15,15 +15,15 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeFile;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.datatypes.RelativeFile;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import com.google.gson.JsonSyntaxException;
import java.io.File;
@@ -39,7 +39,7 @@ public class ExploreFilterCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(2);
File file = args.getDatatypePost(RelativeFile.INSTANCE, mc.gameDir.getAbsoluteFile().getParentFile());
boolean invert = false;
@@ -63,7 +63,7 @@ public class ExploreFilterCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return RelativeFile.tabComplete(args, RelativeFile.gameDir());
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -33,14 +33,14 @@ public class FarmCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
baritone.getFarmProcess().farm();
logDirect("Farming");
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,14 +15,14 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.BlockById;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.datatypes.BlockById;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import net.minecraft.block.Block;
import net.minecraft.util.registry.IRegistry;
@@ -38,7 +38,7 @@ public class FindCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
List<Block> toFind = new ArrayList<>();
while (args.hasAny()) {
toFind.add(args.getDatatypeFor(BlockById.INSTANCE));
@@ -60,7 +60,7 @@ public class FindCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return args.tabCompleteDatatype(BlockById.INSTANCE);
}
@@ -15,16 +15,16 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.EntityClassById;
import baritone.api.utils.command.datatypes.IDatatypeFor;
import baritone.api.utils.command.datatypes.NearbyPlayer;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.Command;
import baritone.api.command.datatypes.EntityClassById;
import baritone.api.command.datatypes.IDatatypeFor;
import baritone.api.command.datatypes.NearbyPlayer;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityType;
@@ -43,7 +43,7 @@ public class FollowCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMin(1);
FollowGroup group;
FollowList list;
@@ -89,7 +89,7 @@ public class FollowCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.append(FollowGroup.class)
@@ -15,13 +15,13 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.behavior.IPathingBehavior;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -34,7 +34,7 @@ public class ForceCancelCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
IPathingBehavior pathingBehavior = baritone.getPathingBehavior();
pathingBehavior.cancelEverything();
@@ -43,7 +43,7 @@ public class ForceCancelCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -33,14 +33,14 @@ public class GcCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
System.gc();
logDirect("ok called System.gc()");
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,18 +15,18 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.pathing.goals.Goal;
import baritone.api.process.ICustomGoalProcess;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeCoordinate;
import baritone.api.utils.command.datatypes.RelativeGoal;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.Command;
import baritone.api.command.datatypes.RelativeCoordinate;
import baritone.api.command.datatypes.RelativeGoal;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import java.util.Arrays;
import java.util.List;
@@ -39,7 +39,7 @@ public class GoalCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
ICustomGoalProcess goalProcess = baritone.getCustomGoalProcess();
if (args.hasAny() && Arrays.asList("reset", "clear", "none").contains(args.peekString())) {
args.requireMax(1);
@@ -59,7 +59,7 @@ public class GoalCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
TabCompleteHelper helper = new TabCompleteHelper();
if (args.hasExactlyOne()) {
helper.append("reset", "clear", "none", "~");
@@ -0,0 +1,82 @@
/*
* 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.command.defaults;
import baritone.api.IBaritone;
import baritone.api.command.Command;
import baritone.api.command.datatypes.BlockById;
import baritone.api.command.datatypes.ForBlockOptionalMeta;
import baritone.api.command.datatypes.RelativeCoordinate;
import baritone.api.command.datatypes.RelativeGoal;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.pathing.goals.Goal;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.BlockOptionalMeta;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class GotoCommand extends Command {
protected GotoCommand(IBaritone baritone) {
super(baritone, "goto");
}
@Override
public void execute(String label, IArgConsumer args) throws CommandException {
if (args.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) != null) { // if we have a numeric first argument...
BetterBlockPos origin = baritone.getPlayerContext().playerFeet();
Goal goal = args.getDatatypePostOrNull(RelativeGoal.INSTANCE, origin);
logDirect(String.format("Going to: %s", goal.toString()));
baritone.getCustomGoalProcess().setGoalAndPath(goal);
return;
}
args.requireMax(1);
BlockOptionalMeta destination = args.getDatatypeFor(ForBlockOptionalMeta.INSTANCE);
baritone.getGetToBlockProcess().getToBlock(destination);
}
@Override
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
// since it's either a goal or a block, I don't think we can tab complete properly?
// so just tab complete for the block variant
return args.tabCompleteDatatype(BlockById.INSTANCE);
}
@Override
public String getShortDesc() {
return "Go to a coordinate or block";
}
@Override
public List<String> getLongDesc() {
return Arrays.asList(
"The got command tells Baritone to head towards a given goal or block.",
"",
"Wherever a coordinate is expected, you can use ~ just like in regular Minecraft commands. Or, you can just use regular numbers.",
"",
"Usage:",
"> goto <block> - Go to a block, wherever it is in the world",
"> goto <y> - Go to a Y level",
"> goto <x> <z> - Go to an X,Z position",
"> goto <x> <y> <z> - Go to an X,Y,Z position"
);
}
}
@@ -15,15 +15,15 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandNotFoundException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.pagination.Paginator;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandNotFoundException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.helpers.pagination.Paginator;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
@@ -35,16 +35,16 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static baritone.api.utils.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
public class HelpCommand extends Command {
public HelpCommand(IBaritone baritone) {
super(baritone, Arrays.asList("help", "?"));
super(baritone, "help", "?");
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(1);
if (!args.hasAny() || args.is(Integer.class)) {
Paginator.paginate(
@@ -55,8 +55,8 @@ public class HelpCommand extends Command {
),
() -> logDirect("All Baritone commands (clickable):"),
command -> {
String names = String.join("/", command.names);
String name = command.names.get(0);
String names = String.join("/", command.getNames());
String name = command.getNames().get(0);
ITextComponent shortDescComponent = new TextComponentString(" - " + command.getShortDesc());
shortDescComponent.getStyle().setColor(TextFormatting.DARK_GRAY);
ITextComponent namesComponent = new TextComponentString(names);
@@ -66,7 +66,7 @@ public class HelpCommand extends Command {
hoverComponent.appendSibling(namesComponent);
hoverComponent.appendText("\n" + command.getShortDesc());
hoverComponent.appendText("\n\nClick to view full help");
String clickCommand = FORCE_COMMAND_PREFIX + String.format("%s %s", label, command.names.get(0));
String clickCommand = FORCE_COMMAND_PREFIX + String.format("%s %s", label, command.getNames().get(0));
ITextComponent component = new TextComponentString(name);
component.getStyle().setColor(TextFormatting.GRAY);
component.appendSibling(shortDescComponent);
@@ -83,7 +83,7 @@ public class HelpCommand extends Command {
if (command == null) {
throw new CommandNotFoundException(commandName);
}
logDirect(String.format("%s - %s", String.join(" / ", command.names), command.getShortDesc()));
logDirect(String.format("%s - %s", String.join(" / ", command.getNames()), command.getShortDesc()));
logDirect("");
command.getLongDesc().forEach(this::logDirect);
logDirect("");
@@ -97,7 +97,7 @@ public class HelpCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.addCommands(this.baritone.getCommandManager())
@@ -15,16 +15,16 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalInverted;
import baritone.api.process.ICustomGoalProcess;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -37,7 +37,7 @@ public class InvertCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess();
Goal goal;
@@ -54,7 +54,7 @@ public class InvertCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,15 +15,15 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.BlockById;
import baritone.api.utils.command.datatypes.ForBlockOptionalMeta;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.datatypes.BlockById;
import baritone.api.command.datatypes.ForBlockOptionalMeta;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.cache.WorldScanner;
import java.util.ArrayList;
@@ -38,7 +38,7 @@ public class MineCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
int quantity = args.getAsOrDefault(Integer.class, 0);
args.requireMin(1);
List<BlockOptionalMeta> boms = new ArrayList<>();
@@ -51,7 +51,7 @@ public class MineCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return args.tabCompleteDatatype(BlockById.INSTANCE);
}
@@ -0,0 +1,65 @@
/*
* 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.command.defaults;
import baritone.api.IBaritone;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.process.ICustomGoalProcess;
import baritone.cache.WorldScanner;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class PathCommand extends Command {
public PathCommand(IBaritone baritone) {
super(baritone, "path");
}
@Override
public void execute(String label, IArgConsumer args) throws CommandException {
ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess();
args.requireMax(0);
WorldScanner.INSTANCE.repack(ctx);
customGoalProcess.path();
logDirect("Now pathing");
}
@Override
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
return Stream.empty();
}
@Override
public String getShortDesc() {
return "Start heading towards the goal";
}
@Override
public List<String> getLongDesc() {
return Arrays.asList(
"The path command tells Baritone to head towards the current goal.",
"",
"Usage:",
"> path - Start the pathing."
);
}
}
@@ -15,16 +15,16 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.process.IBaritoneProcess;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -79,7 +79,7 @@ public class PauseResumeCommands {
);
pauseCommand = new Command(baritone, "pause") {
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
if (paused[0]) {
throw new CommandInvalidStateException("Already paused");
@@ -89,7 +89,7 @@ public class PauseResumeCommands {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -112,8 +112,9 @@ public class PauseResumeCommands {
};
resumeCommand = new Command(baritone, "resume") {
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
baritone.getBuilderProcess().resume();
if (!paused[0]) {
throw new CommandInvalidStateException("Not paused");
}
@@ -122,7 +123,7 @@ public class PauseResumeCommands {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -143,13 +144,13 @@ public class PauseResumeCommands {
};
pausedCommand = new Command(baritone, "paused") {
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
logDirect(String.format("Baritone is %spaused", paused[0] ? "" : "not "));
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,16 +15,16 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.pathing.calc.IPathingControlManager;
import baritone.api.process.IBaritoneProcess;
import baritone.api.process.PathingCommand;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -37,7 +37,7 @@ public class ProcCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
IPathingControlManager pathingControlManager = baritone.getPathingControlManager();
IBaritoneProcess process = pathingControlManager.mostRecentInControl().orElse(null);
@@ -62,7 +62,7 @@ public class ProcCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -33,14 +33,14 @@ public class ReloadAllCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
ctx.worldData().getCachedWorld().reloadAllFromDisk();
logDirect("Reloaded");
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,13 +15,13 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -34,7 +34,7 @@ public class RenderCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
BetterBlockPos origin = ctx.playerFeet();
int renderDistance = (mc.gameSettings.renderDistanceChunks + 1) * 16;
@@ -50,7 +50,7 @@ public class RenderCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.cache.WorldScanner;
import java.util.Arrays;
@@ -30,17 +30,17 @@ import java.util.stream.Stream;
public class RepackCommand extends Command {
public RepackCommand(IBaritone baritone) {
super(baritone, Arrays.asList("repack", "rescan"));
super(baritone, "repack", "rescan");
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
logDirect(String.format("Queued %d chunks for repacking", WorldScanner.INSTANCE.repack(ctx)));
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -33,14 +33,14 @@ public class SaveAllCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
ctx.worldData().getCachedWorld().save();
logDirect("Saved");
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,12 +15,12 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -33,13 +33,13 @@ public class SchematicaCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
baritone.getBuilderProcess().buildOpenSchematic();
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.Baritone;
import baritone.api.IBaritone;
@@ -28,15 +28,15 @@ import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.BlockOptionalMetaLookup;
import baritone.api.utils.ISchematic;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.ForBlockOptionalMeta;
import baritone.api.utils.command.datatypes.ForEnumFacing;
import baritone.api.utils.command.datatypes.RelativeBlockPos;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.Command;
import baritone.api.command.datatypes.ForBlockOptionalMeta;
import baritone.api.command.datatypes.ForEnumFacing;
import baritone.api.command.datatypes.RelativeBlockPos;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.utils.IRenderer;
import net.minecraft.init.Blocks;
import net.minecraft.util.EnumFacing;
@@ -56,7 +56,7 @@ public class SelCommand extends Command {
private BetterBlockPos pos1 = null;
public SelCommand(IBaritone baritone) {
super(baritone, Arrays.asList("sel", "selection", "s"));
super(baritone, "sel", "selection", "s");
baritone.getGameEventHandler().registerEventListener(new AbstractGameEventListener() {
@Override
public void onRenderPass(RenderEvent event) {
@@ -75,7 +75,7 @@ public class SelCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
Action action = Action.getByName(args.getString());
if (action == null) {
throw new CommandInvalidTypeException(args.consumed(), "an action");
@@ -186,7 +186,7 @@ public class SelCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.append(Action.getAllNames())
@@ -15,18 +15,18 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.Baritone;
import baritone.api.IBaritone;
import baritone.api.Settings;
import baritone.api.utils.SettingsUtil;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.pagination.Paginator;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.helpers.pagination.Paginator;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
@@ -41,16 +41,16 @@ import java.util.stream.Stream;
import static baritone.api.utils.SettingsUtil.settingTypeToString;
import static baritone.api.utils.SettingsUtil.settingValueToString;
import static baritone.api.utils.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
public class SetCommand extends Command {
public SetCommand(IBaritone baritone) {
super(baritone, Arrays.asList("set", "setting", "settings"));
super(baritone, "set", "setting", "settings");
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
String arg = args.hasAny() ? args.getString().toLowerCase(Locale.US) : "list";
if (Arrays.asList("s", "save").contains(arg)) {
SettingsUtil.save(Baritone.settings());
@@ -186,7 +186,7 @@ public class SetCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasAny()) {
String arg = args.getString();
if (args.hasExactlyOne() && !Arrays.asList("s", "save").contains(args.peekString().toLowerCase(Locale.US))) {
@@ -15,13 +15,13 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.pathing.goals.GoalXZ;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -30,11 +30,11 @@ import java.util.stream.Stream;
public class ThisWayCommand extends Command {
public ThisWayCommand(IBaritone baritone) {
super(baritone, Arrays.asList("thisway", "forward"));
super(baritone, "thisway", "forward");
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireExactly(1);
GoalXZ goal = GoalXZ.fromDirection(
ctx.playerFeetAsVec(),
@@ -46,7 +46,7 @@ public class ThisWayCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,14 +15,14 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalStrictDirection;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -35,7 +35,7 @@ public class TunnelCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
Goal goal = new GoalStrictDirection(
ctx.playerFeet(),
@@ -46,7 +46,7 @@ public class TunnelCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,13 +15,13 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.command.Command;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import java.util.Arrays;
import java.util.List;
@@ -34,7 +34,7 @@ public class VersionCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
args.requireMax(0);
String version = getClass().getPackage().getImplementationVersion();
if (version == null) {
@@ -45,7 +45,7 @@ public class VersionCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, IArgConsumer args) {
return Stream.empty();
}
@@ -15,7 +15,7 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.utils.command.defaults;
package baritone.command.defaults;
import baritone.api.IBaritone;
import baritone.api.cache.IWaypoint;
@@ -23,15 +23,15 @@ import baritone.api.cache.Waypoint;
import baritone.api.pathing.goals.Goal;
import baritone.api.pathing.goals.GoalBlock;
import baritone.api.utils.BetterBlockPos;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.ForWaypoints;
import baritone.api.utils.command.datatypes.RelativeBlockPos;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.exception.CommandInvalidTypeException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.pagination.Paginator;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.Command;
import baritone.api.command.datatypes.ForWaypoints;
import baritone.api.command.datatypes.RelativeBlockPos;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidStateException;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.helpers.pagination.Paginator;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
@@ -43,16 +43,16 @@ import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;
import static baritone.api.utils.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
public class WaypointsCommand extends Command {
public WaypointsCommand(IBaritone baritone) {
super(baritone, Arrays.asList("waypoints", "waypoint", "wp"));
super(baritone, "waypoints", "waypoint", "wp");
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, IArgConsumer args) throws CommandException {
Action action = args.hasAny() ? Action.getByName(args.getString()) : Action.LIST;
if (action == null) {
throw new CommandInvalidTypeException(args.consumed(), "an action");
@@ -241,7 +241,7 @@ public class WaypointsCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException {
if (args.hasAny()) {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
@@ -0,0 +1,444 @@
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Baritone is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
package baritone.command.helpers.arguments;
import baritone.api.IBaritone;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.datatypes.IDatatype;
import baritone.api.command.datatypes.IDatatypeContext;
import baritone.api.command.datatypes.IDatatypeFor;
import baritone.api.command.datatypes.IDatatypePost;
import baritone.api.command.exception.CommandException;
import baritone.api.command.exception.CommandInvalidTypeException;
import baritone.api.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.command.exception.CommandTooManyArgumentsException;
import baritone.api.command.helpers.arguments.IArgConsumer;
import baritone.api.command.manager.ICommandManager;
import baritone.command.argument.CommandArguments;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;
public class ArgConsumer implements IArgConsumer {
/**
* The parent {@link ICommandManager} for this {@link IArgConsumer}}. Used by {@link #context}.
*/
private final ICommandManager manager;
/**
* The {@link IDatatypeContext} instance for this {@link IArgConsumer}}, passed to
* datatypes when an operation is performed upon them.
*
* @see IDatatype
* @see IDatatypeContext
*/
private final IDatatypeContext context;
/**
* The list of arguments in this ArgConsumer
*/
private final LinkedList<ICommandArgument> args;
/**
* The list of consumed arguments for this ArgConsumer. The most recently consumed argument is the last one
*/
private final Deque<ICommandArgument> consumed;
private ArgConsumer(ICommandManager manager, Deque<ICommandArgument> args, Deque<ICommandArgument> consumed) {
this.manager = manager;
this.context = this.new Context();
this.args = new LinkedList<>(args);
this.consumed = new LinkedList<>(consumed);
}
public ArgConsumer(ICommandManager manager, List<ICommandArgument> args) {
this(manager, new LinkedList<>(args), new LinkedList<>());
}
@Override
public LinkedList<ICommandArgument> getArgs() {
return this.args;
}
@Override
public Deque<ICommandArgument> getConsumed() {
return this.consumed;
}
@Override
public boolean has(int num) {
return args.size() >= num;
}
@Override
public boolean hasAny() {
return has(1);
}
@Override
public boolean hasAtMost(int num) {
return args.size() <= num;
}
@Override
public boolean hasAtMostOne() {
return hasAtMost(1);
}
@Override
public boolean hasExactly(int num) {
return args.size() == num;
}
@Override
public boolean hasExactlyOne() {
return hasExactly(1);
}
@Override
public ICommandArgument peek(int index) throws CommandNotEnoughArgumentsException {
requireMin(index + 1);
return args.get(index);
}
@Override
public ICommandArgument peek() throws CommandNotEnoughArgumentsException {
return peek(0);
}
@Override
public boolean is(Class<?> type, int index) throws CommandNotEnoughArgumentsException {
return peek(index).is(type);
}
@Override
public boolean is(Class<?> type) throws CommandNotEnoughArgumentsException {
return is(type, 0);
}
@Override
public String peekString(int index) throws CommandNotEnoughArgumentsException {
return peek(index).getValue();
}
@Override
public String peekString() throws CommandNotEnoughArgumentsException {
return peekString(0);
}
@Override
public <E extends Enum<?>> E peekEnum(Class<E> enumClass, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getEnum(enumClass);
}
@Override
public <E extends Enum<?>> E peekEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekEnum(enumClass, 0);
}
@Override
public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass, int index) throws CommandNotEnoughArgumentsException {
try {
return peekEnum(enumClass, index);
} catch (CommandInvalidTypeException e) {
return null;
}
}
@Override
public <E extends Enum<?>> E peekEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return peekEnumOrNull(enumClass, 0);
}
@Override
public <T> T peekAs(Class<T> type, int index) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peek(index).getAs(type);
}
@Override
public <T> T peekAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return peekAs(type, 0);
}
@Override
public <T> T peekAsOrDefault(Class<T> type, T def, int index) throws CommandNotEnoughArgumentsException {
try {
return peekAs(type, index);
} catch (CommandInvalidTypeException e) {
return def;
}
}
@Override
public <T> T peekAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, def, 0);
}
@Override
public <T> T peekAsOrNull(Class<T> type, int index) throws CommandNotEnoughArgumentsException {
return peekAsOrDefault(type, null, index);
}
@Override
public <T> T peekAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return peekAsOrNull(type, 0);
}
@Override
public <T> T peekDatatype(IDatatypeFor<T> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypeFor(datatype);
}
@Override
public <T, O> T peekDatatype(IDatatypePost<T, O> datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return this.peekDatatype(datatype, null);
}
@Override
public <T, O> T peekDatatype(IDatatypePost<T, O> datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original);
}
@Override
public <T> T peekDatatypeOrNull(IDatatypeFor<T> datatype) {
return copy().getDatatypeForOrNull(datatype);
}
@Override
public <T, O> T peekDatatypeOrNull(IDatatypePost<T, O> datatype) {
return copy().getDatatypePostOrNull(datatype, null);
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return copy().getDatatypePost(datatype, original);
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrDefault(D datatype, O original, T def) {
return copy().getDatatypePostOrDefault(datatype, original, def);
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T peekDatatypePostOrNull(D datatype, O original) {
return peekDatatypePostOrDefault(datatype, original, null);
}
@Override
public <T, D extends IDatatypeFor<T>> T peekDatatypeFor(Class<D> datatype) {
return copy().peekDatatypeFor(datatype);
}
@Override
public <T, D extends IDatatypeFor<T>> T peekDatatypeForOrDefault(Class<D> datatype, T def) {
return copy().peekDatatypeForOrDefault(datatype, def);
}
@Override
public <T, D extends IDatatypeFor<T>> T peekDatatypeForOrNull(Class<D> datatype) {
return peekDatatypeForOrDefault(datatype, null);
}
@Override
public ICommandArgument get() throws CommandNotEnoughArgumentsException {
requireMin(1);
ICommandArgument arg = args.removeFirst();
consumed.add(arg);
return arg;
}
@Override
public String getString() throws CommandNotEnoughArgumentsException {
return get().getValue();
}
@Override
public <E extends Enum<?>> E getEnum(Class<E> enumClass) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getEnum(enumClass);
}
@Override
public <E extends Enum<?>> E getEnumOrDefault(Class<E> enumClass, E def) throws CommandNotEnoughArgumentsException {
try {
peekEnum(enumClass);
return getEnum(enumClass);
} catch (CommandInvalidTypeException e) {
return def;
}
}
@Override
public <E extends Enum<?>> E getEnumOrNull(Class<E> enumClass) throws CommandNotEnoughArgumentsException {
return getEnumOrDefault(enumClass, null);
}
@Override
public <T> T getAs(Class<T> type) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
return get().getAs(type);
}
@Override
public <T> T getAsOrDefault(Class<T> type, T def) throws CommandNotEnoughArgumentsException {
try {
T val = peek().getAs(type);
get();
return val;
} catch (CommandInvalidTypeException e) {
return def;
}
}
@Override
public <T> T getAsOrNull(Class<T> type) throws CommandNotEnoughArgumentsException {
return getAsOrDefault(type, null);
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePost(D datatype, O original) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
try {
return datatype.apply(this.context, original);
} catch (Exception e) {
e.printStackTrace();
throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getClass().getSimpleName());
}
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrDefault(D datatype, O original, T _default) {
final List<ICommandArgument> argsSnapshot = new ArrayList<>(this.args);
final List<ICommandArgument> consumedSnapshot = new ArrayList<>(this.consumed);
try {
return this.getDatatypePost(datatype, original);
} catch (Exception e) {
this.args.clear();
this.args.addAll(argsSnapshot);
this.consumed.clear();
this.consumed.addAll(consumedSnapshot);
return _default;
}
}
@Override
public <T, O, D extends IDatatypePost<T, O>> T getDatatypePostOrNull(D datatype, O original) {
return this.getDatatypePostOrDefault(datatype, original, null);
}
@Override
public <T, D extends IDatatypeFor<T>> T getDatatypeFor(D datatype) throws CommandInvalidTypeException, CommandNotEnoughArgumentsException {
try {
return datatype.get(this.context);
} catch (Exception e) {
throw new CommandInvalidTypeException(hasAny() ? peek() : consumed(), datatype.getClass().getSimpleName());
}
}
@Override
public <T, D extends IDatatypeFor<T>> T getDatatypeForOrDefault(D datatype, T def) {
final List<ICommandArgument> argsSnapshot = new ArrayList<>(this.args);
final List<ICommandArgument> consumedSnapshot = new ArrayList<>(this.consumed);
try {
return this.getDatatypeFor(datatype);
} catch (Exception e) {
this.args.clear();
this.args.addAll(argsSnapshot);
this.consumed.clear();
this.consumed.addAll(consumedSnapshot);
return def;
}
}
@Override
public <T, D extends IDatatypeFor<T>> T getDatatypeForOrNull(D datatype) {
return this.getDatatypeForOrDefault(datatype, null);
}
@Override
public <T extends IDatatype> Stream<String> tabCompleteDatatype(T datatype) {
try {
return datatype.tabComplete(this.context);
} catch (Exception e) {
e.printStackTrace();
}
return Stream.empty();
}
@Override
public String rawRest() {
return args.size() > 0 ? args.getFirst().getRawRest() : "";
}
@Override
public void requireMin(int min) throws CommandNotEnoughArgumentsException {
if (args.size() < min) {
throw new CommandNotEnoughArgumentsException(min + consumed.size());
}
}
@Override
public void requireMax(int max) throws CommandTooManyArgumentsException {
if (args.size() > max) {
throw new CommandTooManyArgumentsException(max + consumed.size());
}
}
@Override
public void requireExactly(int args) throws CommandException {
requireMin(args);
requireMax(args);
}
@Override
public boolean hasConsumed() {
return !consumed.isEmpty();
}
@Override
public ICommandArgument consumed() {
return consumed.size() > 0 ? consumed.getLast() : CommandArguments.unknown();
}
@Override
public String consumedString() {
return consumed().getValue();
}
@Override
public ArgConsumer copy() {
return new ArgConsumer(manager, args, consumed);
}
/**
* Implementation of {@link IDatatypeContext} which adapts to the parent {@link IArgConsumer}}
*/
private final class Context implements IDatatypeContext {
@Override
public final IBaritone getBaritone() {
return ArgConsumer.this.manager.getBaritone();
}
@Override
public final ArgConsumer getConsumer() {
return ArgConsumer.this;
}
}
}
@@ -0,0 +1,160 @@
/*
* 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.command.manager;
import baritone.Baritone;
import baritone.api.IBaritone;
import baritone.api.command.Command;
import baritone.api.command.argument.ICommandArgument;
import baritone.api.command.exception.CommandUnhandledException;
import baritone.api.command.exception.ICommandException;
import baritone.api.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.command.manager.ICommandManager;
import baritone.api.command.registry.Registry;
import baritone.command.argument.CommandArguments;
import baritone.command.defaults.DefaultCommands;
import baritone.command.helpers.arguments.ArgConsumer;
import net.minecraft.util.Tuple;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;
/**
* The default, internal implementation of {@link ICommandManager}
*
* @author Brady
* @since 9/21/2019
*/
public class CommandManager implements ICommandManager {
private final Registry<Command> registry = new Registry<>();
private final Baritone baritone;
public CommandManager(Baritone baritone) {
this.baritone = baritone;
DefaultCommands.createAll(baritone).forEach(this.registry::register);
}
@Override
public IBaritone getBaritone() {
return this.baritone;
}
@Override
public Registry<Command> getRegistry() {
return this.registry;
}
@Override
public Command getCommand(String name) {
for (Command command : this.registry.entries) {
if (command.getNames().contains(name.toLowerCase(Locale.US))) {
return command;
}
}
return null;
}
@Override
public boolean execute(String string) {
return this.execute(expand(string));
}
@Override
public boolean execute(Tuple<String, List<ICommandArgument>> expanded) {
ExecutionWrapper execution = this.from(expanded);
if (execution != null) {
execution.execute();
}
return execution != null;
}
@Override
public Stream<String> tabComplete(Tuple<String, List<ICommandArgument>> expanded) {
ExecutionWrapper execution = this.from(expanded);
return execution == null ? Stream.empty() : execution.tabComplete();
}
@Override
public Stream<String> tabComplete(String prefix) {
Tuple<String, List<ICommandArgument>> pair = expand(prefix, true);
String label = pair.getA();
List<ICommandArgument> args = pair.getB();
if (args.isEmpty()) {
return new TabCompleteHelper()
.addCommands(this.baritone.getCommandManager())
.filterPrefix(label)
.stream();
} else {
return tabComplete(pair);
}
}
private ExecutionWrapper from(Tuple<String, List<ICommandArgument>> expanded) {
String label = expanded.getA();
ArgConsumer args = new ArgConsumer(this, expanded.getB());
Command command = this.getCommand(label);
return command == null ? null : new ExecutionWrapper(command, label, args);
}
private static Tuple<String, List<ICommandArgument>> expand(String string, boolean preserveEmptyLast) {
String label = string.split("\\s", 2)[0];
List<ICommandArgument> args = CommandArguments.from(string.substring(label.length()), preserveEmptyLast);
return new Tuple<>(label, args);
}
public static Tuple<String, List<ICommandArgument>> expand(String string) {
return expand(string, false);
}
private static final class ExecutionWrapper {
private Command command;
private String label;
private ArgConsumer args;
private ExecutionWrapper(Command command, String label, ArgConsumer args) {
this.command = command;
this.label = label;
this.args = args;
}
private void execute() {
try {
this.command.execute(this.label, this.args);
} catch (Throwable t) {
// Create a handleable exception, wrap if needed
ICommandException exception = t instanceof ICommandException
? (ICommandException) t
: new CommandUnhandledException(t);
exception.handle(command, args.getArgs());
}
}
private Stream<String> tabComplete() {
try {
return this.command.tabComplete(this.label, this.args);
} catch (Throwable t) {
return Stream.empty();
}
}
}
}
@@ -380,7 +380,7 @@ public final class BuilderProcess extends BaritoneProcessHelper implements IBuil
@Override
public boolean inSchematic(int x, int y, int z, IBlockState currentState) {
return ISchematic.super.inSchematic(x, y, z, currentState) && y >= minYInclusive && y <= maxYInclusive;
return ISchematic.super.inSchematic(x, y, z, currentState) && y >= minYInclusive && y <= maxYInclusive && realSchematic.inSchematic(x, y, z, currentState);
}
@Override
@@ -22,6 +22,7 @@ import baritone.api.pathing.goals.*;
import baritone.api.process.IGetToBlockProcess;
import baritone.api.process.PathingCommand;
import baritone.api.process.PathingCommandType;
import baritone.api.utils.BlockOptionalMeta;
import baritone.api.utils.BlockOptionalMetaLookup;
import baritone.api.utils.Rotation;
import baritone.api.utils.RotationUtils;
@@ -33,14 +34,11 @@ import net.minecraft.init.Blocks;
import net.minecraft.inventory.ContainerPlayer;
import net.minecraft.util.math.BlockPos;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.*;
public final class GetToBlockProcess extends BaritoneProcessHelper implements IGetToBlockProcess {
private Block gettingTo;
private BlockOptionalMeta gettingTo;
private List<BlockPos> knownLocations;
private List<BlockPos> blacklist; // locations we failed to calc to
private BlockPos start;
@@ -53,7 +51,7 @@ public final class GetToBlockProcess extends BaritoneProcessHelper implements IG
}
@Override
public void getToBlock(Block block) {
public void getToBlock(BlockOptionalMeta block) {
onLostControl();
gettingTo = block;
start = ctx.playerFeet();
@@ -109,7 +107,7 @@ public final class GetToBlockProcess extends BaritoneProcessHelper implements IG
}
if (goal.isInGoal(ctx.playerFeet()) && goal.isInGoal(baritone.getPathingBehavior().pathStart()) && isSafeToCancel) {
// we're there
if (rightClickOnArrival(gettingTo)) {
if (rightClickOnArrival(gettingTo.getBlock())) {
if (rightClick()) {
onLostControl();
return new PathingCommand(null, PathingCommandType.CANCEL_AND_SET_GOAL);
@@ -175,16 +173,16 @@ public final class GetToBlockProcess extends BaritoneProcessHelper implements IG
}
private synchronized void rescan(List<BlockPos> known, CalculationContext context) {
List<BlockPos> positions = MineProcess.searchWorld(context, new BlockOptionalMetaLookup(gettingTo), 64, known, blacklist);
List<BlockPos> positions = MineProcess.searchWorld(context, new BlockOptionalMetaLookup(gettingTo), 64, known, blacklist, Collections.emptyList());
positions.removeIf(blacklist::contains);
knownLocations = positions;
}
private Goal createGoal(BlockPos pos) {
if (walkIntoInsteadOfAdjacent(gettingTo)) {
if (walkIntoInsteadOfAdjacent(gettingTo.getBlock())) {
return new GoalTwoBlocks(pos);
}
if (blockOnTopMustBeRemoved(gettingTo) && baritone.bsi.get0(pos.up()).isBlockNormalCube()) {
if (blockOnTopMustBeRemoved(gettingTo.getBlock()) && baritone.bsi.get0(pos.up()).isBlockNormalCube()) {
return new GoalBlock(pos.up());
}
return new GoalGetToBlock(pos);
+43 -21
View File
@@ -39,7 +39,6 @@ import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import java.util.*;
import java.util.stream.Collectors;
@@ -58,6 +57,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
private BlockOptionalMetaLookup filter;
private List<BlockPos> knownOreLocations;
private List<BlockPos> blacklist; // inaccessible
private Map<BlockPos, Long> anticipatedDrops;
private BlockPos branchPoint;
private GoalRunAway branchPointRunaway;
private int desiredQuantity;
@@ -101,6 +101,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
cancel();
return null;
}
updateLoucaSystem();
int mineGoalUpdateInterval = Baritone.settings().mineGoalUpdateInterval.value;
List<BlockPos> curr = new ArrayList<>(knownOreLocations);
if (mineGoalUpdateInterval != 0 && tickCount++ % mineGoalUpdateInterval == 0) { // big brain
@@ -141,6 +142,23 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
return command;
}
private void updateLoucaSystem() {
Map<BlockPos, Long> copy = new HashMap<>(anticipatedDrops);
ctx.getSelectedBlock().ifPresent(pos -> {
if (knownOreLocations.contains(pos)) {
copy.put(pos, System.currentTimeMillis() + Baritone.settings().mineDropLoiterDurationMSThanksLouca.value);
}
});
// elaborate dance to avoid concurrentmodificationexcepption since rescan thread reads this
// don't want to slow everything down with a gross lock do we now
for (BlockPos pos : anticipatedDrops.keySet()) {
if (copy.get(pos) < System.currentTimeMillis()) {
copy.remove(pos);
}
}
anticipatedDrops = copy;
}
@Override
public void onLostControl() {
mine(0, (BlockOptionalMetaLookup) null);
@@ -155,9 +173,10 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
boolean legit = Baritone.settings().legitMine.value;
List<BlockPos> locs = knownOreLocations;
if (!locs.isEmpty()) {
List<BlockPos> locs2 = prune(new CalculationContext(baritone), new ArrayList<>(locs), filter, ORE_LOCATIONS_COUNT, blacklist);
CalculationContext context = new CalculationContext(baritone);
List<BlockPos> locs2 = prune(context, new ArrayList<>(locs), filter, ORE_LOCATIONS_COUNT, blacklist, droppedItemsScan());
// can't reassign locs, gotta make a new var locs2, because we use it in a lambda right here, and variables you use in a lambda must be effectively final
Goal goal = new GoalComposite(locs2.stream().map(loc -> coalesce(loc, locs2)).toArray(Goal[]::new));
Goal goal = new GoalComposite(locs2.stream().map(loc -> coalesce(loc, locs2, context)).toArray(Goal[]::new));
knownOreLocations = locs2;
return new PathingCommand(goal, legit ? PathingCommandType.FORCE_REVALIDATE_GOAL_AND_PATH : PathingCommandType.REVALIDATE_GOAL_AND_PATH);
}
@@ -197,8 +216,9 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
if (Baritone.settings().legitMine.value) {
return;
}
List<BlockPos> locs = searchWorld(context, filter, ORE_LOCATIONS_COUNT, already, blacklist);
locs.addAll(droppedItemsScan(filter, ctx.world()));
List<BlockPos> dropped = droppedItemsScan();
List<BlockPos> locs = searchWorld(context, filter, ORE_LOCATIONS_COUNT, already, blacklist, dropped);
locs.addAll(dropped);
if (locs.isEmpty()) {
logDirect("No locations for " + filter + " known, cancelling");
cancel();
@@ -207,19 +227,19 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
knownOreLocations = locs;
}
private boolean internalMiningGoal(BlockPos pos, IPlayerContext ctx, List<BlockPos> locs) {
private boolean internalMiningGoal(BlockPos pos, CalculationContext context, List<BlockPos> locs) {
// Here, BlockStateInterface is used because the position may be in a cached chunk (the targeted block is one that is kept track of)
if (locs.contains(pos)) {
return true;
}
IBlockState state = BlockStateInterface.get(ctx, pos);
IBlockState state = context.bsi.get0(pos);
if (Baritone.settings().internalMiningAirException.value && state.getBlock() instanceof BlockAir) {
return true;
}
return filter.has(state);
return filter.has(state) && plausibleToBreak(context, pos);
}
private Goal coalesce(BlockPos loc, List<BlockPos> locs) {
private Goal coalesce(BlockPos loc, List<BlockPos> locs, CalculationContext context) {
boolean assumeVerticalShaftMine = !(baritone.bsi.get0(loc.up()).getBlock() instanceof BlockFalling);
if (!Baritone.settings().forceInternalMining.value) {
if (assumeVerticalShaftMine) {
@@ -230,9 +250,9 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
return new GoalTwoBlocks(loc);
}
}
boolean upwardGoal = internalMiningGoal(loc.up(), ctx, locs);
boolean downwardGoal = internalMiningGoal(loc.down(), ctx, locs);
boolean doubleDownwardGoal = internalMiningGoal(loc.down(2), ctx, locs);
boolean upwardGoal = internalMiningGoal(loc.up(), context, locs);
boolean downwardGoal = internalMiningGoal(loc.down(), context, locs);
boolean doubleDownwardGoal = internalMiningGoal(loc.down(2), context, locs);
if (upwardGoal == downwardGoal) { // symmetric
if (doubleDownwardGoal && assumeVerticalShaftMine) {
// we have a checkerboard like pattern
@@ -281,12 +301,12 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
}
}
public static List<BlockPos> droppedItemsScan(BlockOptionalMetaLookup filter, World world) {
public List<BlockPos> droppedItemsScan() {
if (!Baritone.settings().mineScanDroppedItems.value) {
return Collections.emptyList();
}
List<BlockPos> ret = new ArrayList<>();
for (Entity entity : world.loadedEntityList) {
for (Entity entity : ctx.world().loadedEntityList) {
if (entity instanceof EntityItem) {
EntityItem ei = (EntityItem) entity;
if (filter.has(ei.getItem())) {
@@ -294,10 +314,11 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
}
}
}
ret.addAll(anticipatedDrops.keySet());
return ret;
}
public static List<BlockPos> searchWorld(CalculationContext ctx, BlockOptionalMetaLookup filter, int max, List<BlockPos> alreadyKnown, List<BlockPos> blacklist) {
public static List<BlockPos> searchWorld(CalculationContext ctx, BlockOptionalMetaLookup filter, int max, List<BlockPos> alreadyKnown, List<BlockPos> blacklist, List<BlockPos> dropped) {
List<BlockPos> locs = new ArrayList<>();
List<Block> untracked = new ArrayList<>();
for (BlockOptionalMeta bom : filter.blocks()) {
@@ -318,7 +339,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
}
}
locs = prune(ctx, locs, filter, max, blacklist);
locs = prune(ctx, locs, filter, max, blacklist, dropped);
if (!untracked.isEmpty() || (Baritone.settings().extendCacheOnThreshold.value && locs.size() < max)) {
locs.addAll(WorldScanner.INSTANCE.scanChunkRadius(
@@ -332,11 +353,12 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
locs.addAll(alreadyKnown);
return prune(ctx, locs, filter, max, blacklist);
return prune(ctx, locs, filter, max, blacklist, dropped);
}
private void addNearby() {
knownOreLocations.addAll(droppedItemsScan(filter, ctx.world()));
List<BlockPos> dropped = droppedItemsScan();
knownOreLocations.addAll(dropped);
BlockPos playerFeet = ctx.playerFeet();
BlockStateInterface bsi = new BlockStateInterface(ctx);
int searchDist = 10;
@@ -355,11 +377,10 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
}
}
}
knownOreLocations = prune(new CalculationContext(baritone), knownOreLocations, filter, ORE_LOCATIONS_COUNT, blacklist);
knownOreLocations = prune(new CalculationContext(baritone), knownOreLocations, filter, ORE_LOCATIONS_COUNT, blacklist, dropped);
}
private static List<BlockPos> prune(CalculationContext ctx, List<BlockPos> locs2, BlockOptionalMetaLookup filter, int max, List<BlockPos> blacklist) {
List<BlockPos> dropped = droppedItemsScan(filter, ctx.world);
private static List<BlockPos> prune(CalculationContext ctx, List<BlockPos> locs2, BlockOptionalMetaLookup filter, int max, List<BlockPos> blacklist, List<BlockPos> dropped) {
dropped.removeIf(drop -> {
for (BlockPos pos : locs2) {
if (pos.distanceSq(drop) <= 9 && filter.has(ctx.get(pos.getX(), pos.getY(), pos.getZ())) && MineProcess.plausibleToBreak(ctx, pos)) { // TODO maybe drop also has to be supported? no lava below?
@@ -416,6 +437,7 @@ public final class MineProcess extends BaritoneProcessHelper implements IMinePro
this.blacklist = new ArrayList<>();
this.branchPoint = null;
this.branchPointRunaway = null;
this.anticipatedDrops = new HashMap<>();
if (filter != null) {
rescan(new ArrayList<>(), new CalculationContext(baritone));
}
+1 -1
View File
@@ -39,7 +39,7 @@ import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Collections;
import static baritone.api.utils.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
import static baritone.api.command.IBaritoneChatControl.FORCE_COMMAND_PREFIX;
import static org.lwjgl.opengl.GL11.*;
public class GuiClick extends GuiScreen {
@@ -1,94 +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.pathing.goals.Goal;
import baritone.api.process.ICustomGoalProcess;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.datatypes.RelativeCoordinate;
import baritone.api.utils.command.datatypes.RelativeGoal;
import baritone.api.utils.command.exception.CommandException;
import baritone.api.utils.command.exception.CommandInvalidStateException;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.cache.WorldScanner;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class PathCommand extends Command {
public PathCommand(IBaritone baritone) {
super(baritone, Arrays.asList("path", "goto"));
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess();
Goal goal;
if (args.hasAny()) {
args.requireMax(3);
goal = args.getDatatypePost(RelativeGoal.INSTANCE, ctx.playerFeet());
} else if ((goal = customGoalProcess.getGoal()) == null) {
throw new CommandInvalidStateException("No goal");
}
args.requireMax(0);
WorldScanner.INSTANCE.repack(ctx);
customGoalProcess.setGoalAndPath(goal);
logDirect("Now pathing");
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
if (args.hasAny() && !args.has(4)) {
while (args.has(2)) {
if (args.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) == null) {
break;
}
args.get();
if (!args.has(2)) {
return new TabCompleteHelper()
.append("~")
.filterPrefix(args.getString())
.stream();
}
}
}
return Stream.empty();
}
@Override
public String getShortDesc() {
return "Start heading towards a goal";
}
@Override
public List<String> getLongDesc() {
return Arrays.asList(
"The path command tells Baritone to head towards the current goal.",
"",
"Usage:",
"> path - Start the pathing.",
"> path <y>",
"> path <x> <z>",
"> path <x> <y> <z> - Define the goal here"
);
}
}
@@ -1,85 +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.execution;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.exception.CommandUnhandledException;
import baritone.api.utils.command.exception.ICommandException;
import baritone.api.utils.command.execution.ICommandExecution;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.utils.command.manager.CommandManager;
import java.util.stream.Stream;
/**
* The default, internal implementation of {@link ICommandExecution}, which is used by {@link CommandManager}
*
* @author LoganDark, Brady
*/
public class CommandExecution implements ICommandExecution {
/**
* The command itself
*/
private final Command command;
/**
* The name this command was called with
*/
private final String label;
/**
* The arg consumer
*/
private final ArgConsumer args;
public CommandExecution(Command command, String label, ArgConsumer args) {
this.command = command;
this.label = label;
this.args = args;
}
@Override
public String getLabel() {
return this.label;
}
@Override
public ArgConsumer getArguments() {
return this.args;
}
@Override
public void execute() {
try {
command.execute(this);
} catch (Throwable t) {
// Create a handleable exception, wrap if needed
ICommandException exception = t instanceof ICommandException
? (ICommandException) t
: new CommandUnhandledException(t);
exception.handle(command, args.args);
}
}
@Override
public Stream<String> tabComplete() {
return command.tabComplete(this);
}
}
@@ -1,115 +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.manager;
import baritone.Baritone;
import baritone.api.IBaritone;
import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.execution.ICommandExecution;
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.utils.command.manager.ICommandManager;
import baritone.api.utils.command.registry.Registry;
import baritone.utils.command.defaults.DefaultCommands;
import baritone.utils.command.execution.CommandExecution;
import net.minecraft.util.Tuple;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;
/**
* The default, internal implementation of {@link ICommandManager}
*
* @author Brady
* @since 9/21/2019
*/
public class CommandManager implements ICommandManager {
private final Registry<Command> registry = new Registry<>();
private final Baritone baritone;
public CommandManager(Baritone baritone) {
this.baritone = baritone;
DefaultCommands.createAll(baritone).forEach(this.registry::register);
}
@Override
public IBaritone getBaritone() {
return this.baritone;
}
@Override
public Registry<Command> getRegistry() {
return this.registry;
}
@Override
public Command getCommand(String name) {
for (Command command : this.registry.entries) {
if (command.names.contains(name.toLowerCase(Locale.US))) {
return command;
}
}
return null;
}
@Override
public boolean execute(String string) {
return this.execute(ICommandExecution.expand(string));
}
@Override
public boolean execute(Tuple<String, List<CommandArgument>> expanded) {
ICommandExecution execution = this.from(expanded);
if (execution != null) {
execution.execute();
}
return execution != null;
}
@Override
public Stream<String> tabComplete(Tuple<String, List<CommandArgument>> expanded) {
ICommandExecution execution = this.from(expanded);
return execution == null ? Stream.empty() : execution.tabComplete();
}
@Override
public Stream<String> tabComplete(String prefix) {
Tuple<String, List<CommandArgument>> pair = ICommandExecution.expand(prefix, true);
String label = pair.getA();
List<CommandArgument> args = pair.getB();
if (args.isEmpty()) {
return new TabCompleteHelper()
.addCommands(this.baritone.getCommandManager())
.filterPrefix(label)
.stream();
} else {
return tabComplete(pair);
}
}
private ICommandExecution from(Tuple<String, List<CommandArgument>> expanded) {
String label = expanded.getA();
ArgConsumer args = new ArgConsumer(this, expanded.getB());
Command command = this.getCommand(label);
return command == null ? null : new CommandExecution(command, label, args);
}
}