001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.plot.flag.implementations;
020
021import com.plotsquared.core.configuration.caption.TranslatableCaption;
022import com.plotsquared.core.plot.flag.FlagParseException;
023import com.plotsquared.core.plot.flag.PlotFlag;
024import net.kyori.adventure.text.Component;
025import net.kyori.adventure.text.minimessage.tag.Tag;
026import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
027import org.checkerframework.checker.nullness.qual.NonNull;
028import org.checkerframework.checker.nullness.qual.Nullable;
029
030import java.util.Arrays;
031import java.util.Collection;
032import java.util.Locale;
033
034public class TitlesFlag extends PlotFlag<TitlesFlag.TitlesFlagValue, TitlesFlag> {
035
036    public static final TitlesFlag TITLES_NONE = new TitlesFlag(TitlesFlagValue.NONE);
037    public static final TitlesFlag TITLES_TRUE = new TitlesFlag(TitlesFlagValue.TRUE);
038    public static final TitlesFlag TITLES_FALSE = new TitlesFlag(TitlesFlagValue.FALSE);
039
040    private TitlesFlag(final TitlesFlagValue value) {
041        super(value, TranslatableCaption.of("flags.flag_category_enum"), TranslatableCaption.of("flags.flag_description_titles"));
042    }
043
044    @Override
045    public TitlesFlag parse(final @NonNull String input) throws FlagParseException {
046        final TitlesFlagValue titlesFlagValue = TitlesFlagValue.fromString(input);
047        if (titlesFlagValue == null) {
048            throw new FlagParseException(
049                    this,
050                    input,
051                    TranslatableCaption.of("flags.flag_error_enum"),
052                    TagResolver.resolver("list", Tag.inserting(Component.text("none, true, false")))
053            );
054        }
055        return flagOf(titlesFlagValue);
056    }
057
058    @Override
059    public TitlesFlag merge(@NonNull TitlesFlagValue newValue) {
060        if (newValue == TitlesFlagValue.TRUE || newValue == TitlesFlagValue.FALSE) {
061            return flagOf(newValue);
062        }
063        return this;
064    }
065
066    @Override
067    public String toString() {
068        return getValue().name().toLowerCase(Locale.ENGLISH);
069    }
070
071    @Override
072    public String getExample() {
073        return "true";
074    }
075
076    @Override
077    protected TitlesFlag flagOf(@NonNull TitlesFlagValue value) {
078        if (value == TitlesFlagValue.TRUE) {
079            return TITLES_TRUE;
080        } else if (value == TitlesFlagValue.FALSE) {
081            return TITLES_FALSE;
082        }
083        return TITLES_NONE;
084    }
085
086    @Override
087    public Collection<String> getTabCompletions() {
088        return Arrays.asList("none", "true", "false");
089    }
090
091    public enum TitlesFlagValue {
092        NONE,
093        TRUE,
094        FALSE;
095
096        public static @Nullable TitlesFlagValue fromString(final String value) {
097            if (value.equalsIgnoreCase("true")) {
098                return TRUE;
099            } else if (value.equalsIgnoreCase("false")) {
100                return FALSE;
101            } else if (value.equalsIgnoreCase("none")) {
102                return NONE;
103            }
104            return null;
105        }
106    }
107
108}