Allow third party command registration

This commit is contained in:
Jesse Boyd 2017-03-07 16:27:41 +11:00
parent fc8fbbb0c4
commit eb55f5fd7d
No known key found for this signature in database
GPG Key ID: 59F1DE6293AF6E1F
2 changed files with 64 additions and 5 deletions

View File

@ -28,7 +28,7 @@ ext {
date = git.head().date.format("yy.MM.dd") date = git.head().date.format("yy.MM.dd")
revision = "-${git.head().abbreviatedId}" revision = "-${git.head().abbreviatedId}"
parents = git.head().parentIds; parents = git.head().parentIds;
index = -85; // Offset to mach CI index = -86; // Offset to mach CI
int major, minor, patch; int major, minor, patch;
major = minor = patch = 0; major = minor = patch = 0;
for (;parents != null && !parents.isEmpty();index++) { for (;parents != null && !parents.isEmpty();index++) {

View File

@ -80,6 +80,7 @@ import com.sk89q.worldedit.util.command.Dispatcher;
import com.sk89q.worldedit.util.command.InvalidUsageException; import com.sk89q.worldedit.util.command.InvalidUsageException;
import com.sk89q.worldedit.util.command.composition.ProvidedValue; import com.sk89q.worldedit.util.command.composition.ProvidedValue;
import com.sk89q.worldedit.util.command.fluent.CommandGraph; import com.sk89q.worldedit.util.command.fluent.CommandGraph;
import com.sk89q.worldedit.util.command.fluent.DispatcherNode;
import com.sk89q.worldedit.util.command.parametric.ExceptionConverter; import com.sk89q.worldedit.util.command.parametric.ExceptionConverter;
import com.sk89q.worldedit.util.command.parametric.LegacyCommandsHandler; import com.sk89q.worldedit.util.command.parametric.LegacyCommandsHandler;
import com.sk89q.worldedit.util.command.parametric.ParametricBuilder; import com.sk89q.worldedit.util.command.parametric.ParametricBuilder;
@ -91,7 +92,9 @@ import com.sk89q.worldedit.util.logging.LogFormat;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.FileHandler; import java.util.logging.FileHandler;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -115,10 +118,13 @@ public final class CommandManager {
private final WorldEdit worldEdit; private final WorldEdit worldEdit;
private final PlatformManager platformManager; private final PlatformManager platformManager;
private final Dispatcher dispatcher; private volatile Dispatcher dispatcher;
private final DynamicStreamHandler dynamicHandler = new DynamicStreamHandler(); private final DynamicStreamHandler dynamicHandler = new DynamicStreamHandler();
private final ExceptionConverter exceptionConverter; private final ExceptionConverter exceptionConverter;
private Map<Object, String[]> methodMap;
private static CommandManager INSTANCE; private static CommandManager INSTANCE;
/** /**
@ -142,7 +148,45 @@ public final class CommandManager {
commandLog.addHandler(dynamicHandler); commandLog.addHandler(dynamicHandler);
dynamicHandler.setFormatter(new LogFormat()); dynamicHandler.setFormatter(new LogFormat());
// Set up the commands manager this.methodMap = new ConcurrentHashMap<>();
TaskManager.IMP.task(new Runnable() {
@Override
public void run() {
setupDispatcher();
}
});
}
/**
* Register all the methods in the class as commands<br>
* - You should try to register commands during startup
* @param clazz The class containing all the commands
*/
public void registerCommands(Object clazz) {
registerCommands(clazz, new String[0]);
if (dispatcher != null) {
setupDispatcher();
}
}
/**
* Create a command with the provided aliases and register all methods of the class as sub commands.<br>
* - You should try to register commands during startup
* @param clazz The class containing all the sub command methods
* @param aliases The aliases to give the command
*/
public void registerCommands(Object clazz, String... aliases) {
methodMap.put(clazz, aliases);
if (dispatcher != null) {
setupDispatcher();
}
}
/**
* Initialize the dispatcher
*/
public void setupDispatcher() {
ParametricBuilder builder = new ParametricBuilder(); ParametricBuilder builder = new ParametricBuilder();
builder.setAuthorizer(new ActorAuthorizer()); builder.setAuthorizer(new ActorAuthorizer());
builder.setDefaultCompleter(new UserCommandCompleter(platformManager)); builder.setDefaultCompleter(new UserCommandCompleter(platformManager));
@ -153,8 +197,21 @@ public final class CommandManager {
builder.addInvokeListener(new LegacyCommandsHandler()); builder.addInvokeListener(new LegacyCommandsHandler());
builder.addInvokeListener(new CommandLoggingHandler(worldEdit, commandLog)); builder.addInvokeListener(new CommandLoggingHandler(worldEdit, commandLog));
dispatcher = new CommandGraph().builder(builder).commands() DispatcherNode graph = new CommandGraph().builder(builder).commands();
.registerMethods(new AnvilCommands(worldEdit)) // Added
for (Map.Entry<Object, String[]> entry : methodMap.entrySet()) {
// add command
String[] aliases = entry.getValue();
if (aliases.length == 0) {
graph = graph.registerMethods(entry.getKey());
} else {
graph = graph.group(aliases).registerMethods(entry.getKey()).parent();
}
}
dispatcher = graph
.registerMethods(new AnvilCommands(worldEdit))
.registerMethods(new BiomeCommands(worldEdit)) .registerMethods(new BiomeCommands(worldEdit))
.registerMethods(new ChunkCommands(worldEdit)) .registerMethods(new ChunkCommands(worldEdit))
.registerMethods(new ClipboardCommands(worldEdit)) .registerMethods(new ClipboardCommands(worldEdit))
@ -188,6 +245,8 @@ public final class CommandManager {
.registerMethods(new ToolCommands(worldEdit)) .registerMethods(new ToolCommands(worldEdit))
.registerMethods(new BrushCommands(worldEdit)) .registerMethods(new BrushCommands(worldEdit))
.parent().graph().getDispatcher(); .parent().graph().getDispatcher();
} }
public static CommandManager getInstance() { public static CommandManager getInstance() {