From a9b25242388dba80e327b03f6389299584ae4c1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Fri, 4 Sep 2020 21:03:58 +0200 Subject: [PATCH] Add CommandRegistrationHandler --- .../commands/CommandTree.java | 18 ++++-- .../internal/CommandRegistrationHandler.java | 64 +++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java diff --git a/src/main/java/com/intellectualsites/commands/CommandTree.java b/src/main/java/com/intellectualsites/commands/CommandTree.java index 15388639..f0bd1588 100644 --- a/src/main/java/com/intellectualsites/commands/CommandTree.java +++ b/src/main/java/com/intellectualsites/commands/CommandTree.java @@ -28,6 +28,7 @@ import com.google.common.collect.Lists; import com.intellectualsites.commands.components.CommandComponent; import com.intellectualsites.commands.components.StaticComponent; import com.intellectualsites.commands.exceptions.NoSuchCommandException; +import com.intellectualsites.commands.internal.CommandRegistrationHandler; import com.intellectualsites.commands.parser.ComponentParseResult; import com.intellectualsites.commands.sender.CommandSender; @@ -44,19 +45,22 @@ import java.util.stream.Collectors; public class CommandTree { private final Node> 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 * - * @param Command sender type + * @param commandRegistrationHandler Command registration handler + * @param Command sender type * @return New command tree */ @Nonnull - public static CommandTree newTree() { - return new CommandTree<>(); + public static CommandTree newTree(@Nonnull final CommandRegistrationHandler commandRegistrationHandler) { + return new CommandTree<>(commandRegistrationHandler); } public Optional parse(@Nonnull final C commandSender, @Nonnull final String[] args) throws NoSuchCommandException { @@ -170,6 +174,8 @@ public class CommandTree { if (node.getValue() != null) { node.getValue().setOwningCommand(command); } + // Verify the command structure every time we add a new command + this.verifyAndRegister(); } /** @@ -182,7 +188,6 @@ public class CommandTree { if (!(commandComponent instanceof StaticComponent)) { throw new IllegalStateException("Top level command component cannot be a variable"); } - // TODO: Register in the command handler }); this.checkAmbiguity(this.internalTree); // Verify that all leaf nodes have command registered @@ -190,8 +195,11 @@ public class CommandTree { if (leaf.getOwningCommand() == null) { // TODO: Custom exception type 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> node) { diff --git a/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java b/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java new file mode 100644 index 00000000..a2cbb03c --- /dev/null +++ b/src/main/java/com/intellectualsites/commands/internal/CommandRegistrationHandler.java @@ -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; + } + + } + +}