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.PlotTitle;
023import com.plotsquared.core.plot.flag.FlagParseException;
024import com.plotsquared.core.plot.flag.PlotFlag;
025import com.plotsquared.core.util.StringMan;
026import org.checkerframework.checker.nullness.qual.NonNull;
027
028public class PlotTitleFlag extends PlotFlag<PlotTitle, PlotTitleFlag> {
029
030    public static final PlotTitleFlag TITLE_FLAG_DEFAULT = new PlotTitleFlag(PlotTitle.CONFIGURED);
031
032    /**
033     * Construct a new flag instance.
034     *
035     * @param value Flag value
036     */
037    protected PlotTitleFlag(PlotTitle value) {
038        super(
039                value,
040                TranslatableCaption.of("flags.flag_category_string"),
041                TranslatableCaption.of("flags.flag_description_title")
042        );
043    }
044
045    @Override
046    public PlotTitleFlag parse(@NonNull String input) throws FlagParseException {
047        if (input.equals("CONFIGURED")) {
048            return TITLE_FLAG_DEFAULT;
049        }
050        if (!input.contains("\"")) {
051            return new PlotTitleFlag(new PlotTitle(input, ""));
052        }
053
054        var split = StringMan.splitMessage(input);
055
056        if (split.isEmpty() || split.size() > 2) {
057            throw new FlagParseException(this, input, TranslatableCaption.of("flags.flag_error_title"));
058        }
059        PlotTitle value;
060        if (split.size() == 1) {
061            value = new PlotTitle(split.get(0), "");
062        } else {
063            value = new PlotTitle(split.get(0), split.get(1));
064        }
065        return new PlotTitleFlag(value);
066    }
067
068    @Override
069    public PlotTitleFlag merge(@NonNull PlotTitle newValue) {
070        if (getValue().title().isEmpty() && getValue().subtitle().isEmpty()) {
071            return new PlotTitleFlag(newValue);
072        } else if (getValue().subtitle().isEmpty()) {
073            return new PlotTitleFlag(new PlotTitle(getValue().title(), newValue.subtitle()));
074        } else if (getValue().title().isEmpty()) {
075            return new PlotTitleFlag(new PlotTitle(newValue.title(), getValue().subtitle()));
076        } else {
077            return this;
078        }
079    }
080
081    @Override
082    public String toString() {
083        if (getValue() == PlotTitle.CONFIGURED) {
084            return "CONFIGURED";
085        }
086        return "\"" + getValue().title() + "\" \"" + getValue().subtitle() + "\"";
087    }
088
089    @Override
090    public boolean isValuedPermission() {
091        return false;
092    }
093
094    @Override
095    public String getExample() {
096        return "\"A Title\" \"The subtitle\"";
097    }
098
099    @Override
100    protected PlotTitleFlag flagOf(@NonNull PlotTitle value) {
101        return new PlotTitleFlag(value);
102    }
103
104}