CommandManager is now Baritone instance dependent
This commit is contained in:
parent
9d2f83d8d6
commit
b88af1d682
@ -26,6 +26,7 @@ import baritone.api.process.*;
|
||||
import baritone.api.selection.ISelectionManager;
|
||||
import baritone.api.utils.IInputOverrideHandler;
|
||||
import baritone.api.utils.IPlayerContext;
|
||||
import baritone.api.utils.command.manager.ICommandManager;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
@ -126,6 +127,12 @@ public interface IBaritone {
|
||||
*/
|
||||
ISelectionManager getSelectionManager();
|
||||
|
||||
/**
|
||||
* @return The {@link ICommandManager} instance
|
||||
* @see ICommandManager
|
||||
*/
|
||||
ICommandManager getCommandManager();
|
||||
|
||||
/**
|
||||
* Open click
|
||||
*/
|
||||
|
@ -33,7 +33,6 @@ import baritone.api.utils.command.exception.CommandNotFoundException;
|
||||
import baritone.api.utils.command.execution.CommandExecution;
|
||||
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
|
||||
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
|
||||
import baritone.api.utils.command.manager.CommandManager;
|
||||
import com.mojang.realmsclient.util.Pair;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
@ -130,9 +129,9 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
|
||||
if (setting != null) {
|
||||
logRanCommand(command, rest);
|
||||
if (setting.getValueClass() == Boolean.class) {
|
||||
CommandManager.execute(String.format("set toggle %s", setting.getName()));
|
||||
this.baritone.getCommandManager().execute(String.format("set toggle %s", setting.getName()));
|
||||
} else {
|
||||
CommandManager.execute(String.format("set %s", setting.getName()));
|
||||
this.baritone.getCommandManager().execute(String.format("set %s", setting.getName()));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -144,18 +143,18 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
|
||||
if (setting.getName().equalsIgnoreCase(pair.first())) {
|
||||
logRanCommand(command, rest);
|
||||
try {
|
||||
CommandManager.execute(String.format("set %s %s", setting.getName(), argc.getString()));
|
||||
this.baritone.getCommandManager().execute(String.format("set %s %s", setting.getName(), argc.getString()));
|
||||
} catch (CommandNotEnoughArgumentsException ignored) {} // The operation is safe
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
CommandExecution execution = CommandExecution.from(pair);
|
||||
CommandExecution execution = CommandExecution.from(this.baritone.getCommandManager(), pair);
|
||||
if (execution == null) {
|
||||
return false;
|
||||
}
|
||||
logRanCommand(command, rest);
|
||||
CommandManager.execute(execution);
|
||||
this.baritone.getCommandManager().execute(execution);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -185,7 +184,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
|
||||
if (argc.hasAtMost(2)) {
|
||||
if (argc.hasExactly(1)) {
|
||||
return new TabCompleteHelper()
|
||||
.addCommands()
|
||||
.addCommands(this.baritone.getCommandManager())
|
||||
.addSettings()
|
||||
.filterPrefix(argc.getString())
|
||||
.stream();
|
||||
@ -205,7 +204,7 @@ public class BaritoneChatControl implements Helper, AbstractGameEventListener {
|
||||
}
|
||||
}
|
||||
}
|
||||
return CommandManager.tabComplete(msg);
|
||||
return this.baritone.getCommandManager().tabComplete(msg);
|
||||
} catch (CommandNotEnoughArgumentsException ignored) { // Shouldn't happen, the operation is safe
|
||||
return Stream.empty();
|
||||
}
|
||||
|
@ -22,7 +22,7 @@ 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.helpers.arguments.ArgConsumer;
|
||||
import baritone.api.utils.command.manager.CommandManager;
|
||||
import baritone.api.utils.command.manager.ICommandManager;
|
||||
import com.mojang.realmsclient.util.Pair;
|
||||
|
||||
import java.util.List;
|
||||
@ -80,8 +80,8 @@ public class CommandExecution {
|
||||
return command.tabComplete(this);
|
||||
}
|
||||
|
||||
public static CommandExecution from(String label, ArgConsumer args) {
|
||||
Command command = CommandManager.getCommand(label);
|
||||
public static CommandExecution from(ICommandManager manager, String label, ArgConsumer args) {
|
||||
Command command = manager.getCommand(label);
|
||||
if (command == null) {
|
||||
return null;
|
||||
}
|
||||
@ -92,11 +92,11 @@ public class CommandExecution {
|
||||
);
|
||||
}
|
||||
|
||||
public static CommandExecution from(Pair<String, List<CommandArgument>> pair) {
|
||||
return from(pair.first(), new ArgConsumer(pair.second()));
|
||||
public static CommandExecution from(ICommandManager manager, Pair<String, List<CommandArgument>> pair) {
|
||||
return from(manager, pair.first(), new ArgConsumer(pair.second()));
|
||||
}
|
||||
|
||||
public static CommandExecution from(String string) {
|
||||
return from(expand(string));
|
||||
public static CommandExecution from(ICommandManager manager, String string) {
|
||||
return from(manager, expand(string));
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ import baritone.api.event.events.TabCompleteEvent;
|
||||
import baritone.api.utils.SettingsUtil;
|
||||
import baritone.api.utils.command.execution.CommandExecution;
|
||||
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
|
||||
import baritone.api.utils.command.manager.CommandManager;
|
||||
import baritone.api.utils.command.manager.ICommandManager;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -46,7 +46,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 CommandManager#tabComplete(CommandExecution)} or {@link ArgConsumer#tabCompleteDatatype(Class)})</li>
|
||||
* {@link ICommandManager#tabComplete(CommandExecution)} or {@link ArgConsumer#tabCompleteDatatype(Class)})</li>
|
||||
* </ul>
|
||||
* <p>
|
||||
* For advanced users: if you're intercepting {@link TabCompleteEvent}s directly, use {@link #build()} instead for an
|
||||
@ -253,15 +253,16 @@ public class TabCompleteHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends every command in the {@link CommandManager#REGISTRY} to this {@link TabCompleteHelper}
|
||||
* Appends every command in the specified {@link ICommandManager} to this {@link TabCompleteHelper}
|
||||
*
|
||||
* @param manager A command manager
|
||||
*
|
||||
* @return This {@link TabCompleteHelper}
|
||||
*/
|
||||
public TabCompleteHelper addCommands() {
|
||||
return append(
|
||||
CommandManager.REGISTRY.descendingStream()
|
||||
.flatMap(command -> command.names.stream())
|
||||
.distinct()
|
||||
public TabCompleteHelper addCommands(ICommandManager manager) {
|
||||
return append(manager.getRegistry().descendingStream()
|
||||
.flatMap(command -> command.names.stream())
|
||||
.distinct()
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,52 @@
|
||||
/*
|
||||
* 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.manager;
|
||||
|
||||
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 java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 9/21/2019
|
||||
*/
|
||||
public interface ICommandManager {
|
||||
|
||||
Registry<Command> getRegistry();
|
||||
|
||||
/**
|
||||
* @param name The command name to search for.
|
||||
* @return The command, if found.
|
||||
*/
|
||||
Command getCommand(String name);
|
||||
|
||||
void execute(CommandExecution execution);
|
||||
|
||||
boolean execute(String string);
|
||||
|
||||
Stream<String> tabComplete(CommandExecution execution);
|
||||
|
||||
Stream<String> tabComplete(Pair<String, List<CommandArgument>> pair);
|
||||
|
||||
Stream<String> tabComplete(String prefix);
|
||||
}
|
@ -29,6 +29,7 @@ import baritone.event.GameEventHandler;
|
||||
import baritone.process.*;
|
||||
import baritone.selection.SelectionManager;
|
||||
import baritone.utils.*;
|
||||
import baritone.utils.command.manager.CommandManager;
|
||||
import baritone.utils.player.PrimaryPlayerContext;
|
||||
import net.minecraft.client.Minecraft;
|
||||
|
||||
@ -79,6 +80,7 @@ public class Baritone implements IBaritone {
|
||||
|
||||
private PathingControlManager pathingControlManager;
|
||||
private SelectionManager selectionManager;
|
||||
private CommandManager commandManager;
|
||||
|
||||
private IPlayerContext playerContext;
|
||||
private WorldProvider worldProvider;
|
||||
@ -114,6 +116,7 @@ public class Baritone implements IBaritone {
|
||||
|
||||
this.worldProvider = new WorldProvider();
|
||||
this.selectionManager = new SelectionManager(this);
|
||||
this.commandManager = new CommandManager(this);
|
||||
|
||||
if (BaritoneAutoTest.ENABLE_AUTO_TEST) {
|
||||
this.gameEventHandler.registerEventListener(BaritoneAutoTest.INSTANCE);
|
||||
@ -205,6 +208,11 @@ public class Baritone implements IBaritone {
|
||||
return this.gameEventHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommandManager getCommandManager() {
|
||||
return this.commandManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void openClick() {
|
||||
new Thread(() -> {
|
||||
|
@ -21,9 +21,7 @@ import baritone.api.IBaritone;
|
||||
import baritone.api.IBaritoneProvider;
|
||||
import baritone.api.cache.IWorldScanner;
|
||||
import baritone.api.utils.command.BaritoneChatControl;
|
||||
import baritone.api.utils.command.manager.CommandManager;
|
||||
import baritone.cache.WorldScanner;
|
||||
import baritone.utils.command.defaults.DefaultCommands;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -38,10 +36,11 @@ public final class BaritoneProvider implements IBaritoneProvider {
|
||||
private final List<IBaritone> all;
|
||||
|
||||
{
|
||||
primary = new Baritone();
|
||||
all = Collections.singletonList(primary);
|
||||
DefaultCommands.commands(primary).forEach(CommandManager.REGISTRY::register);
|
||||
new BaritoneChatControl(primary);
|
||||
this.primary = new Baritone();
|
||||
this.all = Collections.singletonList(this.primary);
|
||||
|
||||
// Setup chat control, just for the primary instance
|
||||
new BaritoneChatControl(this.primary);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,10 +18,8 @@
|
||||
package baritone.utils.command.defaults;
|
||||
|
||||
import baritone.api.IBaritone;
|
||||
import baritone.api.Settings;
|
||||
import baritone.api.utils.command.Command;
|
||||
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
|
||||
import baritone.api.utils.command.manager.CommandManager;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
@ -46,12 +44,12 @@ public class CommandAlias extends Command {
|
||||
|
||||
@Override
|
||||
protected void executed(String label, ArgConsumer args) {
|
||||
CommandManager.execute(String.format("%s %s", target, args.rawRest()));
|
||||
this.baritone.getCommandManager().execute(String.format("%s %s", target, args.rawRest()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Stream<String> tabCompleted(String label, ArgConsumer args) {
|
||||
return CommandManager.tabComplete(String.format("%s %s", target, args.rawRest()));
|
||||
return this.baritone.getCommandManager().tabComplete(String.format("%s %s", target, args.rawRest()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -18,14 +18,12 @@
|
||||
package baritone.utils.command.defaults;
|
||||
|
||||
import baritone.api.IBaritone;
|
||||
import baritone.api.Settings;
|
||||
import baritone.api.utils.command.Command;
|
||||
import baritone.api.utils.command.exception.CommandException;
|
||||
import baritone.api.utils.command.exception.CommandNotFoundException;
|
||||
import baritone.api.utils.command.helpers.arguments.ArgConsumer;
|
||||
import baritone.api.utils.command.helpers.pagination.Paginator;
|
||||
import baritone.api.utils.command.helpers.tabcomplete.TabCompleteHelper;
|
||||
import baritone.api.utils.command.manager.CommandManager;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.util.text.TextFormatting;
|
||||
@ -38,7 +36,6 @@ import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static baritone.api.utils.command.BaritoneChatControl.FORCE_COMMAND_PREFIX;
|
||||
import static baritone.api.utils.command.manager.CommandManager.getCommand;
|
||||
|
||||
public class HelpCommand extends Command {
|
||||
|
||||
@ -52,7 +49,7 @@ public class HelpCommand extends Command {
|
||||
if (!args.hasAny() || args.is(Integer.class)) {
|
||||
Paginator.paginate(
|
||||
args, new Paginator<>(
|
||||
CommandManager.REGISTRY.descendingStream()
|
||||
this.baritone.getCommandManager().getRegistry().descendingStream()
|
||||
.filter(command -> !command.hiddenFromHelp())
|
||||
.collect(Collectors.toList())
|
||||
),
|
||||
@ -82,7 +79,7 @@ public class HelpCommand extends Command {
|
||||
);
|
||||
} else {
|
||||
String commandName = args.getString().toLowerCase();
|
||||
Command command = getCommand(commandName);
|
||||
Command command = this.baritone.getCommandManager().getCommand(commandName);
|
||||
if (command == null) {
|
||||
throw new CommandNotFoundException(commandName);
|
||||
}
|
||||
@ -102,7 +99,10 @@ public class HelpCommand extends Command {
|
||||
@Override
|
||||
protected Stream<String> tabCompleted(String label, ArgConsumer args) throws CommandException {
|
||||
if (args.hasExactlyOne()) {
|
||||
return new TabCompleteHelper().addCommands().filterPrefix(args.getString()).stream();
|
||||
return new TabCompleteHelper()
|
||||
.addCommands(this.baritone.getCommandManager())
|
||||
.filterPrefix(args.getString())
|
||||
.stream();
|
||||
}
|
||||
return Stream.empty();
|
||||
}
|
||||
|
@ -15,31 +15,44 @@
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package baritone.api.utils.command.manager;
|
||||
package baritone.utils.command.manager;
|
||||
|
||||
import baritone.Baritone;
|
||||
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.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 java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.Objects.isNull;
|
||||
/**
|
||||
* @author Brady
|
||||
* @since 9/21/2019
|
||||
*/
|
||||
public class CommandManager implements ICommandManager {
|
||||
|
||||
public class CommandManager {
|
||||
private final Registry<Command> registry = new Registry<>();
|
||||
private final Baritone baritone;
|
||||
|
||||
public static final Registry<Command> REGISTRY = new Registry<>();
|
||||
public CommandManager(Baritone baritone) {
|
||||
this.baritone = baritone;
|
||||
DefaultCommands.commands(baritone).forEach(this.registry::register);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name The command name to search for.
|
||||
* @return The command, if found.
|
||||
*/
|
||||
public static Command getCommand(String name) {
|
||||
for (Command command : REGISTRY.entries) {
|
||||
@Override
|
||||
public Registry<Command> getRegistry() {
|
||||
return this.registry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Command getCommand(String name) {
|
||||
for (Command command : this.registry.entries) {
|
||||
if (command.names.contains(name.toLowerCase(Locale.US))) {
|
||||
return command;
|
||||
}
|
||||
@ -47,34 +60,39 @@ public class CommandManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void execute(CommandExecution execution) {
|
||||
@Override
|
||||
public void execute(CommandExecution execution) {
|
||||
execution.execute();
|
||||
}
|
||||
|
||||
public static boolean execute(String string) {
|
||||
CommandExecution execution = CommandExecution.from(string);
|
||||
@Override
|
||||
public boolean execute(String string) {
|
||||
CommandExecution execution = CommandExecution.from(this, string);
|
||||
if (execution != null) {
|
||||
execution.execute();
|
||||
}
|
||||
return execution != null;
|
||||
}
|
||||
|
||||
public static Stream<String> tabComplete(CommandExecution execution) {
|
||||
@Override
|
||||
public Stream<String> tabComplete(CommandExecution execution) {
|
||||
return execution.tabComplete();
|
||||
}
|
||||
|
||||
public static Stream<String> tabComplete(Pair<String, List<CommandArgument>> pair) {
|
||||
CommandExecution execution = CommandExecution.from(pair);
|
||||
return isNull(execution) ? Stream.empty() : tabComplete(execution);
|
||||
@Override
|
||||
public Stream<String> tabComplete(Pair<String, List<CommandArgument>> pair) {
|
||||
CommandExecution execution = CommandExecution.from(this, pair);
|
||||
return execution == null ? Stream.empty() : tabComplete(execution);
|
||||
}
|
||||
|
||||
public static Stream<String> tabComplete(String prefix) {
|
||||
@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();
|
||||
if (args.isEmpty()) {
|
||||
return new TabCompleteHelper()
|
||||
.addCommands()
|
||||
.addCommands(this.baritone.getCommandManager())
|
||||
.filterPrefix(label)
|
||||
.stream();
|
||||
} else {
|
Loading…
x
Reference in New Issue
Block a user