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.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.plot.flag.FlagParseException;
024import com.plotsquared.core.plot.flag.PlotFlag;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027public abstract class TimedFlag<T, F extends PlotFlag<TimedFlag.Timed<T>, F>>
028        extends PlotFlag<TimedFlag.Timed<T>, F> {
029
030    private final T defaultValue;
031
032    protected TimedFlag(@NonNull Timed<T> value, T defaultValue, @NonNull Caption flagDescription) {
033        super(value, TranslatableCaption.of("flags.flag_category_intervals"), flagDescription);
034        this.defaultValue = defaultValue;
035    }
036
037    @Override
038    public F parse(@NonNull String input) throws FlagParseException {
039        String[] split = input.split(" ", 2);
040        int interval;
041        try {
042            interval = Integer.parseInt(split[0]);
043        } catch (Throwable throwable) {
044            throw new FlagParseException(
045                    this,
046                    input,
047                    TranslatableCaption.of("flags.flag_error_integer")
048            );
049        }
050        if (interval < 1) {
051            throw new FlagParseException(
052                    this,
053                    input,
054                    TranslatableCaption.of("flags.flag_error_integer")
055            );
056        }
057        if (split.length == 1) {
058            return flagOf(new Timed<>(interval, defaultValue));
059        }
060        final T parsedValue = parseValue(split[1]);
061        return flagOf(new Timed<>(interval, parsedValue));
062    }
063
064    @Override
065    public F merge(@NonNull Timed<T> newValue) {
066        return flagOf(
067                new Timed<>(getValue().interval + newValue.interval, mergeValue(newValue.value)));
068    }
069
070    protected abstract T parseValue(String input) throws FlagParseException;
071
072    protected abstract T mergeValue(T other);
073
074    @Override
075    public String toString() {
076        return getValue().toString();
077    }
078
079    public static final class Timed<T> {
080
081        private final int interval;
082        private final T value;
083
084        public Timed(int interval, T value) {
085            this.interval = interval;
086            this.value = value;
087        }
088
089        public int getInterval() {
090            return interval;
091        }
092
093        public T getValue() {
094            return value;
095        }
096
097        @Override
098        public String toString() {
099            return String.format("%d %s", interval, value);
100        }
101
102    }
103
104}