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.minimessage.Template;
025import org.checkerframework.checker.nullness.qual.NonNull;
026import org.checkerframework.checker.nullness.qual.Nullable;
027
028import java.util.Arrays;
029import java.util.Collection;
030import java.util.Locale;
031
032public class TitlesFlag extends PlotFlag<TitlesFlag.TitlesFlagValue, TitlesFlag> {
033
034    public static final TitlesFlag TITLES_NONE = new TitlesFlag(TitlesFlagValue.NONE);
035    public static final TitlesFlag TITLES_TRUE = new TitlesFlag(TitlesFlagValue.TRUE);
036    public static final TitlesFlag TITLES_FALSE = new TitlesFlag(TitlesFlagValue.FALSE);
037
038    private TitlesFlag(final TitlesFlagValue value) {
039        super(value, TranslatableCaption.of("flags.flag_category_enum"), TranslatableCaption.of("flags.flag_description_titles"));
040    }
041
042    @Override
043    public TitlesFlag parse(final @NonNull String input) throws FlagParseException {
044        final TitlesFlagValue titlesFlagValue = TitlesFlagValue.fromString(input);
045        if (titlesFlagValue == null) {
046            throw new FlagParseException(
047                    this,
048                    input,
049                    TranslatableCaption.of("flags.flag_error_enum"),
050                    Template.of("list", "none, true, false")
051            );
052        }
053        return flagOf(titlesFlagValue);
054    }
055
056    @Override
057    public TitlesFlag merge(@NonNull TitlesFlagValue newValue) {
058        if (newValue == TitlesFlagValue.TRUE || newValue == TitlesFlagValue.FALSE) {
059            return flagOf(newValue);
060        }
061        return this;
062    }
063
064    @Override
065    public String toString() {
066        return getValue().name().toLowerCase(Locale.ENGLISH);
067    }
068
069    @Override
070    public String getExample() {
071        return "true";
072    }
073
074    @Override
075    protected TitlesFlag flagOf(@NonNull TitlesFlagValue value) {
076        if (value == TitlesFlagValue.TRUE) {
077            return TITLES_TRUE;
078        } else if (value == TitlesFlagValue.FALSE) {
079            return TITLES_FALSE;
080        }
081        return TITLES_NONE;
082    }
083
084    @Override
085    public Collection<String> getTabCompletions() {
086        return Arrays.asList("none", "true", "false");
087    }
088
089    public enum TitlesFlagValue {
090        NONE,
091        TRUE,
092        FALSE;
093
094        public static @Nullable TitlesFlagValue fromString(final String value) {
095            if (value.equalsIgnoreCase("true")) {
096                return TRUE;
097            } else if (value.equalsIgnoreCase("false")) {
098                return FALSE;
099            } else if (value.equalsIgnoreCase("none")) {
100                return NONE;
101            }
102            return null;
103        }
104    }
105
106}