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.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.database.DBFunc;
024import com.plotsquared.core.location.Location;
025import com.plotsquared.core.permissions.Permission;
026import com.plotsquared.core.player.PlotPlayer;
027import com.plotsquared.core.plot.Plot;
028import com.plotsquared.core.util.EventDispatcher;
029import com.plotsquared.core.util.PlayerManager;
030import com.plotsquared.core.util.TabCompletions;
031import net.kyori.adventure.text.minimessage.Template;
032import org.checkerframework.checker.nullness.qual.NonNull;
033
034import java.util.Collection;
035import java.util.Collections;
036import java.util.UUID;
037import java.util.concurrent.TimeoutException;
038
039@CommandDeclaration(command = "remove",
040        aliases = {"r", "untrust", "ut", "undeny", "unban", "ud", "pardon"},
041        usage = "/plot remove <player | *>",
042        category = CommandCategory.SETTINGS,
043        requiredType = RequiredType.NONE,
044        permission = "plots.remove")
045public class Remove extends SubCommand {
046
047    private final EventDispatcher eventDispatcher;
048
049    @Inject
050    public Remove(final @NonNull EventDispatcher eventDispatcher) {
051        super(Argument.PlayerName);
052        this.eventDispatcher = eventDispatcher;
053    }
054
055    @Override
056    public boolean onCommand(PlotPlayer<?> player, String[] args) {
057        Location location = player.getLocation();
058        Plot plot = location.getPlotAbs();
059        if (plot == null) {
060            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
061            return false;
062        }
063        if (!plot.hasOwner()) {
064            player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
065            return false;
066        }
067        if (!plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_REMOVE)) {
068            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
069            return true;
070        }
071
072        PlayerManager.getUUIDsFromString(args[0], (uuids, throwable) -> {
073            int count = 0;
074            if (throwable instanceof TimeoutException) {
075                player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout"));
076                return;
077            } else if (throwable != null) {
078                player.sendMessage(
079                        TranslatableCaption.of("errors.invalid_player"),
080                        Template.of("value", args[0])
081                );
082                return;
083            } else if (!uuids.isEmpty()) {
084                for (UUID uuid : uuids) {
085                    if (plot.getTrusted().contains(uuid)) {
086                        if (plot.removeTrusted(uuid)) {
087                            this.eventDispatcher.callTrusted(player, plot, uuid, false);
088                            count++;
089                        }
090                    } else if (plot.getMembers().contains(uuid)) {
091                        if (plot.removeMember(uuid)) {
092                            this.eventDispatcher.callMember(player, plot, uuid, false);
093                            count++;
094                        }
095                    } else if (plot.getDenied().contains(uuid)) {
096                        if (plot.removeDenied(uuid)) {
097                            this.eventDispatcher.callDenied(player, plot, uuid, false);
098                            count++;
099                        }
100                    } else if (uuid == DBFunc.EVERYONE) {
101                        if (plot.removeTrusted(uuid)) {
102                            this.eventDispatcher.callTrusted(player, plot, uuid, false);
103                            count++;
104                        } else if (plot.removeMember(uuid)) {
105                            this.eventDispatcher.callMember(player, plot, uuid, false);
106                            count++;
107                        } else if (plot.removeDenied(uuid)) {
108                            this.eventDispatcher.callDenied(player, plot, uuid, false);
109                            count++;
110                        }
111                    }
112                }
113            }
114            if (count == 0) {
115                player.sendMessage(
116                        TranslatableCaption.of("member.player_not_removed"),
117                        Template.of("player", args[0])
118                );
119            } else {
120                player.sendMessage(
121                        TranslatableCaption.of("member.removed_players"),
122                        Template.of("amount", count + "")
123                );
124            }
125        });
126        return true;
127    }
128
129    @Override
130    public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
131        Location location = player.getLocation();
132        Plot plot = location.getPlotAbs();
133        if (plot == null) {
134            return Collections.emptyList();
135        }
136        return TabCompletions.completeAddedPlayers(player, plot, String.join(",", args).trim(),
137                Collections.singletonList(player.getName())
138        );
139    }
140
141}