diff --git a/src/api/java/baritone/api/utils/command/Command.java b/src/api/java/baritone/api/utils/command/Command.java index 28d42bfd..e83d6c50 100644 --- a/src/api/java/baritone/api/utils/command/Command.java +++ b/src/api/java/baritone/api/utils/command/Command.java @@ -21,7 +21,7 @@ import baritone.api.IBaritone; import baritone.api.utils.Helper; import baritone.api.utils.IPlayerContext; import baritone.api.utils.command.exception.CommandException; -import baritone.api.utils.command.execution.CommandExecution; +import baritone.api.utils.command.execution.ICommandExecution; import baritone.api.utils.command.helpers.arguments.ArgConsumer; import java.util.Collections; @@ -62,19 +62,21 @@ public abstract class Command implements Helper { * * @param execution The command execution to execute this command with */ - public void execute(CommandExecution execution) throws CommandException { - executed(execution.label, execution.args); + public final void execute(ICommandExecution execution) throws CommandException { + this.executed(execution.getLabel(), execution.getArguments()); } /** - * Tab completes this command with the specified arguments. This won't throw any exceptions ever. + * 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 Stream tabComplete(CommandExecution execution) { + public final Stream tabComplete(ICommandExecution execution) { try { - return tabCompleted(execution.label, execution.args); + return this.tabCompleted(execution.getLabel(), execution.getArguments()); } catch (Throwable t) { return Stream.empty(); } diff --git a/src/api/java/baritone/api/utils/command/execution/ICommandExecution.java b/src/api/java/baritone/api/utils/command/execution/ICommandExecution.java new file mode 100644 index 00000000..4ca5b193 --- /dev/null +++ b/src/api/java/baritone/api/utils/command/execution/ICommandExecution.java @@ -0,0 +1,68 @@ +/* + * This file is part of Baritone. + * + * Baritone is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Baritone is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Baritone. If not, see . + */ + +package baritone.api.utils.command.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 tabComplete(); + + static Tuple> expand(String string, boolean preserveEmptyLast) { + String label = string.split("\\s", 2)[0]; + List args = CommandArgument.from(string.substring(label.length()), preserveEmptyLast); + return new Tuple<>(label, args); + } + + static Tuple> expand(String string) { + return expand(string, false); + } +} diff --git a/src/api/java/baritone/api/utils/command/helpers/tabcomplete/TabCompleteHelper.java b/src/api/java/baritone/api/utils/command/helpers/tabcomplete/TabCompleteHelper.java index e2cf5701..db63b2d5 100644 --- a/src/api/java/baritone/api/utils/command/helpers/tabcomplete/TabCompleteHelper.java +++ b/src/api/java/baritone/api/utils/command/helpers/tabcomplete/TabCompleteHelper.java @@ -22,7 +22,6 @@ 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.execution.CommandExecution; import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.manager.ICommandManager; import net.minecraft.util.ResourceLocation; @@ -46,7 +45,7 @@ import java.util.stream.Stream; * {@link #filterPrefix(String)} *
  • Get the stream using {@link #stream()}
  • *
  • Pass it up to whatever's calling your tab complete function (i.e. - * {@link ICommandManager#tabComplete(CommandExecution)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)})
  • + * {@link ICommandManager#tabComplete(String)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)}) * *

    * For advanced users: if you're intercepting {@link TabCompleteEvent}s directly, use {@link #build()} instead for an diff --git a/src/api/java/baritone/api/utils/command/manager/ICommandManager.java b/src/api/java/baritone/api/utils/command/manager/ICommandManager.java index 2ed87a64..63682433 100644 --- a/src/api/java/baritone/api/utils/command/manager/ICommandManager.java +++ b/src/api/java/baritone/api/utils/command/manager/ICommandManager.java @@ -20,9 +20,8 @@ package baritone.api.utils.command.manager; import baritone.api.IBaritone; import baritone.api.utils.command.Command; import baritone.api.utils.command.argument.CommandArgument; -import baritone.api.utils.command.execution.CommandExecution; import baritone.api.utils.command.registry.Registry; -import com.mojang.realmsclient.util.Pair; +import net.minecraft.util.Tuple; import java.util.List; import java.util.stream.Stream; @@ -43,13 +42,11 @@ public interface ICommandManager { */ Command getCommand(String name); - void execute(CommandExecution execution); - boolean execute(String string); - Stream tabComplete(CommandExecution execution); + boolean execute(Tuple> expanded); - Stream tabComplete(Pair> pair); + Stream tabComplete(Tuple> expanded); Stream tabComplete(String prefix); } diff --git a/src/main/java/baritone/utils/command/BaritoneChatControl.java b/src/main/java/baritone/utils/command/BaritoneChatControl.java index 352aff1c..facc24da 100644 --- a/src/main/java/baritone/utils/command/BaritoneChatControl.java +++ b/src/main/java/baritone/utils/command/BaritoneChatControl.java @@ -29,11 +29,11 @@ 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.CommandExecution; +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 com.mojang.realmsclient.util.Pair; +import net.minecraft.util.Tuple; import net.minecraft.util.text.ITextComponent; import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextFormatting; @@ -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(CommandExecution.expand(commandStr).first()).handle(null, null); + new CommandNotFoundException(ICommandExecution.expand(commandStr).getFirst()).handle(null, null); } } else if ((settings.chatControl.value || settings.chatControlAnyway.value) && runCommand(msg)) { event.cancel(); @@ -106,10 +106,10 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener { if (msg.isEmpty()) { return this.runCommand("help"); } - Pair> pair = CommandExecution.expand(msg); - String command = pair.first(); - String rest = msg.substring(pair.first().length()); - ArgConsumer argc = new ArgConsumer(this.manager, pair.second()); + Tuple> pair = ICommandExecution.expand(msg); + String command = pair.getFirst(); + String rest = msg.substring(pair.getFirst().length()); + ArgConsumer argc = new ArgConsumer(this.manager, pair.getSecond()); if (!argc.hasAny()) { Settings.Setting setting = settings.byLowerName.get(command.toLowerCase(Locale.US)); if (setting != null) { @@ -126,7 +126,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener { if (setting.getName().equals("logger")) { continue; } - if (setting.getName().equalsIgnoreCase(pair.first())) { + if (setting.getName().equalsIgnoreCase(pair.getFirst())) { logRanCommand(command, rest); try { this.manager.execute(String.format("set %s %s", setting.getName(), argc.getString())); @@ -135,13 +135,13 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener { } } } - CommandExecution execution = CommandExecution.from(this.manager, pair); - if (execution == null) { - return false; + + // If the command exists, then handle echoing the input + if (this.manager.getCommand(pair.getFirst()) != null) { + logRanCommand(command, rest); } - logRanCommand(command, rest); - this.manager.execute(execution); - return true; + + return this.manager.execute(pair); } @Override diff --git a/src/api/java/baritone/api/utils/command/execution/CommandExecution.java b/src/main/java/baritone/utils/command/execution/CommandExecution.java similarity index 53% rename from src/api/java/baritone/api/utils/command/execution/CommandExecution.java rename to src/main/java/baritone/utils/command/execution/CommandExecution.java index 181c0e6b..1e8fe838 100644 --- a/src/api/java/baritone/api/utils/command/execution/CommandExecution.java +++ b/src/main/java/baritone/utils/command/execution/CommandExecution.java @@ -15,21 +15,23 @@ * along with Baritone. If not, see . */ -package baritone.api.utils.command.execution; +package baritone.utils.command.execution; import baritone.api.utils.command.Command; -import baritone.api.utils.command.argument.CommandArgument; -import baritone.api.utils.command.exception.CommandException; 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.api.utils.command.manager.ICommandManager; -import com.mojang.realmsclient.util.Pair; +import baritone.utils.command.manager.CommandManager; -import java.util.List; import java.util.stream.Stream; -public class CommandExecution { +/** + * The default, internal implementation of {@link ICommandExecution}, which is used by {@link CommandManager} + * + * @author LoganDark, Brady + */ +public class CommandExecution implements ICommandExecution { /** * The command itself @@ -39,12 +41,12 @@ public class CommandExecution { /** * The name this command was called with */ - public final String label; + private final String label; /** * The arg consumer */ - public final ArgConsumer args; + private final ArgConsumer args; public CommandExecution(Command command, String label, ArgConsumer args) { this.command = command; @@ -52,20 +54,17 @@ public class CommandExecution { this.args = args; } - public static String getLabel(String string) { - return string.split("\\s", 2)[0]; + @Override + public String getLabel() { + return this.label; } - public static Pair> expand(String string, boolean preserveEmptyLast) { - String label = getLabel(string); - List args = CommandArgument.from(string.substring(label.length()), preserveEmptyLast); - return Pair.of(label, args); - } - - public static Pair> expand(String string) { - return expand(string, false); + @Override + public ArgConsumer getArguments() { + return this.args; } + @Override public void execute() { try { command.execute(this); @@ -79,27 +78,8 @@ public class CommandExecution { } } + @Override public Stream tabComplete() { return command.tabComplete(this); } - - public static CommandExecution from(ICommandManager manager, String label, ArgConsumer args) { - Command command = manager.getCommand(label); - if (command == null) { - return null; - } - return new CommandExecution( - command, - label, - args - ); - } - - public static CommandExecution from(ICommandManager manager, Pair> pair) { - return from(manager, pair.first(), new ArgConsumer(manager, pair.second())); - } - - public static CommandExecution from(ICommandManager manager, String string) { - return from(manager, expand(string)); - } } diff --git a/src/main/java/baritone/utils/command/manager/CommandManager.java b/src/main/java/baritone/utils/command/manager/CommandManager.java index 46e79825..e5c5d035 100644 --- a/src/main/java/baritone/utils/command/manager/CommandManager.java +++ b/src/main/java/baritone/utils/command/manager/CommandManager.java @@ -21,18 +21,22 @@ 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.CommandExecution; +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 com.mojang.realmsclient.util.Pair; +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 */ @@ -67,13 +71,13 @@ public class CommandManager implements ICommandManager { } @Override - public void execute(CommandExecution execution) { - execution.execute(); + public boolean execute(String string) { + return this.execute(ICommandExecution.expand(string)); } @Override - public boolean execute(String string) { - CommandExecution execution = CommandExecution.from(this, string); + public boolean execute(Tuple> expanded) { + ICommandExecution execution = this.from(expanded); if (execution != null) { execution.execute(); } @@ -81,21 +85,16 @@ public class CommandManager implements ICommandManager { } @Override - public Stream tabComplete(CommandExecution execution) { - return execution.tabComplete(); - } - - @Override - public Stream tabComplete(Pair> pair) { - CommandExecution execution = CommandExecution.from(this, pair); - return execution == null ? Stream.empty() : tabComplete(execution); + public Stream tabComplete(Tuple> expanded) { + ICommandExecution execution = this.from(expanded); + return execution == null ? Stream.empty() : execution.tabComplete(); } @Override public Stream tabComplete(String prefix) { - Pair> pair = CommandExecution.expand(prefix, true); - String label = pair.first(); - List args = pair.second(); + Tuple> pair = ICommandExecution.expand(prefix, true); + String label = pair.getFirst(); + List args = pair.getSecond(); if (args.isEmpty()) { return new TabCompleteHelper() .addCommands(this.baritone.getCommandManager()) @@ -105,4 +104,12 @@ public class CommandManager implements ICommandManager { return tabComplete(pair); } } + + private ICommandExecution from(Tuple> 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); + } }