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.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<String> tabComplete(CommandExecution execution) {
public final Stream<String> tabComplete(ICommandExecution execution) {
try {
return tabCompleted(execution.label, execution.args);
return this.tabCompleted(execution.getLabel(), execution.getArguments());
} catch (Throwable t) {
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.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)}</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(CommandExecution)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)})</li>
* {@link ICommandManager#tabComplete(String)} or {@link ArgConsumer#tabCompleteDatatype(IDatatype)})</li>
* </ul>
* <p>
* 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.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<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);
}

View File

@ -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<String, List<CommandArgument>> pair = CommandExecution.expand(msg);
String command = pair.first();
String rest = msg.substring(pair.first().length());
ArgConsumer argc = new ArgConsumer(this.manager, pair.second());
Tuple<String, List<CommandArgument>> 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

View File

@ -15,21 +15,23 @@
* 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.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<String, List<CommandArgument>> expand(String string, boolean preserveEmptyLast) {
String label = getLabel(string);
List<CommandArgument> args = CommandArgument.from(string.substring(label.length()), preserveEmptyLast);
return Pair.of(label, args);
}
public static Pair<String, List<CommandArgument>> 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<String> 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<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.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<String, List<CommandArgument>> expanded) {
ICommandExecution execution = this.from(expanded);
if (execution != null) {
execution.execute();
}
@ -81,21 +85,16 @@ public class CommandManager implements ICommandManager {
}
@Override
public Stream<String> tabComplete(CommandExecution execution) {
return 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);
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) {
Pair<String, List<CommandArgument>> pair = CommandExecution.expand(prefix, true);
String label = pair.first();
List<CommandArgument> args = pair.second();
Tuple<String, List<CommandArgument>> pair = ICommandExecution.expand(prefix, true);
String label = pair.getFirst();
List<CommandArgument> 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<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);
}
}