Use literal as the default argument when generating a syntax

This commit is contained in:
TheMode 2021-04-16 21:51:33 +02:00
parent 502ec22b92
commit 8f3ee93191
2 changed files with 18 additions and 9 deletions

View File

@ -82,10 +82,14 @@ public class ArgumentParser {
// No state // No state
if (state == 0) { if (state == 0) {
if (c == ' ') if (c == ' ') {
continue; // Use literal as the default argument
final String argument = builder.toString();
if (c == '<') { if (argument.length() != 0) {
result.add(new ArgumentLiteral(argument));
builder = new StringBuilder();
}
} else if (c == '<') {
// Retrieve argument type // Retrieve argument type
final String argument = builder.toString(); final String argument = builder.toString();
argumentFunction = ARGUMENT_FUNCTION_MAP.get(argument); argumentFunction = ARGUMENT_FUNCTION_MAP.get(argument);
@ -122,6 +126,14 @@ public class ArgumentParser {
} }
// Use remaining as literal if present
if (state == 0) {
final String argument = builder.toString();
if (argument.length() != 0) {
result.add(new ArgumentLiteral(argument));
}
}
return result.toArray(Argument[]::new); return result.toArray(Argument[]::new);
} }

View File

@ -4,8 +4,7 @@ import net.kyori.adventure.text.Component;
import net.minestom.server.command.CommandSender; import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.CommandContext; import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.command.builder.arguments.ArgumentType;
import static net.minestom.server.command.builder.arguments.ArgumentType.ResourceLocation;
public class TestCommand extends Command { public class TestCommand extends Command {
@ -13,9 +12,7 @@ public class TestCommand extends Command {
super("testcmd"); super("testcmd");
setDefaultExecutor(this::usage); setDefaultExecutor(this::usage);
var test = ResourceLocation("msg"); addSyntax((sender, context) -> System.out.println("executed"), ArgumentType.generate("test get"));
addSyntax((sender, context) -> System.out.println("executed"),test);
} }
private void usage(CommandSender sender, CommandContext context) { private void usage(CommandSender sender, CommandContext context) {