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.permissions.Permission;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.plot.Plot;
027import com.plotsquared.core.util.EventDispatcher;
028import com.plotsquared.core.util.PlayerManager;
029import com.plotsquared.core.util.TabCompletions;
030import net.kyori.adventure.text.Component;
031import net.kyori.adventure.text.minimessage.tag.Tag;
032import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
033import org.checkerframework.checker.nullness.qual.NonNull;
034
035import java.util.Collection;
036import java.util.Collections;
037import java.util.UUID;
038import java.util.concurrent.TimeoutException;
039
040@CommandDeclaration(command = "remove",
041        aliases = {"r", "untrust", "ut", "undeny", "unban", "ud", "pardon"},
042        usage = "/plot remove <player | *>",
043        category = CommandCategory.SETTINGS,
044        requiredType = RequiredType.NONE,
045        permission = "plots.remove")
046public class Remove extends SubCommand {
047
048    private final EventDispatcher eventDispatcher;
049
050    @Inject
051    public Remove(final @NonNull EventDispatcher eventDispatcher) {
052        super(Argument.PlayerName);
053        this.eventDispatcher = eventDispatcher;
054    }
055
056    @Override
057    public boolean onCommand(PlotPlayer<?> player, String[] args) {
058        Plot plot = player.getCurrentPlot();
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                        TagResolver.resolver("value", Tag.inserting(Component.text(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                        count += plot.getTrusted().size();
102                        if (plot.removeTrusted(uuid)) {
103                            this.eventDispatcher.callTrusted(player, plot, uuid, false);
104                        }
105                        count += plot.getMembers().size();
106                        if (plot.removeMember(uuid)) {
107                            this.eventDispatcher.callMember(player, plot, uuid, false);
108                        }
109                        count += plot.getDenied().size();
110                        if (plot.removeDenied(uuid)) {
111                            this.eventDispatcher.callDenied(player, plot, uuid, false);
112                        }
113                    }
114                }
115            }
116            if (count == 0) {
117                player.sendMessage(
118                        TranslatableCaption.of("member.player_not_removed"),
119                        TagResolver.resolver("player", Tag.inserting(Component.text(args[0])))
120                );
121            } else {
122                player.sendMessage(
123                        TranslatableCaption.of("member.removed_players"),
124                        TagResolver.resolver("amount", Tag.inserting(Component.text(count)))
125                );
126            }
127        });
128        return true;
129    }
130
131    @Override
132    public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
133        Plot plot = player.getCurrentPlot();
134        if (plot == null) {
135            return Collections.emptyList();
136        }
137        return TabCompletions.completeAddedPlayers(player, plot, String.join(",", args).trim(),
138                Collections.singletonList(player.getName())
139        );
140    }
141
142}