mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-09 18:08:37 +01:00
Added support for empty syntax
This commit is contained in:
parent
7309d05666
commit
a47bf24034
@ -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;
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user