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