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.Component;
032import net.kyori.adventure.text.minimessage.tag.Tag;
033import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
034import org.checkerframework.checker.nullness.qual.NonNull;
035
036import java.util.Collection;
037import java.util.Collections;
038import java.util.UUID;
039import java.util.concurrent.TimeoutException;
040
041@CommandDeclaration(command = "remove",
042        aliases = {"r", "untrust", "ut", "undeny", "unban", "ud", "pardon"},
043        usage = "/plot remove <player | *>",
044        category = CommandCategory.SETTINGS,
045        requiredType = RequiredType.NONE,
046        permission = "plots.remove")
047public class Remove extends SubCommand {
048
049    private final EventDispatcher eventDispatcher;
050
051    @Inject
052    public Remove(final @NonNull EventDispatcher eventDispatcher) {
053        super(Argument.PlayerName);
054        this.eventDispatcher = eventDispatcher;
055    }
056
057    @Override
058    public boolean onCommand(PlotPlayer<?> player, String[] args) {
059        Location location = player.getLocation();
060        Plot plot = location.getPlotAbs();
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 (plot.getTrusted().contains(uuid)) {
088                        if (plot.removeTrusted(uuid)) {
089                            this.eventDispatcher.callTrusted(player, plot, uuid, false);
090                            count++;
091                        }
092                    } else if (plot.getMembers().contains(uuid)) {
093                        if (plot.removeMember(uuid)) {
094                            this.eventDispatcher.callMember(player, plot, uuid, false);
095                            count++;
096                        }
097                    } else if (plot.getDenied().contains(uuid)) {
098                        if (plot.removeDenied(uuid)) {
099                            this.eventDispatcher.callDenied(player, plot, uuid, false);
100                            count++;
101                        }
102                    } else if (uuid == DBFunc.EVERYONE) {
103                        if (plot.removeTrusted(uuid)) {
104                            this.eventDispatcher.callTrusted(player, plot, uuid, false);
105                            count++;
106                        } else if (plot.removeMember(uuid)) {
107                            this.eventDispatcher.callMember(player, plot, uuid, false);
108                            count++;
109                        } else if (plot.removeDenied(uuid)) {
110                            this.eventDispatcher.callDenied(player, plot, uuid, false);
111                            count++;
112                        }
113                    }
114                }
115            }
116            if (count == 0) {
117                player.sendMessage(
118                        TranslatableCaption.of("errors.invalid_player"),
119                        TagResolver.resolver("value", 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        Location location = player.getLocation();
134        Plot plot = location.getPlotAbs();
135        if (plot == null) {
136            return Collections.emptyList();
137        }
138        return TabCompletions.completeAddedPlayers(player, plot, String.join(",", args).trim(),
139                Collections.singletonList(player.getName())
140        );
141    }
142
143}