diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/Command.java b/cloud-core/src/main/java/com/intellectualsites/commands/Command.java index 490a1a53..dec4049d 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/Command.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/Command.java @@ -126,14 +126,16 @@ public class Command { * * @param commandName Base command component * @param commandMeta Command meta instance + * @param aliases Command aliases * @param Command sender type * @param Command meta type * @return Command builder */ @Nonnull public static Builder newBuilder(@Nonnull final String commandName, - @Nonnull final M commandMeta) { - return new Builder<>(commandMeta, null, Collections.singletonList(StaticComponent.required(commandName)), + @Nonnull final M commandMeta, + @Nonnull final String... aliases) { + return new Builder<>(commandMeta, null, Collections.singletonList(StaticComponent.required(commandName, aliases)), new CommandExecutionHandler.NullCommandExecutionHandler<>(), ""); } diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java b/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java index ba23c61b..4b21a1d8 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/CommandManager.java @@ -35,9 +35,9 @@ import com.intellectualsites.commands.execution.CommandExecutionCoordinator; import com.intellectualsites.commands.execution.CommandResult; import com.intellectualsites.commands.execution.CommandSuggestionProcessor; import com.intellectualsites.commands.execution.FilteringCommandSuggestionProcessor; +import com.intellectualsites.commands.execution.preprocessor.AcceptingCommandPreprocessor; import com.intellectualsites.commands.execution.preprocessor.CommandPreprocessingContext; import com.intellectualsites.commands.execution.preprocessor.CommandPreprocessor; -import com.intellectualsites.commands.execution.preprocessor.AcceptingCommandPreprocessor; import com.intellectualsites.commands.internal.CommandRegistrationHandler; import com.intellectualsites.commands.meta.CommandMeta; import com.intellectualsites.commands.sender.CommandSender; @@ -48,6 +48,7 @@ import javax.annotation.Nonnull; import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.Set; import java.util.StringTokenizer; import java.util.concurrent.CompletableFuture; import java.util.function.Function; @@ -186,12 +187,28 @@ public abstract class CommandManager commandBuilder(@Nonnull final String name, @Nonnull final M meta) { + public Command.Builder commandBuilder(@Nonnull final String name, + @Nonnull final Set aliases, + @Nonnull final M meta) { + return Command.newBuilder(name, meta, aliases.toArray(new String[0])); + } + + /** + * Create a new command builder + * + * @param name Command name + * @param meta Command meta + * @return Builder instance + */ + @Nonnull + public Command.Builder commandBuilder(@Nonnull final String name, + @Nonnull final M meta) { return Command.newBuilder(name, meta); } @@ -208,7 +225,6 @@ public abstract class CommandManager { * * @param command Command to insert */ + @SuppressWarnings("unchecked") public void insertCommand(@Nonnull final Command command) { synchronized (this.commandLock) { Node> node = this.internalTree; @@ -343,6 +344,10 @@ public final class CommandTree { Node> tempNode = node.getChild(component); if (tempNode == null) { tempNode = node.addChild(component); + } else if (component instanceof StaticComponent && tempNode.getValue() != null) { + for (final String alias : ((StaticComponent) component).getAliases()) { + ((StaticComponent) tempNode.getValue()).registerAlias(alias); + } } if (node.children.size() > 0) { node.children.sort(Comparator.comparing(Node::getValue)); diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/components/StaticComponent.java b/cloud-core/src/main/java/com/intellectualsites/commands/components/StaticComponent.java index f965fcc6..d0a73bbe 100644 --- a/cloud-core/src/main/java/com/intellectualsites/commands/components/StaticComponent.java +++ b/cloud-core/src/main/java/com/intellectualsites/commands/components/StaticComponent.java @@ -75,6 +75,25 @@ public final class StaticComponent extends CommandCompo return new StaticComponent<>(false, name, aliases); } + /** + * Register a new alias + * + * @param alias New alias + */ + public void registerAlias(@Nonnull final String alias) { + ((StaticComponentParser) this.getParser()).acceptedStrings.add(alias); + } + + /** + * Get an immutable view of the aliases + * + * @return Immutable view of the component aliases + */ + @Nonnull + public Set getAliases() { + return Collections.unmodifiableSet(((StaticComponentParser) this.getParser()).getAcceptedStrings()); + } + private static final class StaticComponentParser implements ComponentParser { @@ -111,6 +130,15 @@ public final class StaticComponent extends CommandCompo return Collections.singletonList(this.name); } + /** + * Get the accepted strings + * + * @return Accepted strings + */ + @Nonnull + public Set getAcceptedStrings() { + return this.acceptedStrings; + } } } diff --git a/cloud-core/src/test/java/com/intellectualsites/commands/CommandTreeTest.java b/cloud-core/src/test/java/com/intellectualsites/commands/CommandTreeTest.java index c0be1957..3bf68881 100644 --- a/cloud-core/src/test/java/com/intellectualsites/commands/CommandTreeTest.java +++ b/cloud-core/src/test/java/com/intellectualsites/commands/CommandTreeTest.java @@ -50,8 +50,9 @@ class CommandTreeTest { .withComponent(StaticComponent.required("one")).build()) .registerCommand(commandManager.commandBuilder("test", SimpleCommandMeta.empty()) .withComponent(StaticComponent.required("two")).withPermission("no").build()) - .registerCommand(commandManager.commandBuilder("test", SimpleCommandMeta.empty()) - .withComponent(StaticComponent.required("opt")) + .registerCommand(commandManager.commandBuilder("test", Collections.singleton("other"), + SimpleCommandMeta.empty()) + .withComponent(StaticComponent.required("opt", "öpt")) .withComponent(IntegerComponent .optional("num", EXPECTED_INPUT_NUMBER)) .build()); @@ -79,6 +80,13 @@ class CommandTreeTest { .ifPresent(c -> c.getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()))); } + @Test + void testAlias() { + commandManager.getCommandTree() + .parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("other", "öpt", "12"))) + .ifPresent(c -> c.getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()))); + } + @Test void getSuggestions() { Assertions.assertFalse(