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.command;
020
021import com.google.inject.Inject;
022import com.plotsquared.core.PlotSquared;
023import com.plotsquared.core.configuration.Settings;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.database.DBFunc;
026import com.plotsquared.core.events.TeleportCause;
027import com.plotsquared.core.location.Location;
028import com.plotsquared.core.permissions.Permission;
029import com.plotsquared.core.player.PlotPlayer;
030import com.plotsquared.core.plot.Plot;
031import com.plotsquared.core.plot.world.PlotAreaManager;
032import com.plotsquared.core.util.EventDispatcher;
033import com.plotsquared.core.util.PlayerManager;
034import com.plotsquared.core.util.TabCompletions;
035import com.plotsquared.core.util.WorldUtil;
036import com.sk89q.worldedit.world.gamemode.GameModes;
037import net.kyori.adventure.text.minimessage.Template;
038import org.checkerframework.checker.nullness.qual.NonNull;
039
040import java.util.Collection;
041import java.util.Collections;
042import java.util.UUID;
043import java.util.concurrent.TimeoutException;
044
045@CommandDeclaration(command = "deny",
046        aliases = {"d", "ban"},
047        usage = "/plot deny <player>",
048        category = CommandCategory.SETTINGS,
049        requiredType = RequiredType.PLAYER)
050public class Deny extends SubCommand {
051
052    private final PlotAreaManager plotAreaManager;
053    private final EventDispatcher eventDispatcher;
054    private final WorldUtil worldUtil;
055
056    @Inject
057    public Deny(
058            final @NonNull PlotAreaManager plotAreaManager,
059            final @NonNull EventDispatcher eventDispatcher,
060            final @NonNull WorldUtil worldUtil
061    ) {
062        super(Argument.PlayerName);
063        this.plotAreaManager = plotAreaManager;
064        this.eventDispatcher = eventDispatcher;
065        this.worldUtil = worldUtil;
066    }
067
068    @Override
069    public boolean onCommand(PlotPlayer<?> player, String[] args) {
070
071        Location location = player.getLocation();
072        final Plot plot = location.getPlotAbs();
073        if (plot == null) {
074            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
075            return false;
076        }
077        if (!plot.hasOwner()) {
078            player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
079            return false;
080        }
081        if (!plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_DENY)) {
082            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
083            return true;
084        }
085
086        int maxDenySize = player.hasPermissionRange(Permission.PERMISSION_DENY, Settings.Limit.MAX_PLOTS);
087        int size = plot.getDenied().size();
088        if (size >= maxDenySize) {
089            player.sendMessage(
090                    TranslatableCaption.of("members.plot_max_members_denied"),
091                    Template.of("amount", String.valueOf(size))
092            );
093            return false;
094        }
095
096        PlayerManager.getUUIDsFromString(args[0], (uuids, throwable) -> {
097            if (throwable instanceof TimeoutException) {
098                player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout"));
099            } else if (throwable != null || uuids.isEmpty()) {
100                player.sendMessage(
101                        TranslatableCaption.of("errors.invalid_player"),
102                        Template.of("value", args[0])
103                );
104            } else {
105                for (UUID uuid : uuids) {
106                    if (uuid == DBFunc.EVERYONE && !(
107                            player.hasPermission(Permission.PERMISSION_DENY_EVERYONE) || player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_DENY))) {
108                        player.sendMessage(
109                                TranslatableCaption.of("errors.invalid_player"),
110                                Template.of("value", args[0])
111                        );
112                    } else if (plot.isOwner(uuid)) {
113                        player.sendMessage(TranslatableCaption.of("deny.cant_remove_owner"));
114                        return;
115                    } else if (plot.getDenied().contains(uuid)) {
116                        player.sendMessage(
117                                TranslatableCaption.of("member.already_added"),
118                                Template.of("player", PlayerManager.resolveName(uuid).getComponent(player))
119                        );
120                        return;
121                    } else {
122                        if (uuid != DBFunc.EVERYONE) {
123                            plot.removeMember(uuid);
124                            plot.removeTrusted(uuid);
125                        }
126                        plot.addDenied(uuid);
127                        this.eventDispatcher.callDenied(player, plot, uuid, true);
128                        if (!uuid.equals(DBFunc.EVERYONE)) {
129                            handleKick(PlotSquared.platform().playerManager().getPlayerIfExists(uuid), plot);
130                        } else {
131                            for (PlotPlayer<?> plotPlayer : plot.getPlayersInPlot()) {
132                                // Ignore plot-owners
133                                if (plot.isAdded(plotPlayer.getUUID())) {
134                                    continue;
135                                }
136                                handleKick(plotPlayer, plot);
137                            }
138                        }
139                    }
140                }
141                player.sendMessage(TranslatableCaption.of("deny.denied_added"));
142            }
143        });
144
145        return true;
146    }
147
148    @Override
149    public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
150        return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList());
151    }
152
153    private void handleKick(PlotPlayer<?> player, Plot plot) {
154        plot = plot.getBasePlot(false);
155        if (player == null) {
156            return;
157        }
158        if (!plot.equals(player.getCurrentPlot())) {
159            return;
160        }
161        if (player.hasPermission("plots.admin.entry.denied")) {
162            return;
163        }
164        if (player.getGameMode() == GameModes.SPECTATOR) {
165            player.stopSpectating();
166        }
167        Location location = player.getLocation();
168        Location spawn = this.worldUtil.getSpawn(location.getWorldName());
169        player.sendMessage(TranslatableCaption.of("deny.you_got_denied"));
170        if (plot.equals(spawn.getPlot())) {
171            Location newSpawn = this.worldUtil.getSpawn(this.plotAreaManager.getAllWorlds()[0]);
172            if (plot.equals(newSpawn.getPlot())) {
173                // Kick from server if you can't be teleported to spawn
174                // Use string based message here for legacy uses
175                player.kick("You got kicked from the plot! This server did not set up a loaded spawn, so you got " +
176                        "kicked from the server.");
177            } else {
178                player.teleport(newSpawn, TeleportCause.DENIED);
179            }
180        } else {
181            player.teleport(spawn, TeleportCause.DENIED);
182        }
183    }
184
185}