CommandManager is now Baritone instance dependent

This commit is contained in:
Brady
2019-09-21 02:40:03 -05:00
parent 9d2f83d8d6
commit b88af1d682
10 changed files with 140 additions and 58 deletions

View File

@@ -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(() -> {

View File

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

View File

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

View File

@@ -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();
}

View File

@@ -0,0 +1,102 @@
/*
* 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.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;
/**
* @author Brady
* @since 9/21/2019
*/
public class CommandManager implements ICommandManager {
private final Registry<Command> registry = new Registry<>();
private final Baritone baritone;
public CommandManager(Baritone baritone) {
this.baritone = baritone;
DefaultCommands.commands(baritone).forEach(this.registry::register);
}
@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;
}
}
return null;
}
@Override
public void execute(CommandExecution execution) {
execution.execute();
}
@Override
public boolean execute(String string) {
CommandExecution execution = CommandExecution.from(this, string);
if (execution != null) {
execution.execute();
}
return execution != null;
}
@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);
}
@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(this.baritone.getCommandManager())
.filterPrefix(label)
.stream();
} else {
return tabComplete(pair);
}
}
}