Skip to content
Permalink
Browse files

Change how components register their commands

  • Loading branch information
DarkArc committed Jan 2, 2020
1 parent a264217 commit 9a4130013ea51ef55da8b9b73eb8f210c63ba6cf
@@ -24,7 +24,6 @@
import com.sk89q.commandbook.config.LegacyCommandBookConfigurationMigrator;
import com.sk89q.util.yaml.YAMLFormat;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.internal.command.CommandUtil;
import com.zachsthings.libcomponents.InjectComponent;
import com.zachsthings.libcomponents.InjectComponentAnnotationHandler;
@@ -40,13 +39,11 @@
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Listener;
import org.enginehub.piston.CommandManager;
import org.yaml.snakeyaml.error.YAMLException;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;

@@ -85,8 +82,8 @@ public static void registerEvents(Listener listener) {
server().getPluginManager().registerEvents(listener, inst());
}

public static void registerComponentCommands(BiConsumer<CommandManager, CommandRegistrationHandler> op) {
inst().commandManager.registerComponentCommands(op);
public static ComponentCommandRegistrar getComponentRegistrar() {
return inst().commandManager.getComponentRegistrar();
}

private void publishPistonCommands() {
@@ -0,0 +1,50 @@
package com.sk89q.commandbook;

import com.sk89q.worldedit.internal.command.CommandRegistrationHandler;
import com.sk89q.worldedit.util.formatting.text.TextComponent;
import com.sk89q.worldedit.util.formatting.text.TranslatableComponent;
import org.enginehub.piston.CommandManager;
import org.enginehub.piston.CommandManagerService;
import org.enginehub.piston.converter.ArgumentConverter;
import org.enginehub.piston.inject.Key;
import org.enginehub.piston.part.SubCommandPart;

import java.util.function.BiConsumer;
import java.util.stream.Collectors;

public class ComponentCommandRegistrar {
private CommandManagerService service;
private CommandManager topLevelCommandManager;
private CommandRegistrationHandler registration;

public ComponentCommandRegistrar(CommandManagerService service, CommandManager topLevelCommandManager, CommandRegistrationHandler registration) {
this.service = service;
this.topLevelCommandManager = topLevelCommandManager;
this.registration = registration;
}

public void registerAsSubCommand(String command, String description, CommandManager parentManager, BiConsumer<CommandManager, CommandRegistrationHandler> op) {
parentManager.register(command, builder -> {
builder.description(TextComponent.of(description));

CommandManager manager = service.newCommandManager();
op.accept(manager, registration);

builder.addPart(SubCommandPart.builder(TranslatableComponent.of("worldedit.argument.action"), TextComponent.of("Sub-command to run."))
.withCommands(manager.getAllCommands().collect(Collectors.toList()))
.required()
.build());
});
}

public void registerTopLevelCommands(BiConsumer<CommandManager, CommandRegistrationHandler> op) {
CommandManager componentManager = service.newCommandManager();
op.accept(componentManager, registration);
topLevelCommandManager.registerManager(componentManager);
}

@Deprecated
public <T> void registerConverter(Key<T> key, ArgumentConverter<T> converter) {
topLevelCommandManager.registerConverter(key, converter);
}
}
@@ -36,7 +36,6 @@

import java.util.Collections;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -56,6 +55,7 @@
private final InjectedValueStore globalInjectedValues;
private final DynamicStreamHandler dynamicHandler = new DynamicStreamHandler();
private final CommandRegistrationHandler registration;
private final ComponentCommandRegistrar componentRegistrar;

protected PlatformCommandManager(final CommandBook commandBook) {
checkNotNull(commandBook);
@@ -65,6 +65,7 @@ protected PlatformCommandManager(final CommandBook commandBook) {
this.commandManager = commandManagerService.newCommandManager();
this.globalInjectedValues = MapBackedValueStore.create();
this.registration = new CommandRegistrationHandler(ImmutableList.of());
this.componentRegistrar = new ComponentCommandRegistrar(commandManagerService, commandManager, registration);

// setup separate from main constructor
// ensures that everything is definitely assigned
@@ -91,10 +92,8 @@ private void registerCoreCommands() {
CommandBookCommands.register(commandManagerService, commandManager, registration);
}

public void registerComponentCommands(BiConsumer<CommandManager, CommandRegistrationHandler> op) {
CommandManager componentManager = commandManagerService.newCommandManager();
op.accept(componentManager, registration);
commandManager.registerManager(componentManager);
public ComponentCommandRegistrar getComponentRegistrar() {
return componentRegistrar;
}

public void registerCommandsWith(CommandBook commandBook) {
@@ -51,7 +51,7 @@
public void enable() {
config = configure(new LocalConfiguration());

CommandBook.registerComponentCommands((commandManager, registration) -> {
CommandBook.getComponentRegistrar().registerTopLevelCommands((commandManager, registration) -> {
registration.register(commandManager, GodCommandsRegistration.builder(), new GodCommands(this));
});

0 comments on commit 9a41300

Please sign in to comment.
You can’t perform that action at this time.