Added support for empty syntax

This commit is contained in:
themode 2021-03-12 22:10:03 +01:00
parent 7309d05666
commit a47bf24034
3 changed files with 33 additions and 13 deletions

View File

@ -7,7 +7,6 @@ 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 net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
@ -134,9 +133,6 @@ public class Command {
public Collection<CommandSyntax> addSyntax(@Nullable CommandCondition commandCondition,
@NotNull CommandExecutor executor,
@NotNull Argument<?>... args) {
Check.argCondition(args.length == 0,
"The syntax argument cannot be empty, consider using Command#setDefaultExecutor");
// Check optional argument(s)
boolean hasOptional = false;
{

View File

@ -169,14 +169,34 @@ public class CommandDispatcher {
// The default executor should be used if no argument is provided
{
final CommandExecutor defaultExecutor = command.getDefaultExecutor();
if (defaultExecutor != null && !hasArgument) {
parsedCommand.executor = defaultExecutor;
parsedCommand.context = new CommandContext(input);
if (!hasArgument) {
Optional<CommandSyntax> optionalSyntax = command.getSyntaxes()
.stream()
.filter(syntax -> syntax.getArguments().length == 0)
.findFirst();
result.type = CommandResult.Type.SUCCESS;
result.parsedCommand = parsedCommand;
return parsedCommand;
if (optionalSyntax.isPresent()) {
// Empty syntax found
final CommandSyntax syntax = optionalSyntax.get();
parsedCommand.executor = syntax.getExecutor();
parsedCommand.context = new CommandContext(input);
result.type = CommandResult.Type.SUCCESS;
result.parsedCommand = parsedCommand;
return parsedCommand;
} else {
// No empty syntax, use default executor if any
final CommandExecutor defaultExecutor = command.getDefaultExecutor();
if (defaultExecutor != null) {
parsedCommand.executor = defaultExecutor;
parsedCommand.context = new CommandContext(input);
result.type = CommandResult.Type.SUCCESS;
result.parsedCommand = parsedCommand;
return parsedCommand;
}
}
}
}

View File

@ -5,7 +5,7 @@ import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
import static net.minestom.server.command.builder.arguments.ArgumentType.*;
import static net.minestom.server.command.builder.arguments.ArgumentType.Integer;
public class TestCommand extends Command {
@ -13,10 +13,14 @@ public class TestCommand extends Command {
super("testcmd");
setDefaultExecutor(this::usage);
var test1 = Word("msg").setSuggestionCallback((sender, context, suggestion) -> {
var test1 = Integer("msg").setSuggestionCallback((sender, context, suggestion) -> {
suggestion.addEntry(new SuggestionEntry("test"));
});
addSyntax((sender, context) -> {
sender.sendMessage("no argument syntax");
});
addSyntax((sender, context) -> {
System.out.println("executed");
}, test1);