Some basic API/Main separation

This commit is contained in:
Brady 2019-09-28 10:21:07 -05:00
parent a75b90fcef
commit 6a90b57ced
No known key found for this signature in database
GPG Key ID: 73A788379A197567
7 changed files with 137 additions and 84 deletions

View File

@ -21,7 +21,7 @@ import baritone.api.IBaritone;
import baritone.api.utils.Helper; import baritone.api.utils.Helper;
import baritone.api.utils.IPlayerContext; import baritone.api.utils.IPlayerContext;
import baritone.api.utils.command.exception.CommandException; import baritone.api.utils.command.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 baritone.api.utils.command.helpers.arguments.ArgConsumer;
import java.util.Collections; import java.util.Collections;
@ -62,19 +62,21 @@ public abstract class Command implements Helper {
* *
* @param execution The command execution to execute this command with * @param execution The command execution to execute this command with
*/ */
public void execute(CommandExecution execution) throws CommandException { public final void execute(ICommandExecution execution) throws CommandException {
executed(execution.label, execution.args); 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 * @param execution The command execution to tab complete
* @return The list of completions. * @return The list of completions.
*/ */
public Stream<String> tabComplete(CommandExecution execution) { public final Stream<String> tabComplete(ICommandExecution execution) {
try { try {
return tabCompleted(execution.label, execution.args); return this.tabCompleted(execution.getLabel(), execution.getArguments());
} catch (Throwable t) { } catch (Throwable t) {
return Stream.empty(); return Stream.empty();
} }

View File

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

@ -22,7 +22,6 @@ import baritone.api.Settings;
import baritone.api.event.events.TabCompleteEvent; import baritone.api.event.events.TabCompleteEvent;
import baritone.api.utils.SettingsUtil; import baritone.api.utils.SettingsUtil;
import baritone.api.utils.command.datatypes.IDatatype; 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.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.manager.ICommandManager; import baritone.api.utils.command.manager.ICommandManager;
import net.minecraft.util.ResourceLocation; import net.minecraft.util.ResourceLocation;
@ -46,7 +45,7 @@ import java.util.stream.Stream;
* {@link #filterPrefix(String)}</li> * {@link #filterPrefix(String)}</li>
* <li>Get the stream using {@link #stream()}</li> * <li>Get the stream using {@link #stream()}</li>
* <li>Pass it up to whatever's calling your tab complete function (i.e. * <li>Pass it up to whatever's calling your tab complete function (i.e.
* {@link ICommandManager#tabComplete(CommandExecution)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)})</li> * {@link ICommandManager#tabComplete(String)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)})</li>
* </ul> * </ul>
* <p> * <p>
* For advanced users: if you're intercepting {@link TabCompleteEvent}s directly, use {@link #build()} instead for an * For advanced users: if you're intercepting {@link TabCompleteEvent}s directly, use {@link #build()} instead for an

View File

@ -20,9 +20,8 @@ package baritone.api.utils.command.manager;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument; import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.execution.CommandExecution;
import baritone.api.utils.command.registry.Registry; 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.List;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -43,13 +42,11 @@ public interface ICommandManager {
*/ */
Command getCommand(String name); Command getCommand(String name);
void execute(CommandExecution execution);
boolean execute(String string); boolean execute(String string);
Stream<String> tabComplete(CommandExecution execution); boolean execute(Tuple<String, List<CommandArgument>> expanded);
Stream<String> tabComplete(Pair<String, List<CommandArgument>> pair); Stream<String> tabComplete(Tuple<String, List<CommandArgument>> expanded);
Stream<String> tabComplete(String prefix); Stream<String> tabComplete(String prefix);
} }

View File

@ -29,11 +29,11 @@ import baritone.api.utils.SettingsUtil;
import baritone.api.utils.command.argument.CommandArgument; import baritone.api.utils.command.argument.CommandArgument;
import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException; import baritone.api.utils.command.exception.CommandNotEnoughArgumentsException;
import baritone.api.utils.command.exception.CommandNotFoundException; import baritone.api.utils.command.exception.CommandNotFoundException;
import baritone.api.utils.command.execution.CommandExecution; import baritone.api.utils.command.execution.ICommandExecution;
import baritone.api.utils.command.helpers.arguments.ArgConsumer; import baritone.api.utils.command.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper; import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.utils.command.manager.ICommandManager; 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.ITextComponent;
import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting; import net.minecraft.util.text.TextFormatting;
@ -67,7 +67,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
event.cancel(); event.cancel();
String commandStr = msg.substring(forceRun ? FORCE_COMMAND_PREFIX.length() : prefix.length()); String commandStr = msg.substring(forceRun ? FORCE_COMMAND_PREFIX.length() : prefix.length());
if (!runCommand(commandStr) && !commandStr.trim().isEmpty()) { 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)) { } else if ((settings.chatControl.value || settings.chatControlAnyway.value) && runCommand(msg)) {
event.cancel(); event.cancel();
@ -106,10 +106,10 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
if (msg.isEmpty()) { if (msg.isEmpty()) {
return this.runCommand("help"); return this.runCommand("help");
} }
Pair<String, List<CommandArgument>> pair = CommandExecution.expand(msg); Tuple<String, List<CommandArgument>> pair = ICommandExecution.expand(msg);
String command = pair.first(); String command = pair.getFirst();
String rest = msg.substring(pair.first().length()); String rest = msg.substring(pair.getFirst().length());
ArgConsumer argc = new ArgConsumer(this.manager, pair.second()); ArgConsumer argc = new ArgConsumer(this.manager, pair.getSecond());
if (!argc.hasAny()) { if (!argc.hasAny()) {
Settings.Setting setting = settings.byLowerName.get(command.toLowerCase(Locale.US)); Settings.Setting setting = settings.byLowerName.get(command.toLowerCase(Locale.US));
if (setting != null) { if (setting != null) {
@ -126,7 +126,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
if (setting.getName().equals("logger")) { if (setting.getName().equals("logger")) {
continue; continue;
} }
if (setting.getName().equalsIgnoreCase(pair.first())) { if (setting.getName().equalsIgnoreCase(pair.getFirst())) {
logRanCommand(command, rest); logRanCommand(command, rest);
try { try {
this.manager.execute(String.format("set %s %s", setting.getName(), argc.getString())); 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) { // If the command exists, then handle echoing the input
return false; if (this.manager.getCommand(pair.getFirst()) != null) {
logRanCommand(command, rest);
} }
logRanCommand(command, rest);
this.manager.execute(execution); return this.manager.execute(pair);
return true;
} }
@Override @Override

View File

@ -15,21 +15,23 @@
* along with Baritone. If not, see <https://www.gnu.org/licenses/>. * along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/ */
package baritone.api.utils.command.execution; package baritone.utils.command.execution;
import baritone.api.utils.command.Command; 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.CommandUnhandledException;
import baritone.api.utils.command.exception.ICommandException; 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.helpers.arguments.ArgConsumer;
import baritone.api.utils.command.manager.ICommandManager; import baritone.utils.command.manager.CommandManager;
import com.mojang.realmsclient.util.Pair;
import java.util.List;
import java.util.stream.Stream; 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 * The command itself
@ -39,12 +41,12 @@ public class CommandExecution {
/** /**
* The name this command was called with * The name this command was called with
*/ */
public final String label; private final String label;
/** /**
* The arg consumer * The arg consumer
*/ */
public final ArgConsumer args; private final ArgConsumer args;
public CommandExecution(Command command, String label, ArgConsumer args) { public CommandExecution(Command command, String label, ArgConsumer args) {
this.command = command; this.command = command;
@ -52,20 +54,17 @@ public class CommandExecution {
this.args = args; this.args = args;
} }
public static String getLabel(String string) { @Override
return string.split("\\s", 2)[0]; public String getLabel() {
return this.label;
} }
public static Pair<String, List<CommandArgument>> expand(String string, boolean preserveEmptyLast) { @Override
String label = getLabel(string); public ArgConsumer getArguments() {
List<CommandArgument> args = CommandArgument.from(string.substring(label.length()), preserveEmptyLast); return this.args;
return Pair.of(label, args);
}
public static Pair<String, List<CommandArgument>> expand(String string) {
return expand(string, false);
} }
@Override
public void execute() { public void execute() {
try { try {
command.execute(this); command.execute(this);
@ -79,27 +78,8 @@ public class CommandExecution {
} }
} }
@Override
public Stream<String> tabComplete() { public Stream<String> tabComplete() {
return command.tabComplete(this); 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<String, List<CommandArgument>> pair) {
return from(manager, pair.first(), new ArgConsumer(manager, pair.second()));
}
public static CommandExecution from(ICommandManager manager, String string) {
return from(manager, expand(string));
}
} }

View File

@ -21,18 +21,22 @@ import baritone.Baritone;
import baritone.api.IBaritone; import baritone.api.IBaritone;
import baritone.api.utils.command.Command; import baritone.api.utils.command.Command;
import baritone.api.utils.command.argument.CommandArgument; 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.helpers.tabcomplete.TabCompleteHelper;
import baritone.api.utils.command.manager.ICommandManager; import baritone.api.utils.command.manager.ICommandManager;
import baritone.api.utils.command.registry.Registry; import baritone.api.utils.command.registry.Registry;
import baritone.utils.command.defaults.DefaultCommands; 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.List;
import java.util.Locale; import java.util.Locale;
import java.util.stream.Stream; import java.util.stream.Stream;
/** /**
* The default, internal implementation of {@link ICommandManager}
*
* @author Brady * @author Brady
* @since 9/21/2019 * @since 9/21/2019
*/ */
@ -67,13 +71,13 @@ public class CommandManager implements ICommandManager {
} }
@Override @Override
public void execute(CommandExecution execution) { public boolean execute(String string) {
execution.execute(); return this.execute(ICommandExecution.expand(string));
} }
@Override @Override
public boolean execute(String string) { public boolean execute(Tuple<String, List<CommandArgument>> expanded) {
CommandExecution execution = CommandExecution.from(this, string); ICommandExecution execution = this.from(expanded);
if (execution != null) { if (execution != null) {
execution.execute(); execution.execute();
} }
@ -81,21 +85,16 @@ public class CommandManager implements ICommandManager {
} }
@Override @Override
public Stream<String> tabComplete(CommandExecution execution) { public Stream<String> tabComplete(Tuple<String, List<CommandArgument>> expanded) {
return execution.tabComplete(); ICommandExecution execution = this.from(expanded);
} return execution == null ? Stream.empty() : execution.tabComplete();
@Override
public Stream<String> tabComplete(Pair<String, List<CommandArgument>> pair) {
CommandExecution execution = CommandExecution.from(this, pair);
return execution == null ? Stream.empty() : tabComplete(execution);
} }
@Override @Override
public Stream<String> tabComplete(String prefix) { public Stream<String> tabComplete(String prefix) {
Pair<String, List<CommandArgument>> pair = CommandExecution.expand(prefix, true); Tuple<String, List<CommandArgument>> pair = ICommandExecution.expand(prefix, true);
String label = pair.first(); String label = pair.getFirst();
List<CommandArgument> args = pair.second(); List<CommandArgument> args = pair.getSecond();
if (args.isEmpty()) { if (args.isEmpty()) {
return new TabCompleteHelper() return new TabCompleteHelper()
.addCommands(this.baritone.getCommandManager()) .addCommands(this.baritone.getCommandManager())
@ -105,4 +104,12 @@ public class CommandManager implements ICommandManager {
return tabComplete(pair); return tabComplete(pair);
} }
} }
private ICommandExecution 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);
}
} }