diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLoop.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLoop.java index 0d94bd108..4e470e2c6 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLoop.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentLoop.java @@ -3,6 +3,7 @@ package net.minestom.server.command.builder.arguments; import net.minestom.server.command.builder.NodeMaker; import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; +import org.apache.commons.lang3.StringUtils; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -11,6 +12,8 @@ import java.util.List; public class ArgumentLoop extends Argument> { + public static final int INVALID_INPUT_ERROR = 1; + private final List> arguments = new ArrayList<>(); @SafeVarargs @@ -22,8 +25,37 @@ public class ArgumentLoop extends Argument> { @NotNull @Override public List parse(@NotNull String input) throws ArgumentSyntaxException { - // TODO - return null; + List result = new ArrayList<>(); + final String[] split = input.split(StringUtils.SPACE); + + final StringBuilder builder = new StringBuilder(); + boolean success = false; + for (String s : split) { + builder.append(s); + + for (Argument argument : arguments) { + try { + final String inputString = builder.toString(); + final T value = argument.parse(inputString); + success = true; + result.add(value); + break; + } catch (ArgumentSyntaxException ignored) { + success = false; + } + } + if (success) { + builder.setLength(0); // Clear + } else { + builder.append(StringUtils.SPACE); + } + } + + if (result.isEmpty() || !success) { + throw new ArgumentSyntaxException("Invalid loop, there is no valid argument found", input, INVALID_INPUT_ERROR); + } + + return result; } @Override diff --git a/src/test/java/demo/commands/TestCommand.java b/src/test/java/demo/commands/TestCommand.java index 21380d928..3ec28f1e2 100644 --- a/src/test/java/demo/commands/TestCommand.java +++ b/src/test/java/demo/commands/TestCommand.java @@ -1,15 +1,14 @@ package demo.commands; -import net.minestom.server.chat.ChatColor; -import net.minestom.server.chat.ColoredText; import net.minestom.server.command.CommandSender; import net.minestom.server.command.builder.Arguments; import net.minestom.server.command.builder.Command; -import net.minestom.server.entity.Player; -import net.minestom.server.instance.InstanceContainer; -import net.minestom.server.instance.batch.BlockBatch; +import net.minestom.server.command.builder.CommandResult; -import java.util.concurrent.ThreadLocalRandom; +import java.util.List; + +import static net.minestom.server.command.builder.arguments.ArgumentType.Integer; +import static net.minestom.server.command.builder.arguments.ArgumentType.*; public class TestCommand extends Command { @@ -17,26 +16,17 @@ public class TestCommand extends Command { super("testcmd"); setDefaultExecutor(this::usage); - setDefaultExecutor((sender, args) -> { - if (!sender.isPlayer()) { - sender.sendMessage("This command may only be run by players."); - return; - } - Player player = sender.asPlayer(); + addSyntax((sender, args) -> { + final CommandResult result = args.get("command"); + System.out.println("test " + result.getType() + " " + result.getInput()); + }, Literal("cmd"), Command("command")); - BlockBatch batch = new BlockBatch((InstanceContainer) player.getInstance()); - - int offset = 5; - for (int x = 0; x < 50; x += 1) { - for (int y = 0; y < 50; y += 1) { - for (int z = 0; z < 50; z += 1) { - batch.setBlockStateId(x + offset, y + offset+50, z + offset, (short) ThreadLocalRandom.current().nextInt(500)); - } - } - } - - batch.flush(() -> sender.sendMessage(ColoredText.of(ChatColor.BRIGHT_GREEN, "Created cube."))); - }); + addSyntax((sender, args) -> { + List groups = args.get("groups"); + System.out.println("size " + groups.size()); + }, Literal("loop"), Loop("groups", + Group("group", Literal("name"), Word("word1")), + Group("group2", Literal("delay"), Integer("number2")))); } private void usage(CommandSender sender, Arguments arguments) {