Added ArgumentLoop parsing

This commit is contained in:
themode 2021-02-12 00:25:12 +01:00
parent 84da72d798
commit 5610b1a72a
2 changed files with 49 additions and 27 deletions

View File

@ -3,6 +3,7 @@ package net.minestom.server.command.builder.arguments;
import net.minestom.server.command.builder.NodeMaker; import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException; import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket; import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
@ -11,6 +12,8 @@ import java.util.List;
public class ArgumentLoop<T> extends Argument<List<T>> { public class ArgumentLoop<T> extends Argument<List<T>> {
public static final int INVALID_INPUT_ERROR = 1;
private final List<Argument<T>> arguments = new ArrayList<>(); private final List<Argument<T>> arguments = new ArrayList<>();
@SafeVarargs @SafeVarargs
@ -22,8 +25,37 @@ public class ArgumentLoop<T> extends Argument<List<T>> {
@NotNull @NotNull
@Override @Override
public List<T> parse(@NotNull String input) throws ArgumentSyntaxException { public List<T> parse(@NotNull String input) throws ArgumentSyntaxException {
// TODO List<T> result = new ArrayList<>();
return null; final String[] split = input.split(StringUtils.SPACE);
final StringBuilder builder = new StringBuilder();
boolean success = false;
for (String s : split) {
builder.append(s);
for (Argument<T> 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 @Override

View File

@ -1,15 +1,14 @@
package demo.commands; 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.CommandSender;
import net.minestom.server.command.builder.Arguments; import net.minestom.server.command.builder.Arguments;
import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.Command;
import net.minestom.server.entity.Player; import net.minestom.server.command.builder.CommandResult;
import net.minestom.server.instance.InstanceContainer;
import net.minestom.server.instance.batch.BlockBatch;
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 { public class TestCommand extends Command {
@ -17,26 +16,17 @@ public class TestCommand extends Command {
super("testcmd"); super("testcmd");
setDefaultExecutor(this::usage); setDefaultExecutor(this::usage);
setDefaultExecutor((sender, args) -> { addSyntax((sender, args) -> {
if (!sender.isPlayer()) { final CommandResult result = args.get("command");
sender.sendMessage("This command may only be run by players."); System.out.println("test " + result.getType() + " " + result.getInput());
return; }, Literal("cmd"), Command("command"));
}
Player player = sender.asPlayer();
BlockBatch batch = new BlockBatch((InstanceContainer) player.getInstance()); addSyntax((sender, args) -> {
List<Arguments> groups = args.get("groups");
int offset = 5; System.out.println("size " + groups.size());
for (int x = 0; x < 50; x += 1) { }, Literal("loop"), Loop("groups",
for (int y = 0; y < 50; y += 1) { Group("group", Literal("name"), Word("word1")),
for (int z = 0; z < 50; z += 1) { Group("group2", Literal("delay"), Integer("number2"))));
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.")));
});
} }
private void usage(CommandSender sender, Arguments arguments) { private void usage(CommandSender sender, Arguments arguments) {