From b734d172505ff65c3096669d08092305ab7d84d4 Mon Sep 17 00:00:00 2001 From: TheMode Date: Sun, 25 Apr 2021 11:04:11 +0200 Subject: [PATCH] Added Command#getSyntaxesStrings --- .../server/command/builder/Command.java | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/src/main/java/net/minestom/server/command/builder/Command.java b/src/main/java/net/minestom/server/command/builder/Command.java index 5d65bd63a..1f4218389 100644 --- a/src/main/java/net/minestom/server/command/builder/Command.java +++ b/src/main/java/net/minestom/server/command/builder/Command.java @@ -8,13 +8,16 @@ import net.minestom.server.command.builder.arguments.ArgumentDynamicWord; import net.minestom.server.command.builder.arguments.ArgumentType; import net.minestom.server.command.builder.arguments.minecraft.SuggestionType; import net.minestom.server.command.builder.condition.CommandCondition; +import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.*; +import java.util.function.Consumer; import java.util.function.Supplier; +import java.util.stream.Stream; /** * Represents a command which has suggestion/auto-completion. @@ -42,6 +45,7 @@ public class Command { private final String name; private final String[] aliases; + private final String[] names; private CommandExecutor defaultExecutor; private CommandCondition condition; @@ -59,6 +63,7 @@ public class Command { public Command(@NotNull String name, @Nullable String... aliases) { this.name = name; this.aliases = aliases; + this.names = Stream.concat(Arrays.stream(aliases), Stream.of(name)).toArray(String[]::new); this.subcommands = new ArrayList<>(); this.syntaxes = new ArrayList<>(); @@ -223,8 +228,7 @@ public class Command { * * @return the main command's name */ - @NotNull - public String getName() { + public @NotNull String getName() { return name; } @@ -233,11 +237,21 @@ public class Command { * * @return the command aliases, can be null or empty */ - @Nullable - public String[] getAliases() { + public @Nullable String[] getAliases() { return aliases; } + /** + * Gets all the possible names for this command. + *

+ * Include {@link #getName()} and {@link #getAliases()}. + * + * @return this command names + */ + public @NotNull String[] getNames() { + return names; + } + /** * Gets the default {@link CommandExecutor} which is called when there is no argument * or if no corresponding syntax has been found. @@ -266,8 +280,7 @@ public class Command { * @return a collection containing all this command syntaxes * @see #addSyntax(CommandExecutor, Argument[]) */ - @NotNull - public Collection getSyntaxes() { + public @NotNull Collection getSyntaxes() { return syntaxes; } @@ -280,8 +293,7 @@ public class Command { * @param text the whole player's text * @return the array containing all the suggestion for the current arg (split SPACE), can be null */ - @Nullable - public String[] onDynamicWrite(@NotNull CommandSender sender, @NotNull String text) { + public @Nullable String[] onDynamicWrite(@NotNull CommandSender sender, @NotNull String text) { return null; } @@ -300,15 +312,29 @@ public class Command { public void globalListener(@NotNull CommandSender sender, @NotNull CommandContext context, @NotNull String command) { } + @Beta + public @NotNull Set getSyntaxesStrings() { + Set syntaxes = new HashSet<>(); + + Consumer syntaxConsumer = syntaxString -> { + for (String name : getNames()) { + final String syntax = name + StringUtils.SPACE + syntaxString; + syntaxes.add(syntax); + } + }; + + this.subcommands.forEach(subcommand -> subcommand.getSyntaxesStrings().forEach(syntaxConsumer)); + + this.syntaxes.forEach(commandSyntax -> syntaxConsumer.accept(commandSyntax.getSyntaxString())); + + return syntaxes; + } + public static boolean isValidName(@NotNull Command command, @NotNull String name) { - if (command.getName().equals(name)) - return true; - final String[] aliases = command.getAliases(); - if (aliases == null) - return false; - for (String alias : aliases) { - if (alias.equals(name)) + for (String commandName : command.getNames()) { + if (commandName.equals(name)) { return true; + } } return false; }