All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.command.builder.arguments.ArgumentLoop Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
package net.minestom.server.command.builder.arguments;

import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArgumentLoop extends Argument> {

    public static final int INVALID_INPUT_ERROR = 1;

    private final List> arguments = new ArrayList<>();

    @SafeVarargs
    public ArgumentLoop(@NotNull String id, @NotNull Argument... arguments) {
        super(id, true, true);
        this.arguments.addAll(Arrays.asList(arguments));
    }

    @NotNull
    @Override
    public List parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
        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(sender, 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;
    }

    public List> arguments() {
        return arguments;
    }

    @Override
    public String parser() {
        return null;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy