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.sk89q.worldedit.world.gamemode.GameMode;
025import com.sk89q.worldedit.world.gamemode.GameModes;
026import org.checkerframework.checker.nullness.qual.NonNull;
027
028public class GuestGamemodeFlag extends PlotFlag<GameMode, GuestGamemodeFlag> {
029
030    public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_CREATIVE =
031            new GuestGamemodeFlag(GameModes.CREATIVE);
032    public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_ADVENTURE =
033            new GuestGamemodeFlag(GameModes.ADVENTURE);
034    public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_SPECTATOR =
035            new GuestGamemodeFlag(GameModes.SPECTATOR);
036    public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_SURVIVAL =
037            new GuestGamemodeFlag(GameModes.SURVIVAL);
038    public static final GuestGamemodeFlag GUEST_GAMEMODE_FLAG_DEFAULT =
039            new GuestGamemodeFlag(GamemodeFlag.DEFAULT);
040
041    /**
042     * Construct a new flag instance.
043     *
044     * @param value Flag value
045     */
046    protected GuestGamemodeFlag(@NonNull GameMode value) {
047        super(
048                value,
049                TranslatableCaption.of("flags.flag_category_gamemode"),
050                TranslatableCaption.of("flags.flag_description_guest_gamemode")
051        );
052    }
053
054    @Override
055    public GuestGamemodeFlag parse(@NonNull String input) throws FlagParseException {
056        return switch (input) {
057            case "creative", "c", "1" -> flagOf(GameModes.CREATIVE);
058            case "adventure", "a", "2" -> flagOf(GameModes.ADVENTURE);
059            case "spectator", "sp", "3" -> flagOf(GameModes.SPECTATOR);
060            case "survival", "s", "0" -> flagOf(GameModes.SURVIVAL);
061            default -> flagOf(GamemodeFlag.DEFAULT);
062        };
063    }
064
065    @Override
066    public GuestGamemodeFlag merge(@NonNull GameMode newValue) {
067        return flagOf(newValue);
068    }
069
070    @Override
071    public String toString() {
072        return getValue().getId();
073    }
074
075    @Override
076    public String getExample() {
077        return "survival";
078    }
079
080    @Override
081    protected GuestGamemodeFlag flagOf(@NonNull GameMode value) {
082        return switch (value.getId()) {
083            case "creative" -> GUEST_GAMEMODE_FLAG_CREATIVE;
084            case "adventure" -> GUEST_GAMEMODE_FLAG_ADVENTURE;
085            case "spectator" -> GUEST_GAMEMODE_FLAG_SPECTATOR;
086            case "survival" -> GUEST_GAMEMODE_FLAG_SURVIVAL;
087            default -> GUEST_GAMEMODE_FLAG_DEFAULT;
088        };
089    }
090
091}