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.caption.TranslatableCaption;
024import com.plotsquared.core.database.DBFunc;
025import com.plotsquared.core.location.Location;
026import com.plotsquared.core.permissions.Permission;
027import com.plotsquared.core.player.PlotPlayer;
028import com.plotsquared.core.plot.Plot;
029import com.plotsquared.core.plot.world.PlotAreaManager;
030import com.plotsquared.core.util.PlayerManager;
031import com.plotsquared.core.util.TabCompletions;
032import com.plotsquared.core.util.WorldUtil;
033import net.kyori.adventure.text.Component;
034import net.kyori.adventure.text.minimessage.tag.Tag;
035import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
036import org.checkerframework.checker.nullness.qual.NonNull;
037
038import java.util.Collection;
039import java.util.Collections;
040import java.util.HashSet;
041import java.util.Set;
042import java.util.UUID;
043import java.util.concurrent.TimeoutException;
044
045@CommandDeclaration(command = "kick",
046        aliases = "k",
047        permission = "plots.kick",
048        usage = "/plot kick <player | *>",
049        category = CommandCategory.TELEPORT,
050        requiredType = RequiredType.PLAYER)
051public class Kick extends SubCommand {
052
053    private final PlotAreaManager plotAreaManager;
054    private final WorldUtil worldUtil;
055
056    @Inject
057    public Kick(
058            final @NonNull PlotAreaManager plotAreaManager,
059            final @NonNull WorldUtil worldUtil
060    ) {
061        super(Argument.PlayerName);
062        this.plotAreaManager = plotAreaManager;
063        this.worldUtil = worldUtil;
064    }
065
066    @Override
067    public boolean onCommand(PlotPlayer<?> player, String[] args) {
068        Plot plot = player.getCurrentPlot();
069        if (plot == null) {
070            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
071            return false;
072        }
073        if ((!plot.hasOwner() || !plot.isOwner(player.getUUID())) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_KICK)) {
074            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
075            return false;
076        }
077
078        PlayerManager.getUUIDsFromString(args[0], (uuids, throwable) -> {
079            if (throwable instanceof TimeoutException) {
080                player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout"));
081            } else if (throwable != null || uuids.isEmpty()) {
082                player.sendMessage(
083                        TranslatableCaption.of("errors.invalid_player"),
084                        TagResolver.resolver("value", Tag.inserting(Component.text(args[0])))
085                );
086            } else {
087                Set<PlotPlayer<?>> players = new HashSet<>();
088                for (UUID uuid : uuids) {
089                    if (uuid == DBFunc.EVERYONE) {
090                        for (PlotPlayer<?> pp : plot.getPlayersInPlot()) {
091                            if (pp == player || pp.hasPermission(Permission.PERMISSION_ADMIN_ENTRY_DENIED)) {
092                                continue;
093                            }
094                            players.add(pp);
095                        }
096                        continue;
097                    }
098                    PlotPlayer<?> pp = PlotSquared.platform().playerManager().getPlayerIfExists(uuid);
099                    if (pp != null) {
100                        players.add(pp);
101                    }
102                }
103                players.remove(player); // Don't ever kick the calling player
104                if (players.isEmpty()) {
105                    player.sendMessage(
106                            TranslatableCaption.of("errors.invalid_player"),
107                            TagResolver.resolver("value", Tag.inserting(Component.text(args[0])))
108                    );
109                    return;
110                }
111                for (PlotPlayer<?> player2 : players) {
112                    if (!plot.equals(player2.getCurrentPlot())) {
113                        player.sendMessage(
114                                TranslatableCaption.of("kick.player_not_in_plot"),
115                                TagResolver.resolver("player", Tag.inserting(Component.text(player2.getName())))
116                        );
117                        return;
118                    }
119                    if (player2.hasPermission(Permission.PERMISSION_ADMIN_ENTRY_DENIED)) {
120                        player.sendMessage(
121                                TranslatableCaption.of("kick.cannot_kick_player"),
122                                TagResolver.resolver("player", Tag.inserting(Component.text(player2.getName())))
123                        );
124                        return;
125                    }
126                    Location spawn = this.worldUtil.getSpawn(plot.getWorldName());
127                    player2.sendMessage(TranslatableCaption.of("kick.you_got_kicked"));
128                    if (plot.equals(spawn.getPlot())) {
129                        Location newSpawn = this.worldUtil.getSpawn(this.plotAreaManager.getAllWorlds()[0]);
130                        if (plot.equals(newSpawn.getPlot())) {
131                            // Kick from server if you can't be teleported to spawn
132                            // Use string based message here for legacy uses
133                            player2.kick("You got kicked from the plot! This server did not set up a loaded spawn, so you got " +
134                                    "kicked from the server.");
135                        } else {
136                            player2.plotkick(newSpawn);
137                        }
138                    } else {
139                        player2.plotkick(spawn);
140                    }
141                }
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        Plot plot = player.getCurrentPlot();
151        if (plot == null) {
152            return Collections.emptyList();
153        }
154        return TabCompletions.completePlayersInPlot(player, plot, String.join(",", args).trim(),
155                Collections.singletonList(player.getName())
156        );
157    }
158
159}