Remove ICommandExecution due to redundancy

This commit is contained in:
Brady 2019-09-30 21:41:30 -05:00
parent 47e6a039ef
commit f1fb109d40
No known key found for this signature in database
GPG Key ID: 73A788379A197567
38 changed files with 126 additions and 262 deletions

View File

@ -21,7 +21,6 @@ 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 java.util.Collections;
@ -57,41 +56,16 @@ public abstract class Command implements Helper {
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, ArgConsumer 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, ArgConsumer args) throws CommandException;
/**
* @return A <b>single-line</b> string containing a short description of this command's purpose.

View File

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

View File

@ -29,10 +29,10 @@ 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.utils.command.manager.CommandManager;
import net.minecraft.util.Tuple;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentString;
@ -67,7 +67,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).getFirst()).handle(null, null);
new CommandNotFoundException(CommandManager.expand(commandStr).getFirst()).handle(null, null);
}
} else if ((settings.chatControl.value || settings.chatControlAnyway.value) && runCommand(msg)) {
event.cancel();
@ -106,7 +106,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<CommandArgument>> pair = CommandManager.expand(msg);
String command = pair.getFirst();
String rest = msg.substring(pair.getFirst().length());
ArgConsumer argc = new ArgConsumer(this.manager, pair.getSecond());

View File

@ -35,7 +35,7 @@ public class AxisCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -35,7 +35,7 @@ public class BlacklistCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

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

View File

@ -33,14 +33,14 @@ public class CancelCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -41,7 +41,7 @@ public class ChestsCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -33,14 +33,14 @@ public class ClickCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -37,7 +37,7 @@ public class ComeCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -43,12 +43,12 @@ public class CommandAlias extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return this.baritone.getCommandManager().tabComplete(String.format("%s %s", target, args.rawRest()));
}

View File

@ -35,7 +35,7 @@ public class ExploreCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
if (args.hasAtMost(2)) {
return args.tabCompleteDatatype(RelativeGoalXZ.INSTANCE);
}

View 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, ArgConsumer 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, ArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return RelativeFile.tabComplete(args, RelativeFile.gameDir());
}

View File

@ -33,14 +33,14 @@ public class FarmCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -37,7 +37,7 @@ public class FindCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer args) throws CommandException {
List<Block> toFind = new ArrayList<>();
while (args.hasAny()) {
toFind.add(args.getDatatypeFor(BlockById.INSTANCE));
@ -59,7 +59,7 @@ public class FindCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, ArgConsumer args) {
return args.tabCompleteDatatype(BlockById.INSTANCE);
}

View File

@ -42,7 +42,7 @@ public class FollowCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer args) throws CommandException {
args.requireMin(1);
FollowGroup group;
FollowList list;
@ -88,7 +88,7 @@ public class FollowCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, ArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.append(FollowGroup.class)

View File

@ -34,7 +34,7 @@ public class ForceCancelCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -33,14 +33,14 @@ public class GcCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -39,7 +39,7 @@ public class GoalCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) throws CommandException {
TabCompleteHelper helper = new TabCompleteHelper();
if (args.hasExactlyOne()) {
helper.append("reset", "clear", "none", "~");

View File

@ -44,7 +44,7 @@ public class HelpCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer args) throws CommandException {
args.requireMax(1);
if (!args.hasAny() || args.is(Integer.class)) {
Paginator.paginate(
@ -97,7 +97,7 @@ public class HelpCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, ArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.addCommands(this.baritone.getCommandManager())

View File

@ -37,7 +37,7 @@ public class InvertCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -38,7 +38,7 @@ public class MineCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return args.tabCompleteDatatype(BlockById.INSTANCE);
}

View File

@ -40,7 +40,7 @@ public class PathCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer args) throws CommandException {
ICustomGoalProcess customGoalProcess = baritone.getCustomGoalProcess();
Goal goal;
if (args.hasAny()) {
@ -56,7 +56,7 @@ public class PathCommand extends Command {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
public Stream<String> tabComplete(String label, ArgConsumer args) throws CommandException {
if (args.hasAny() && !args.has(4)) {
while (args.has(2)) {
if (args.peekDatatypeOrNull(RelativeCoordinate.INSTANCE) == null) {

View File

@ -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, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}
@ -112,7 +112,7 @@ public class PauseResumeCommands {
};
resumeCommand = new Command(baritone, "resume") {
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer args) throws CommandException {
args.requireMax(0);
if (!paused[0]) {
throw new CommandInvalidStateException("Not paused");
@ -122,7 +122,7 @@ public class PauseResumeCommands {
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, ArgConsumer args) {
return Stream.empty();
}
@ -143,13 +143,13 @@ public class PauseResumeCommands {
};
pausedCommand = new Command(baritone, "paused") {
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -37,7 +37,7 @@ public class ProcCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -33,14 +33,14 @@ public class ReloadAllCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -34,7 +34,7 @@ public class RenderCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -34,13 +34,13 @@ public class RepackCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -33,14 +33,14 @@ public class SaveAllCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -33,13 +33,13 @@ public class SchematicaCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer args) throws CommandException {
args.requireMax(0);
baritone.getBuilderProcess().buildOpenSchematic();
}
@Override
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
public Stream<String> tabComplete(String label, ArgConsumer args) {
return Stream.empty();
}

View File

@ -75,7 +75,7 @@ public class SelCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) throws CommandException {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()
.append(Action.getAllNames())

View File

@ -50,7 +50,7 @@ public class SetCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) throws CommandException {
if (args.hasAny()) {
String arg = args.getString();
if (args.hasExactlyOne() && !Arrays.asList("s", "save").contains(args.peekString().toLowerCase(Locale.US))) {

View File

@ -34,7 +34,7 @@ public class ThisWayCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -35,7 +35,7 @@ public class TunnelCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -34,7 +34,7 @@ public class VersionCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) {
return Stream.empty();
}

View File

@ -52,7 +52,7 @@ public class WaypointsCommand extends Command {
}
@Override
protected void executed(String label, ArgConsumer args) throws CommandException {
public void execute(String label, ArgConsumer 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, ArgConsumer args) throws CommandException {
if (args.hasAny()) {
if (args.hasExactlyOne()) {
return new TabCompleteHelper()

View File

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

View File

@ -21,13 +21,13 @@ 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.exception.CommandUnhandledException;
import baritone.api.utils.command.exception.ICommandException;
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;
@ -72,12 +72,12 @@ public class CommandManager implements ICommandManager {
@Override
public boolean execute(String string) {
return this.execute(ICommandExecution.expand(string));
return this.execute(expand(string));
}
@Override
public boolean execute(Tuple<String, List<CommandArgument>> expanded) {
ICommandExecution execution = this.from(expanded);
ExecutionWrapper execution = this.from(expanded);
if (execution != null) {
execution.execute();
}
@ -86,13 +86,13 @@ public class CommandManager implements ICommandManager {
@Override
public Stream<String> tabComplete(Tuple<String, List<CommandArgument>> expanded) {
ICommandExecution execution = this.from(expanded);
ExecutionWrapper execution = this.from(expanded);
return execution == null ? Stream.empty() : execution.tabComplete();
}
@Override
public Stream<String> tabComplete(String prefix) {
Tuple<String, List<CommandArgument>> pair = ICommandExecution.expand(prefix, true);
Tuple<String, List<CommandArgument>> pair = expand(prefix, true);
String label = pair.getFirst();
List<CommandArgument> args = pair.getSecond();
if (args.isEmpty()) {
@ -105,11 +105,54 @@ public class CommandManager implements ICommandManager {
}
}
private ICommandExecution from(Tuple<String, List<CommandArgument>> expanded) {
private ExecutionWrapper from(Tuple<String, List<CommandArgument>> expanded) {
String label = expanded.getFirst();
ArgConsumer args = new ArgConsumer(this, expanded.getSecond());
Command command = this.getCommand(label);
return command == null ? null : new CommandExecution(command, label, args);
return command == null ? null : new ExecutionWrapper(command, label, args);
}
private 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);
}
public static Tuple<String, List<CommandArgument>> 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.args);
}
}
private Stream<String> tabComplete() {
try {
return this.command.tabComplete(this.label, this.args);
} catch (Throwable t) {
return Stream.empty();
}
}
}
}