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

cloud.commandframework.brigadier.BrigadierMapping Maven / Gradle / Ivy

//
// MIT License
//
// Copyright (c) 2022 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.brigadier;

import cloud.commandframework.arguments.parser.ArgumentParser;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import static java.util.Objects.requireNonNull;

final class BrigadierMapping, S> {

    private final boolean cloudSuggestions;
    private final BrigadierMappingBuilder.@Nullable SuggestionProviderSupplier suggestionsOverride;
    private final @Nullable Function> mapper;

    BrigadierMapping(
            final boolean cloudSuggestions,
            final BrigadierMappingBuilder.@Nullable SuggestionProviderSupplier suggestionsOverride,
            final @Nullable Function> mapper
    ) {
        this.cloudSuggestions = cloudSuggestions;
        this.suggestionsOverride = suggestionsOverride;
        this.mapper = mapper;
    }

    public @Nullable Function> getMapper() {
        return this.mapper;
    }

    public @NonNull BrigadierMapping withNativeSuggestions(final boolean nativeSuggestions) {
        if (nativeSuggestions && this.cloudSuggestions) {
            return new BrigadierMapping<>(false, this.suggestionsOverride, this.mapper);
        } else if (!nativeSuggestions && !this.cloudSuggestions) {
            return new BrigadierMapping<>(true, this.suggestionsOverride, this.mapper);
        }
        return this;
    }

    @SuppressWarnings("unchecked")
    public @Nullable SuggestionProvider makeSuggestionProvider(final K commandArgument) {
        if (this.cloudSuggestions) {
            return CloudBrigadierManager.delegateSuggestions();
        }
        return this.suggestionsOverride == null
                ? null
                : (SuggestionProvider) this.suggestionsOverride.provide(
                        commandArgument,
                        CloudBrigadierManager.delegateSuggestions()
                );
    }


    static final class BuilderImpl, S> implements BrigadierMappingBuilder {

        private Function> mapper;
        private boolean cloudSuggestions = false;
        private SuggestionProviderSupplier suggestionsOverride;

        @Override
        public BrigadierMappingBuilder toConstant(final ArgumentType constant) {
            return this.to(parser -> constant);
        }

        @Override
        public BrigadierMappingBuilder to(final Function> mapper) {
            this.mapper = mapper;
            return this;
        }

        @Override
        public BrigadierMappingBuilder nativeSuggestions() {
            this.cloudSuggestions = false;
            this.suggestionsOverride = null;
            return this;
        }

        @Override
        public BrigadierMappingBuilder cloudSuggestions() {
            this.cloudSuggestions = true;
            this.suggestionsOverride = null;
            return this;
        }

        @Override
        public BrigadierMappingBuilder suggestedByConstant(final SuggestionProvider provider) {
            BrigadierMappingBuilder.super.suggestedByConstant(provider);
            this.cloudSuggestions = false;
            return this;
        }

        @Override
        public BrigadierMappingBuilder suggestedBy(final SuggestionProviderSupplier provider) {
            this.suggestionsOverride = requireNonNull(provider, "provider");
            this.cloudSuggestions = false;
            return this;
        }

        public BrigadierMapping build() {
            return new BrigadierMapping<>(this.cloudSuggestions, this.suggestionsOverride, this.mapper);
        }
    }
}