From 8f3ee93191e4eede49f581b1d44d9444b285d772 Mon Sep 17 00:00:00 2001 From: TheMode Date: Fri, 16 Apr 2021 21:51:33 +0200 Subject: [PATCH] Use literal as the default argument when generating a syntax --- .../builder/parser/ArgumentParser.java | 20 +++++++++++++++---- src/test/java/demo/commands/TestCommand.java | 7 ++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minestom/server/command/builder/parser/ArgumentParser.java b/src/main/java/net/minestom/server/command/builder/parser/ArgumentParser.java index 9b3f0debe..36241924d 100644 --- a/src/main/java/net/minestom/server/command/builder/parser/ArgumentParser.java +++ b/src/main/java/net/minestom/server/command/builder/parser/ArgumentParser.java @@ -82,10 +82,14 @@ public class ArgumentParser { // No state if (state == 0) { - if (c == ' ') - continue; - - if (c == '<') { + if (c == ' ') { + // Use literal as the default argument + final String argument = builder.toString(); + if (argument.length() != 0) { + result.add(new ArgumentLiteral(argument)); + builder = new StringBuilder(); + } + } else if (c == '<') { // Retrieve argument type final String argument = builder.toString(); 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); } diff --git a/src/test/java/demo/commands/TestCommand.java b/src/test/java/demo/commands/TestCommand.java index 767394682..8df9f9ea5 100644 --- a/src/test/java/demo/commands/TestCommand.java +++ b/src/test/java/demo/commands/TestCommand.java @@ -4,8 +4,7 @@ import net.kyori.adventure.text.Component; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandContext; - -import static net.minestom.server.command.builder.arguments.ArgumentType.ResourceLocation; +import net.minestom.server.command.builder.arguments.ArgumentType; public class TestCommand extends Command { @@ -13,9 +12,7 @@ public class TestCommand extends Command { super("testcmd"); setDefaultExecutor(this::usage); - var test = ResourceLocation("msg"); - - addSyntax((sender, context) -> System.out.println("executed"),test); + addSyntax((sender, context) -> System.out.println("executed"), ArgumentType.generate("test get")); } private void usage(CommandSender sender, CommandContext context) {