Some basic API/Main separation
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user