Added Command#getSyntaxesStrings

This commit is contained in:
TheMode 2021-04-25 11:04:11 +02:00
parent 41d61e7df6
commit b734d17250

View File

@ -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.ArgumentType;
import net.minestom.server.command.builder.arguments.minecraft.SuggestionType; import net.minestom.server.command.builder.arguments.minecraft.SuggestionType;
import net.minestom.server.command.builder.condition.CommandCondition; import net.minestom.server.command.builder.condition.CommandCondition;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.*; import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.stream.Stream;
/** /**
* Represents a command which has suggestion/auto-completion. * Represents a command which has suggestion/auto-completion.
@ -42,6 +45,7 @@ public class Command {
private final String name; private final String name;
private final String[] aliases; private final String[] aliases;
private final String[] names;
private CommandExecutor defaultExecutor; private CommandExecutor defaultExecutor;
private CommandCondition condition; private CommandCondition condition;
@ -59,6 +63,7 @@ public class Command {
public Command(@NotNull String name, @Nullable String... aliases) { public Command(@NotNull String name, @Nullable String... aliases) {
this.name = name; this.name = name;
this.aliases = aliases; this.aliases = aliases;
this.names = Stream.concat(Arrays.stream(aliases), Stream.of(name)).toArray(String[]::new);
this.subcommands = new ArrayList<>(); this.subcommands = new ArrayList<>();
this.syntaxes = new ArrayList<>(); this.syntaxes = new ArrayList<>();
@ -223,8 +228,7 @@ public class Command {
* *
* @return the main command's name * @return the main command's name
*/ */
@NotNull public @NotNull String getName() {
public String getName() {
return name; return name;
} }
@ -233,11 +237,21 @@ public class Command {
* *
* @return the command aliases, can be null or empty * @return the command aliases, can be null or empty
*/ */
@Nullable public @Nullable String[] getAliases() {
public String[] getAliases() {
return aliases; return aliases;
} }
/**
* Gets all the possible names for this command.
* <p>
* 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 * Gets the default {@link CommandExecutor} which is called when there is no argument
* or if no corresponding syntax has been found. * or if no corresponding syntax has been found.
@ -266,8 +280,7 @@ public class Command {
* @return a collection containing all this command syntaxes * @return a collection containing all this command syntaxes
* @see #addSyntax(CommandExecutor, Argument[]) * @see #addSyntax(CommandExecutor, Argument[])
*/ */
@NotNull public @NotNull Collection<CommandSyntax> getSyntaxes() {
public Collection<CommandSyntax> getSyntaxes() {
return syntaxes; return syntaxes;
} }
@ -280,8 +293,7 @@ public class Command {
* @param text the whole player's text * @param text the whole player's text
* @return the array containing all the suggestion for the current arg (split SPACE), can be null * @return the array containing all the suggestion for the current arg (split SPACE), can be null
*/ */
@Nullable public @Nullable String[] onDynamicWrite(@NotNull CommandSender sender, @NotNull String text) {
public String[] onDynamicWrite(@NotNull CommandSender sender, @NotNull String text) {
return null; return null;
} }
@ -300,15 +312,29 @@ public class Command {
public void globalListener(@NotNull CommandSender sender, @NotNull CommandContext context, @NotNull String command) { public void globalListener(@NotNull CommandSender sender, @NotNull CommandContext context, @NotNull String command) {
} }
@Beta
public @NotNull Set<String> getSyntaxesStrings() {
Set<String> syntaxes = new HashSet<>();
Consumer<String> 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) { public static boolean isValidName(@NotNull Command command, @NotNull String name) {
if (command.getName().equals(name)) for (String commandName : command.getNames()) {
return true; if (commandName.equals(name)) {
final String[] aliases = command.getAliases();
if (aliases == null)
return false;
for (String alias : aliases) {
if (alias.equals(name))
return true; return true;
}
} }
return false; return false;
} }