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        Location location = player.getLocation();
069        Plot plot = location.getPlot();
070        if (plot == null) {
071            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
072            return false;
073        }
074        if ((!plot.hasOwner() || !plot.isOwner(player.getUUID())) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_KICK)) {
075            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
076            return false;
077        }
078
079        PlayerManager.getUUIDsFromString(args[0], (uuids, throwable) -> {
080            if (throwable instanceof TimeoutException) {
081                player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout"));
082            } else if (throwable != null || uuids.isEmpty()) {
083                player.sendMessage(
084                        TranslatableCaption.of("errors.invalid_player"),
085                        TagResolver.resolver("value", Tag.inserting(Component.text(args[0])))
086                );
087            } else {
088                Set<PlotPlayer<?>> players = new HashSet<>();
089                for (UUID uuid : uuids) {
090                    if (uuid == DBFunc.EVERYONE) {
091                        for (PlotPlayer<?> pp : plot.getPlayersInPlot()) {
092                            if (pp == player || pp.hasPermission(Permission.PERMISSION_ADMIN_ENTRY_DENIED)) {
093                                continue;
094                            }
095                            players.add(pp);
096                        }
097                        continue;
098                    }
099                    PlotPlayer<?> pp = PlotSquared.platform().playerManager().getPlayerIfExists(uuid);
100                    if (pp != null) {
101                        players.add(pp);
102                    }
103                }
104                players.remove(player); // Don't ever kick the calling player
105                if (players.isEmpty()) {
106                    player.sendMessage(
107                            TranslatableCaption.of("errors.invalid_player"),
108                            TagResolver.resolver("value", Tag.inserting(Component.text(args[0])))
109                    );
110                    return;
111                }
112                for (PlotPlayer<?> player2 : players) {
113                    if (!plot.equals(player2.getCurrentPlot())) {
114                        player.sendMessage(
115                                TranslatableCaption.of("kick.player_not_in_plot"),
116                                TagResolver.resolver("player", Tag.inserting(Component.text(player2.getName())))
117                        );
118                        return;
119                    }
120                    if (player2.hasPermission(Permission.PERMISSION_ADMIN_ENTRY_DENIED)) {
121                        player.sendMessage(
122                                TranslatableCaption.of("kick.cannot_kick_player"),
123                                TagResolver.resolver("player", Tag.inserting(Component.text(player2.getName())))
124                        );
125                        return;
126                    }
127                    Location spawn = this.worldUtil.getSpawn(location.getWorldName());
128                    player2.sendMessage(TranslatableCaption.of("kick.you_got_kicked"));
129                    if (plot.equals(spawn.getPlot())) {
130                        Location newSpawn = this.worldUtil.getSpawn(this.plotAreaManager.getAllWorlds()[0]);
131                        if (plot.equals(newSpawn.getPlot())) {
132                            // Kick from server if you can't be teleported to spawn
133                            // Use string based message here for legacy uses
134                            player2.kick("You got kicked from the plot! This server did not set up a loaded spawn, so you got " +
135                                    "kicked from the server.");
136                        } else {
137                            player2.plotkick(newSpawn);
138                        }
139                    } else {
140                        player2.plotkick(spawn);
141                    }
142                }
143            }
144        });
145
146        return true;
147    }
148
149    @Override
150    public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
151        Location location = player.getLocation();
152        Plot plot = location.getPlotAbs();
153        if (plot == null) {
154            return Collections.emptyList();
155        }
156        return TabCompletions.completePlayersInPlot(plot, String.join(",", args).trim(),
157                Collections.singletonList(player.getName())
158        );
159    }
160
161}