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.impl.util.PCollections;
020import ca.stellardrift.permissionsex.minecraft.MinecraftPermissionsEx;
021import ca.stellardrift.permissionsex.minecraft.command.CommandException;
022import ca.stellardrift.permissionsex.minecraft.command.PEXCommandPreprocessor;
023import ca.stellardrift.permissionsex.subject.SubjectType;
024import cloud.commandframework.arguments.parser.ArgumentParseResult;
025import cloud.commandframework.arguments.parser.ArgumentParser;
026import cloud.commandframework.context.CommandContext;
027import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
028import org.checkerframework.checker.nullness.qual.NonNull;
029import org.checkerframework.checker.nullness.qual.Nullable;
030
031import java.util.List;
032import java.util.Locale;
033import java.util.Queue;
034
035public final class SubjectTypeParser<C> implements ArgumentParser<C, SubjectType<?>> {
036
037    SubjectTypeParser() {
038    }
039
040    @Override
041    public @NonNull ArgumentParseResult<SubjectType<?>> parse(@NonNull CommandContext<C> commandContext, @NonNull Queue<String> inputQueue) {
042        final @Nullable String input = inputQueue.peek();
043        if (input == null) {
044            return ArgumentParseResult.failure(new NoInputProvidedException(SubjectTypeParser.class, commandContext));
045        }
046
047        final MinecraftPermissionsEx<?> pex = commandContext.get(PEXCommandPreprocessor.PEX_MANAGER);
048        final String lowerInput = input.toLowerCase(Locale.ROOT);
049        for (final SubjectType<?> type : pex.engine().knownSubjectTypes()) {
050            if (type.name().toLowerCase(Locale.ROOT).startsWith(lowerInput)) { // TODO: do we want to use startsWith here?
051                inputQueue.remove();
052                return ArgumentParseResult.success(type);
053            }
054        }
055        return ArgumentParseResult.failure(new CommandException(Messages.SUBJECTTYPE_ERROR_NOTATYPE.tr(input)));
056    }
057
058    @Override
059    public @NonNull List<String> suggestions(@NonNull CommandContext<C> commandContext, @NonNull String input) {
060        final MinecraftPermissionsEx<?> pex = commandContext.get(PEXCommandPreprocessor.PEX_MANAGER);
061        return PCollections.asVector(pex.engine().knownSubjectTypes(), SubjectType::name);
062    }
063
064}