001/*
002 * PermissionsEx
003 * Copyright (C) zml and PermissionsEx contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ca.stellardrift.permissionsex.minecraft.command.argument;
018
019import ca.stellardrift.permissionsex.PermissionsEngine;
020import ca.stellardrift.permissionsex.minecraft.command.PEXCommandPreprocessor;
021import ca.stellardrift.permissionsex.rank.RankLadder;
022import cloud.commandframework.arguments.parser.ArgumentParseResult;
023import cloud.commandframework.arguments.parser.ArgumentParser;
024import cloud.commandframework.context.CommandContext;
025import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
026import org.checkerframework.checker.nullness.qual.NonNull;
027import org.checkerframework.checker.nullness.qual.Nullable;
028
029import java.util.List;
030import java.util.Queue;
031import java.util.stream.Collectors;
032
033public final class RankLadderParser<C> implements ArgumentParser<C, RankLadder> {
034
035    RankLadderParser() {
036    }
037
038    @Override
039    public @NonNull ArgumentParseResult<@NonNull RankLadder> parse(
040            final @NonNull CommandContext<@NonNull C> ctx,
041            final @NonNull Queue<@NonNull String> inputQueue) {
042        final @Nullable String input = inputQueue.poll();
043        if (input == null) {
044            return ArgumentParseResult.failure(new NoInputProvidedException(ContextValueParser.class, ctx));
045        }
046        final PermissionsEngine engine = ctx.get(PEXCommandPreprocessor.PEX_ENGINE);
047        return ArgumentParseResult.success(engine.ladders().get(input, null).join());
048    }
049
050    @Override
051    public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> ctx, @NonNull String input) {
052        final PermissionsEngine engine = ctx.get(PEXCommandPreprocessor.PEX_ENGINE);
053        return engine.ladders()
054                .names()
055                .collect(Collectors.toList());
056    }
057
058    @Override
059    public boolean isContextFree() {
060        return true;
061    }
062}