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.types;
020
021import com.plotsquared.core.configuration.caption.Caption;
022import com.plotsquared.core.plot.flag.PlotFlag;
023import com.plotsquared.core.util.StringMan;
024import org.checkerframework.checker.nullness.qual.NonNull;
025
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.List;
029
030public abstract class ListFlag<V, F extends PlotFlag<List<V>, F>> extends PlotFlag<List<V>, F> {
031
032    public ListFlag(final List<V> valueList, final Caption category, final Caption description) {
033        super(Collections.unmodifiableList(valueList), category, description);
034    }
035
036    @Override
037    public F merge(@NonNull List<V> newValue) {
038        final List<V> mergedList = new ArrayList<>();
039        // If a server already used PlotSquared before this fix, we remove all present duplicates on an eventual merge
040        for (final V v : getValue()) {
041            if (!mergedList.contains(v)) {
042                mergedList.add(v);
043            }
044        }
045        // Only add new values if not already present from #getValue()
046        for (final V v : newValue) {
047            if (!mergedList.contains(v)) {
048                mergedList.add(v);
049            }
050        }
051        return this.flagOf(mergedList);
052    }
053
054    @Override
055    public String toString() {
056        return StringMan.join(this.getValue(), ",");
057    }
058
059}