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 com.plotsquared.core.util.ItemUtil;
025import com.sk89q.worldedit.world.item.ItemType;
026import com.sk89q.worldedit.world.item.ItemTypes;
027import org.checkerframework.checker.nullness.qual.NonNull;
028
029public class MusicFlag extends PlotFlag<ItemType, MusicFlag> {
030
031    public static final MusicFlag MUSIC_FLAG_NONE = new MusicFlag(ItemTypes.AIR);
032
033    /**
034     * Construct a new flag instance.
035     *
036     * @param value Flag value
037     */
038    protected MusicFlag(ItemType value) {
039        super(value, TranslatableCaption.of("flags.flag_category_music"), TranslatableCaption.of("flags.flag_description_music"));
040    }
041
042    @Override
043    public MusicFlag parse(@NonNull String input) throws FlagParseException {
044        if (!input.isEmpty() && !input.contains("music_disc_")) {
045            input = "music_disc_" + input;
046        }
047        final ItemType itemType = ItemUtil.get(input);
048        if (itemType != null && itemType.getId() != null && (itemType == ItemTypes.AIR || itemType
049                .getId().contains("music_disc_"))) {
050            return new MusicFlag(ItemUtil.get(input));
051        } else {
052            throw new FlagParseException(this, input, TranslatableCaption.of("flags.flag_error_music"));
053        }
054    }
055
056    @Override
057    public MusicFlag merge(@NonNull ItemType newValue) {
058        if (getValue().equals(ItemTypes.AIR)) {
059            return new MusicFlag(newValue);
060        } else if (newValue.equals(ItemTypes.AIR)) {
061            return this;
062        } else {
063            return new MusicFlag(newValue);
064        }
065    }
066
067    @Override
068    public String toString() {
069        return getValue().getId();
070    }
071
072    @Override
073    public String getExample() {
074        return "ward";
075    }
076
077    @Override
078    protected MusicFlag flagOf(@NonNull ItemType value) {
079        return new MusicFlag(value);
080    }
081
082}