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.player.PlotPlayer;
023import com.plotsquared.core.plot.Plot;
024import com.plotsquared.core.plot.flag.FlagParseException;
025import com.plotsquared.core.plot.flag.PlotFlag;
026import net.kyori.adventure.text.Component;
027import net.kyori.adventure.text.minimessage.tag.Tag;
028import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
029import org.checkerframework.checker.nullness.qual.NonNull;
030import org.checkerframework.checker.nullness.qual.Nullable;
031
032import java.util.Arrays;
033import java.util.Collection;
034
035public class DenyTeleportFlag extends PlotFlag<DenyTeleportFlag.DeniedGroup, DenyTeleportFlag> {
036
037    public static final DenyTeleportFlag DENY_TELEPORT_FLAG_NONE =
038            new DenyTeleportFlag(DeniedGroup.NONE);
039
040    /**
041     * Construct a new flag instance.
042     *
043     * @param value Flag value
044     */
045    protected DenyTeleportFlag(@NonNull DeniedGroup value) {
046        super(
047                value,
048                TranslatableCaption.of("flags.flag_category_enum"),
049                TranslatableCaption.of("flags.flag_description_deny_teleport")
050        );
051    }
052
053    public static boolean allowsTeleport(PlotPlayer<?> player, Plot plot) {
054        final DeniedGroup value = plot.getFlag(DenyTeleportFlag.class);
055        if (value == DeniedGroup.NONE) {
056            return true;
057        }
058        final boolean result;
059        switch (value) {
060            case TRUSTED -> result = !plot.getTrusted().contains(player.getUUID());
061            case MEMBERS -> result = !plot.getMembers().contains(player.getUUID());
062            case NONMEMBERS -> result = plot.isAdded(player.getUUID());
063            case NONTRUSTED -> result =
064                    plot.getTrusted().contains(player.getUUID()) || plot.isOwner(player.getUUID());
065            case NONOWNERS -> result = plot.isOwner(player.getUUID());
066            default -> {
067                return true;
068            }
069        }
070        return result || player.hasPermission("plots.admin.entry.denied");
071    }
072
073    @Override
074    public DenyTeleportFlag parse(@NonNull String input) throws FlagParseException {
075        final DeniedGroup group = DeniedGroup.fromString(input);
076        if (group == null) {
077            throw new FlagParseException(this, input, TranslatableCaption.of("flags.flag_error_enum"),
078                    TagResolver.resolver(
079                            "list",
080                            Tag.inserting(Component.text("members, nonmembers, trusted, nontrusted, nonowners"))
081                    )
082            );
083        }
084        return flagOf(group);
085    }
086
087    @Override
088    public DenyTeleportFlag merge(@NonNull DeniedGroup newValue) {
089        if (getValue().ordinal() < newValue.ordinal()) {
090            return flagOf(newValue);
091        }
092        return this;
093    }
094
095    @Override
096    public String toString() {
097        return this.getValue().name();
098    }
099
100    @Override
101    public String getExample() {
102        return "trusted";
103    }
104
105    @Override
106    protected DenyTeleportFlag flagOf(@NonNull DeniedGroup value) {
107        return new DenyTeleportFlag(value);
108    }
109
110    @Override
111    public Collection<String> getTabCompletions() {
112        return Arrays.asList("none", "members", "trusted", "nonmembers", "nontrusted", "nonowners");
113    }
114
115    public enum DeniedGroup {
116        NONE,
117        MEMBERS,
118        TRUSTED,
119        NONMEMBERS,
120        NONTRUSTED,
121        NONOWNERS;
122
123        public static @Nullable DeniedGroup fromString(final @NonNull String string) {
124            for (final DeniedGroup group : values()) {
125                if (group.name().equalsIgnoreCase(string)) {
126                    return group;
127                }
128            }
129            return null;
130        }
131    }
132
133}