mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-09 18:08:37 +01:00
Added ArgumentLoop parsing
This commit is contained in:
parent
84da72d798
commit
5610b1a72a
@ -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<T> extends Argument<List<T>> {
|
||||
|
||||
public static final int INVALID_INPUT_ERROR = 1;
|
||||
|
||||
private final List<Argument<T>> arguments = new ArrayList<>();
|
||||
|
||||
@SafeVarargs
|
||||
@ -22,8 +25,37 @@ public class ArgumentLoop<T> extends Argument<List<T>> {
|
||||
@NotNull
|
||||
@Override
|
||||
public List<T> parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
// TODO
|
||||
return null;
|
||||
List<T> 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<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
|
||||
|
@ -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<Arguments> 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) {
|
||||
|
Loading…
Reference in New Issue
Block a user