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
028import java.util.Arrays;
029import java.util.Collection;
030
031public class GamemodeFlag extends PlotFlag<GameMode, GamemodeFlag> {
032
033    public static final GameMode DEFAULT = new GameMode("default");
034    public static final GamemodeFlag GAMEMODE_FLAG_CREATIVE = new GamemodeFlag(GameModes.CREATIVE);
035    public static final GamemodeFlag GAMEMODE_FLAG_ADVENTURE =
036            new GamemodeFlag(GameModes.ADVENTURE);
037    public static final GamemodeFlag GAMEMODE_FLAG_SPECTATOR =
038            new GamemodeFlag(GameModes.SPECTATOR);
039    public static final GamemodeFlag GAMEMODE_FLAG_SURVIVAL = new GamemodeFlag(GameModes.SURVIVAL);
040    public static final GamemodeFlag GAMEMODE_FLAG_DEFAULT = new GamemodeFlag(DEFAULT);
041
042    static {
043        GameModes.register(DEFAULT);
044    }
045
046    /**
047     * Construct a new flag instance.
048     *
049     * @param value Flag value
050     */
051    protected GamemodeFlag(@NonNull GameMode value) {
052        super(
053                value,
054                TranslatableCaption.of("flags.flag_category_gamemode"),
055                TranslatableCaption.of("flags.flag_description_gamemode")
056        );
057    }
058
059    @Override
060    public GamemodeFlag parse(@NonNull String input) throws FlagParseException {
061        return switch (input) {
062            case "creative", "c", "1" -> flagOf(GameModes.CREATIVE);
063            case "adventure", "a", "2" -> flagOf(GameModes.ADVENTURE);
064            case "spectator", "sp", "3" -> flagOf(GameModes.SPECTATOR);
065            case "survival", "s", "0" -> flagOf(GameModes.SURVIVAL);
066            default -> flagOf(DEFAULT);
067        };
068    }
069
070    @Override
071    public GamemodeFlag merge(@NonNull GameMode newValue) {
072        return flagOf(newValue);
073    }
074
075    @Override
076    public String toString() {
077        return getValue().getId();
078    }
079
080    @Override
081    public String getExample() {
082        return "survival";
083    }
084
085    @Override
086    protected GamemodeFlag flagOf(@NonNull GameMode value) {
087        return switch (value.getId()) {
088            case "creative" -> GAMEMODE_FLAG_CREATIVE;
089            case "adventure" -> GAMEMODE_FLAG_ADVENTURE;
090            case "spectator" -> GAMEMODE_FLAG_SPECTATOR;
091            case "survival" -> GAMEMODE_FLAG_SURVIVAL;
092            default -> GAMEMODE_FLAG_DEFAULT;
093        };
094    }
095
096    @Override
097    public Collection<String> getTabCompletions() {
098        return Arrays.asList("survival", "creative", "adventure", "spectator");
099    }
100
101}