Search in sources :

Example 1 with ValueParser

use of org.spongepowered.api.command.parameter.managed.ValueParser in project SpongeCommon by SpongePowered.

the class SpongeParameterValueBuilder method build.

@Override
@NonNull
public SpongeParameterValue<T> build() throws IllegalStateException {
    Preconditions.checkState(this.key != null, "The command key may not be null");
    Preconditions.checkState(!this.parsers.isEmpty(), "There must be parsers");
    final ImmutableList.Builder<ValueParser<? extends T>> parsersBuilder = ImmutableList.builder();
    parsersBuilder.addAll(this.parsers);
    final ValueCompleter completer;
    if (this.completer != null) {
        completer = this.completer;
    } else {
        final ImmutableList.Builder<ValueCompleter> completersBuilder = ImmutableList.builder();
        for (final ValueParser<? extends T> parser : this.parsers) {
            if (parser instanceof ValueCompleter) {
                completersBuilder.add((ValueCompleter) parser);
            }
        }
        final ImmutableList<ValueCompleter> completers = completersBuilder.build();
        if (completers.isEmpty()) {
            completer = SpongeParameterValueBuilder.EMPTY_COMPLETER;
        } else if (completers.size() == 1) {
            completer = completers.get(0);
        } else {
            completer = (context, currentInput) -> {
                final ImmutableList.Builder<CommandCompletion> builder = ImmutableList.builder();
                for (final ValueCompleter valueCompleter : completers) {
                    builder.addAll(valueCompleter.complete(context, currentInput));
                }
                return builder.build();
            };
        }
    }
    return new SpongeParameterValue<>(parsersBuilder.build(), completer, this.usage, this.executionRequirements == null ? commandCause -> true : this.executionRequirements, this.key, this.isOptional, this.consumesAll, this.terminal, this.modifier);
}
Also used : NonNull(org.checkerframework.checker.nullness.qual.NonNull) CommandCompletion(org.spongepowered.api.command.CommandCompletion) Predicate(java.util.function.Predicate) ValueParameterModifier(org.spongepowered.api.command.parameter.managed.ValueParameterModifier) ArrayList(java.util.ArrayList) ValueParser(org.spongepowered.api.command.parameter.managed.ValueParser) Objects(java.util.Objects) CommandCause(org.spongepowered.api.command.CommandCause) ValueCompleter(org.spongepowered.api.command.parameter.managed.ValueCompleter) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Parameter(org.spongepowered.api.command.parameter.Parameter) Type(java.lang.reflect.Type) ValueUsage(org.spongepowered.api.command.parameter.managed.ValueUsage) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ValueCompleter(org.spongepowered.api.command.parameter.managed.ValueCompleter) ValueParser(org.spongepowered.api.command.parameter.managed.ValueParser) ImmutableList(com.google.common.collect.ImmutableList) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Objects (java.util.Objects)1 Predicate (java.util.function.Predicate)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 CommandCause (org.spongepowered.api.command.CommandCause)1 CommandCompletion (org.spongepowered.api.command.CommandCompletion)1 Parameter (org.spongepowered.api.command.parameter.Parameter)1 ValueCompleter (org.spongepowered.api.command.parameter.managed.ValueCompleter)1 ValueParameterModifier (org.spongepowered.api.command.parameter.managed.ValueParameterModifier)1 ValueParser (org.spongepowered.api.command.parameter.managed.ValueParser)1 ValueUsage (org.spongepowered.api.command.parameter.managed.ValueUsage)1