Create ICommand to go alongside Command default implementation
This commit is contained in:
parent
c908552174
commit
4d05b152f0
@ -18,7 +18,7 @@
|
|||||||
package baritone.api;
|
package baritone.api;
|
||||||
|
|
||||||
import baritone.api.cache.IWorldScanner;
|
import baritone.api.cache.IWorldScanner;
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.ICommand;
|
||||||
import baritone.api.command.ICommandSystem;
|
import baritone.api.command.ICommandSystem;
|
||||||
import net.minecraft.client.entity.EntityPlayerSP;
|
import net.minecraft.client.entity.EntityPlayerSP;
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ public interface IBaritoneProvider {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the {@link ICommandSystem} instance. This is not bound to a specific {@link IBaritone}
|
* Returns the {@link ICommandSystem} instance. This is not bound to a specific {@link IBaritone}
|
||||||
* instance because {@link ICommandSystem} itself controls global behavior for {@link Command}s.
|
* instance because {@link ICommandSystem} itself controls global behavior for {@link ICommand}s.
|
||||||
*
|
*
|
||||||
* @return The {@link ICommandSystem} instance.
|
* @return The {@link ICommandSystem} instance.
|
||||||
*/
|
*/
|
||||||
|
@ -18,10 +18,7 @@
|
|||||||
package baritone.api.command;
|
package baritone.api.command;
|
||||||
|
|
||||||
import baritone.api.IBaritone;
|
import baritone.api.IBaritone;
|
||||||
import baritone.api.utils.Helper;
|
|
||||||
import baritone.api.utils.IPlayerContext;
|
import baritone.api.utils.IPlayerContext;
|
||||||
import baritone.api.command.exception.CommandException;
|
|
||||||
import baritone.api.command.argument.IArgConsumer;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -29,7 +26,19 @@ import java.util.Locale;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public abstract class Command implements Helper {
|
/**
|
||||||
|
* A default implementation of {@link ICommand} which provides easy access to the
|
||||||
|
* command's bound {@link IBaritone} instance, {@link IPlayerContext} and an easy
|
||||||
|
* way to provide multiple valid command execution names through the default constructor.
|
||||||
|
* <p>
|
||||||
|
* So basically, you should use it because it provides a small amount of boilerplate,
|
||||||
|
* but you're not forced to use it.
|
||||||
|
*
|
||||||
|
* @see ICommand
|
||||||
|
*
|
||||||
|
* @author LoganDark
|
||||||
|
*/
|
||||||
|
public abstract class Command implements ICommand {
|
||||||
|
|
||||||
protected IBaritone baritone;
|
protected IBaritone baritone;
|
||||||
protected IPlayerContext ctx;
|
protected IPlayerContext ctx;
|
||||||
@ -52,34 +61,7 @@ public abstract class Command implements Helper {
|
|||||||
this.ctx = baritone.getPlayerContext();
|
this.ctx = baritone.getPlayerContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Called when this command is executed.
|
|
||||||
*/
|
|
||||||
public abstract void execute(String label, IArgConsumer args) throws CommandException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Called when the command needs to tab complete. Return a Stream representing the entries to put in the completions
|
|
||||||
* list.
|
|
||||||
*/
|
|
||||||
public abstract Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return A <b>single-line</b> string containing a short description of this command's purpose.
|
|
||||||
*/
|
|
||||||
public abstract String getShortDesc();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return A list of lines that will be printed by the help command when the user wishes to view them.
|
|
||||||
*/
|
|
||||||
public abstract List<String> getLongDesc();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return {@code true} if this command should be hidden from the help menu
|
|
||||||
*/
|
|
||||||
public boolean hiddenFromHelp() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final List<String> getNames() {
|
public final List<String> getNames() {
|
||||||
return this.names;
|
return this.names;
|
||||||
}
|
}
|
||||||
|
67
src/api/java/baritone/api/command/ICommand.java
Normal file
67
src/api/java/baritone/api/command/ICommand.java
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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.command;
|
||||||
|
|
||||||
|
import baritone.api.command.exception.CommandException;
|
||||||
|
import baritone.api.command.helpers.arguments.IArgConsumer;
|
||||||
|
import baritone.api.utils.Helper;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The base for a command.
|
||||||
|
*
|
||||||
|
* @author Brady
|
||||||
|
* @since 10/7/2019
|
||||||
|
*/
|
||||||
|
public interface ICommand extends Helper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when this command is executed.
|
||||||
|
*/
|
||||||
|
void execute(String label, IArgConsumer args) throws CommandException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the command needs to tab complete. Return a Stream representing the entries to put in the completions
|
||||||
|
* list.
|
||||||
|
*/
|
||||||
|
Stream<String> tabComplete(String label, IArgConsumer args) throws CommandException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return A <b>single-line</b> string containing a short description of this command's purpose.
|
||||||
|
*/
|
||||||
|
String getShortDesc();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return A list of lines that will be printed by the help command when the user wishes to view them.
|
||||||
|
*/
|
||||||
|
List<String> getLongDesc();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return A list of the names that can be accepted to have arguments passed to this command
|
||||||
|
*/
|
||||||
|
List<String> getNames();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {@code true} if this command should be hidden from the help menu
|
||||||
|
*/
|
||||||
|
default boolean hiddenFromHelp() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
package baritone.api.command.argument;
|
package baritone.api.command.argument;
|
||||||
|
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.ICommand;
|
||||||
import baritone.api.command.exception.CommandTooManyArgumentsException;
|
import baritone.api.command.exception.CommandTooManyArgumentsException;
|
||||||
import baritone.api.utils.Helper;
|
import baritone.api.utils.Helper;
|
||||||
import baritone.api.command.argparser.IArgParser;
|
import baritone.api.command.argparser.IArgParser;
|
||||||
@ -34,7 +34,7 @@ import java.util.LinkedList;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link IArgConsumer} is how {@link Command}s read the arguments passed to them. This class has many benefits:
|
* The {@link IArgConsumer} is how {@link ICommand}s read the arguments passed to them. This class has many benefits:
|
||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>Mutability. The whole concept of the {@link IArgConsumer}} is to let you gradually consume arguments in any way
|
* <li>Mutability. The whole concept of the {@link IArgConsumer}} is to let you gradually consume arguments in any way
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
package baritone.api.command.exception;
|
package baritone.api.command.exception;
|
||||||
|
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.ICommand;
|
||||||
import baritone.api.command.argument.ICommandArgument;
|
import baritone.api.command.argument.ICommandArgument;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -34,7 +34,7 @@ public class CommandNotFoundException extends CommandException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(Command command, List<ICommandArgument> args) {
|
public void handle(ICommand command, List<ICommandArgument> args) {
|
||||||
HELPER.logDirect(getMessage());
|
HELPER.logDirect(getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
package baritone.api.command.exception;
|
package baritone.api.command.exception;
|
||||||
|
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.ICommand;
|
||||||
import baritone.api.command.argument.ICommandArgument;
|
import baritone.api.command.argument.ICommandArgument;
|
||||||
import net.minecraft.util.text.TextFormatting;
|
import net.minecraft.util.text.TextFormatting;
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ public class CommandUnhandledException extends RuntimeException implements IComm
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(Command command, List<ICommandArgument> args) {
|
public void handle(ICommand command, List<ICommandArgument> args) {
|
||||||
HELPER.logDirect("An unhandled exception occurred." +
|
HELPER.logDirect("An unhandled exception occurred." +
|
||||||
"The error is in your game's log, please report this at https://github.com/cabaletta/baritone/issues",
|
"The error is in your game's log, please report this at https://github.com/cabaletta/baritone/issues",
|
||||||
TextFormatting.RED);
|
TextFormatting.RED);
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
package baritone.api.command.exception;
|
package baritone.api.command.exception;
|
||||||
|
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.ICommand;
|
||||||
import baritone.api.command.argument.ICommandArgument;
|
import baritone.api.command.argument.ICommandArgument;
|
||||||
import net.minecraft.util.text.TextFormatting;
|
import net.minecraft.util.text.TextFormatting;
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ import static baritone.api.utils.Helper.HELPER;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The base for a Baritone Command Exception, checked or unchecked. Provides a
|
* The base for a Baritone Command Exception, checked or unchecked. Provides a
|
||||||
* {@link #handle(Command, List)} method that is used to provide useful output
|
* {@link #handle(ICommand, List)} method that is used to provide useful output
|
||||||
* to the user for diagnosing issues that may have occurred during execution.
|
* to the user for diagnosing issues that may have occurred during execution.
|
||||||
* <p>
|
* <p>
|
||||||
* Anything implementing this interface should be assignable to {@link Exception}.
|
* Anything implementing this interface should be assignable to {@link Exception}.
|
||||||
@ -49,7 +49,7 @@ public interface ICommandException {
|
|||||||
* @param command The command that threw it.
|
* @param command The command that threw it.
|
||||||
* @param args The arguments the command was called with.
|
* @param args The arguments the command was called with.
|
||||||
*/
|
*/
|
||||||
default void handle(Command command, List<ICommandArgument> args) {
|
default void handle(ICommand command, List<ICommandArgument> args) {
|
||||||
HELPER.logDirect(this.getMessage(), TextFormatting.RED);
|
HELPER.logDirect(this.getMessage(), TextFormatting.RED);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
package baritone.api.command.manager;
|
package baritone.api.command.manager;
|
||||||
|
|
||||||
import baritone.api.IBaritone;
|
import baritone.api.IBaritone;
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.ICommand;
|
||||||
import baritone.api.command.argument.ICommandArgument;
|
import baritone.api.command.argument.ICommandArgument;
|
||||||
import baritone.api.command.registry.Registry;
|
import baritone.api.command.registry.Registry;
|
||||||
import net.minecraft.util.Tuple;
|
import net.minecraft.util.Tuple;
|
||||||
@ -34,13 +34,13 @@ public interface ICommandManager {
|
|||||||
|
|
||||||
IBaritone getBaritone();
|
IBaritone getBaritone();
|
||||||
|
|
||||||
Registry<Command> getRegistry();
|
Registry<ICommand> getRegistry();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param name The command name to search for.
|
* @param name The command name to search for.
|
||||||
* @return The command, if found.
|
* @return The command, if found.
|
||||||
*/
|
*/
|
||||||
Command getCommand(String name);
|
ICommand getCommand(String name);
|
||||||
|
|
||||||
boolean execute(String string);
|
boolean execute(String string);
|
||||||
|
|
||||||
|
@ -27,6 +27,9 @@ import java.util.Arrays;
|
|||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* An ease-of-access interface to provide the {@link Minecraft} game instance,
|
||||||
|
* chat and console logging mechanisms, and the Baritone chat prefix.
|
||||||
|
*
|
||||||
* @author Brady
|
* @author Brady
|
||||||
* @since 8/1/2018
|
* @since 8/1/2018
|
||||||
*/
|
*/
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
package baritone.command.defaults;
|
package baritone.command.defaults;
|
||||||
|
|
||||||
import baritone.api.IBaritone;
|
import baritone.api.IBaritone;
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.ICommand;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@ -26,9 +26,9 @@ public final class DefaultCommands {
|
|||||||
|
|
||||||
private DefaultCommands() {}
|
private DefaultCommands() {}
|
||||||
|
|
||||||
public static List<Command> createAll(IBaritone baritone) {
|
public static List<ICommand> createAll(IBaritone baritone) {
|
||||||
Objects.requireNonNull(baritone);
|
Objects.requireNonNull(baritone);
|
||||||
List<Command> commands = new ArrayList<>(Arrays.asList(
|
List<ICommand> commands = new ArrayList<>(Arrays.asList(
|
||||||
new HelpCommand(baritone),
|
new HelpCommand(baritone),
|
||||||
new SetCommand(baritone),
|
new SetCommand(baritone),
|
||||||
new CommandAlias(baritone, Arrays.asList("modified", "mod", "baritone", "modifiedsettings"), "List modified settings", "set modified"),
|
new CommandAlias(baritone, Arrays.asList("modified", "mod", "baritone", "modifiedsettings"), "List modified settings", "set modified"),
|
||||||
|
@ -19,6 +19,7 @@ package baritone.command.defaults;
|
|||||||
|
|
||||||
import baritone.api.IBaritone;
|
import baritone.api.IBaritone;
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.Command;
|
||||||
|
import baritone.api.command.ICommand;
|
||||||
import baritone.api.command.exception.CommandException;
|
import baritone.api.command.exception.CommandException;
|
||||||
import baritone.api.command.exception.CommandNotFoundException;
|
import baritone.api.command.exception.CommandNotFoundException;
|
||||||
import baritone.api.command.argument.IArgConsumer;
|
import baritone.api.command.argument.IArgConsumer;
|
||||||
@ -79,7 +80,7 @@ public class HelpCommand extends Command {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
String commandName = args.getString().toLowerCase();
|
String commandName = args.getString().toLowerCase();
|
||||||
Command command = this.baritone.getCommandManager().getCommand(commandName);
|
ICommand command = this.baritone.getCommandManager().getCommand(commandName);
|
||||||
if (command == null) {
|
if (command == null) {
|
||||||
throw new CommandNotFoundException(commandName);
|
throw new CommandNotFoundException(commandName);
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ package baritone.command.manager;
|
|||||||
|
|
||||||
import baritone.Baritone;
|
import baritone.Baritone;
|
||||||
import baritone.api.IBaritone;
|
import baritone.api.IBaritone;
|
||||||
import baritone.api.command.Command;
|
import baritone.api.command.ICommand;
|
||||||
import baritone.api.command.argument.ICommandArgument;
|
import baritone.api.command.argument.ICommandArgument;
|
||||||
import baritone.api.command.exception.CommandUnhandledException;
|
import baritone.api.command.exception.CommandUnhandledException;
|
||||||
import baritone.api.command.exception.ICommandException;
|
import baritone.api.command.exception.ICommandException;
|
||||||
@ -43,7 +43,7 @@ import java.util.stream.Stream;
|
|||||||
*/
|
*/
|
||||||
public class CommandManager implements ICommandManager {
|
public class CommandManager implements ICommandManager {
|
||||||
|
|
||||||
private final Registry<Command> registry = new Registry<>();
|
private final Registry<ICommand> registry = new Registry<>();
|
||||||
private final Baritone baritone;
|
private final Baritone baritone;
|
||||||
|
|
||||||
public CommandManager(Baritone baritone) {
|
public CommandManager(Baritone baritone) {
|
||||||
@ -57,13 +57,13 @@ public class CommandManager implements ICommandManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Registry<Command> getRegistry() {
|
public Registry<ICommand> getRegistry() {
|
||||||
return this.registry;
|
return this.registry;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Command getCommand(String name) {
|
public ICommand getCommand(String name) {
|
||||||
for (Command command : this.registry.entries) {
|
for (ICommand command : this.registry.entries) {
|
||||||
if (command.getNames().contains(name.toLowerCase(Locale.US))) {
|
if (command.getNames().contains(name.toLowerCase(Locale.US))) {
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
@ -110,7 +110,7 @@ public class CommandManager implements ICommandManager {
|
|||||||
String label = expanded.getFirst();
|
String label = expanded.getFirst();
|
||||||
ArgConsumer args = new ArgConsumer(this, expanded.getSecond());
|
ArgConsumer args = new ArgConsumer(this, expanded.getSecond());
|
||||||
|
|
||||||
Command command = this.getCommand(label);
|
ICommand command = this.getCommand(label);
|
||||||
return command == null ? null : new ExecutionWrapper(command, label, args);
|
return command == null ? null : new ExecutionWrapper(command, label, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,11 +125,11 @@ public class CommandManager implements ICommandManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static final class ExecutionWrapper {
|
private static final class ExecutionWrapper {
|
||||||
private Command command;
|
private ICommand command;
|
||||||
private String label;
|
private String label;
|
||||||
private ArgConsumer args;
|
private ArgConsumer args;
|
||||||
|
|
||||||
private ExecutionWrapper(Command command, String label, ArgConsumer args) {
|
private ExecutionWrapper(ICommand command, String label, ArgConsumer args) {
|
||||||
this.command = command;
|
this.command = command;
|
||||||
this.label = label;
|
this.label = label;
|
||||||
this.args = args;
|
this.args = args;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user