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

de.placeblock.commandapi.core.tree.ParameterTreeCommand Maven / Gradle / Ivy

package de.placeblock.commandapi.core.tree;

import de.placeblock.commandapi.core.Command;
import de.placeblock.commandapi.core.CommandExecutor;
import de.placeblock.commandapi.core.SuggestionBuilder;
import de.placeblock.commandapi.core.exception.CommandParseException;
import de.placeblock.commandapi.core.parameter.Parameter;
import de.placeblock.commandapi.core.parser.ParsedCommandBranch;
import lombok.Getter;
import net.kyori.adventure.text.TextComponent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Author: Placeblock
 */
@Getter
public class ParameterTreeCommand extends TreeCommand {
    private final Parameter parameter;

    public ParameterTreeCommand(Command command, String name, List> children, TextComponent description,
                                String permission, CommandExecutor run, Parameter parameter) {
        super(command, name, children, description, permission, run);
        this.parameter = parameter;
    }

    @Override
    protected void parse(ParsedCommandBranch command, S source) throws CommandParseException {
        command.getBranch().add(this);
        T result = this.parameter.parse(command, source);
        command.addParsedParameter(this, result);
    }

    @Override
    public List getSuggestions(ParsedCommandBranch command, S source) {
        // If there is an error we only want to send an empty arraylist if we are not at the beginning of the parameter:
        // test command |012
        // This fails because it cannot parse an empty string to an integer.
        if (this.hasNoPermission(source)) {
            return new ArrayList<>();
        }
        SuggestionBuilder suggestionBuilder = new SuggestionBuilder<>(this, command.getReader().getRemaining(), source, new HashMap<>(command.getParsedParameters()));
        this.parameter.getSuggestions(suggestionBuilder);
        return suggestionBuilder.getSuggestions();
    }

    @Override
    public TextComponent getHelpComponent() {
        return this.getCommand().getDesign().getHelpParameterTreeCommand(this);
    }

    @Override
    public TextComponent getHelpExtraDescription() {
        return this.getCommand().getDesign().getHelpParameterTreeCommandDescription(this);
    }
}