Add CommandRegistrationHandler

This commit is contained in:
Alexander Söderberg 2020-09-04 21:03:58 +02:00
parent 9ca5675c71
commit a9b2524238
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
2 changed files with 77 additions and 5 deletions

View file

@ -28,6 +28,7 @@ import com.google.common.collect.Lists;
import com.intellectualsites.commands.components.CommandComponent; import com.intellectualsites.commands.components.CommandComponent;
import com.intellectualsites.commands.components.StaticComponent; import com.intellectualsites.commands.components.StaticComponent;
import com.intellectualsites.commands.exceptions.NoSuchCommandException; import com.intellectualsites.commands.exceptions.NoSuchCommandException;
import com.intellectualsites.commands.internal.CommandRegistrationHandler;
import com.intellectualsites.commands.parser.ComponentParseResult; import com.intellectualsites.commands.parser.ComponentParseResult;
import com.intellectualsites.commands.sender.CommandSender; import com.intellectualsites.commands.sender.CommandSender;
@ -44,19 +45,22 @@ import java.util.stream.Collectors;
public class CommandTree<C extends CommandSender> { public class CommandTree<C extends CommandSender> {
private final Node<CommandComponent<?>> internalTree = new Node<>(null); private final Node<CommandComponent<?>> internalTree = new Node<>(null);
private final CommandRegistrationHandler commandRegistrationHandler;
private CommandTree() { private CommandTree(@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
this.commandRegistrationHandler = commandRegistrationHandler;
} }
/** /**
* Create a new command tree instance * Create a new command tree instance
* *
* @param <C> Command sender type * @param commandRegistrationHandler Command registration handler
* @param <C> Command sender type
* @return New command tree * @return New command tree
*/ */
@Nonnull @Nonnull
public static <C extends CommandSender> CommandTree<C> newTree() { public static <C extends CommandSender> CommandTree<C> newTree(@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
return new CommandTree<>(); return new CommandTree<>(commandRegistrationHandler);
} }
public Optional<Command> parse(@Nonnull final C commandSender, @Nonnull final String[] args) throws NoSuchCommandException { public Optional<Command> parse(@Nonnull final C commandSender, @Nonnull final String[] args) throws NoSuchCommandException {
@ -170,6 +174,8 @@ public class CommandTree<C extends CommandSender> {
if (node.getValue() != null) { if (node.getValue() != null) {
node.getValue().setOwningCommand(command); node.getValue().setOwningCommand(command);
} }
// Verify the command structure every time we add a new command
this.verifyAndRegister();
} }
/** /**
@ -182,7 +188,6 @@ public class CommandTree<C extends CommandSender> {
if (!(commandComponent instanceof StaticComponent)) { if (!(commandComponent instanceof StaticComponent)) {
throw new IllegalStateException("Top level command component cannot be a variable"); throw new IllegalStateException("Top level command component cannot be a variable");
} }
// TODO: Register in the command handler
}); });
this.checkAmbiguity(this.internalTree); this.checkAmbiguity(this.internalTree);
// Verify that all leaf nodes have command registered // Verify that all leaf nodes have command registered
@ -190,8 +195,11 @@ public class CommandTree<C extends CommandSender> {
if (leaf.getOwningCommand() == null) { if (leaf.getOwningCommand() == null) {
// TODO: Custom exception type // TODO: Custom exception type
throw new IllegalStateException("Leaf node does not have associated owning command"); throw new IllegalStateException("Leaf node does not have associated owning command");
} else {
this.commandRegistrationHandler.registerCommand(leaf.getOwningCommand());
} }
}); });
/* TODO: Figure out a way to register all combinations along a command component path */
} }
private void checkAmbiguity(@Nonnull final Node<CommandComponent<?>> node) { private void checkAmbiguity(@Nonnull final Node<CommandComponent<?>> node) {

View file

@ -0,0 +1,64 @@
//
// MIT License
//
// Copyright (c) 2020 IntellectualSites
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package com.intellectualsites.commands.internal;
import com.intellectualsites.commands.Command;
import javax.annotation.Nonnull;
/**
* Utility that registers commands natively for whatever
* platform the library is used in. This can do nothing, if
* the target platform does not have its own concept of commands
*/
@FunctionalInterface public interface CommandRegistrationHandler {
/**
* Command registration handler that does nothing
*/
NullCommandRegistrationHandler NULL_COMMAND_REGISTRATION_HANDLER = new NullCommandRegistrationHandler();
/**
* Attempt to register the command
*
* @param command Command to register
* @return {@code true} if the command was registered successfully,
* else {@code false}
*/
boolean registerCommand(@Nonnull final Command command);
final class NullCommandRegistrationHandler implements CommandRegistrationHandler {
private NullCommandRegistrationHandler() {
}
@Override
public boolean registerCommand(@Nonnull final Command command) {
return true;
}
}
}