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.Settings;
023import com.plotsquared.core.configuration.caption.TranslatableCaption;
024import com.plotsquared.core.database.DBFunc;
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 com.plotsquared.core.util.task.RunnableVal2;
032import com.plotsquared.core.util.task.RunnableVal3;
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.Iterator;
041import java.util.UUID;
042import java.util.concurrent.CompletableFuture;
043import java.util.concurrent.TimeoutException;
044
045@CommandDeclaration(command = "add",
046        usage = "/plot add <player | *>",
047        category = CommandCategory.SETTINGS,
048        permission = "plots.add",
049        requiredType = RequiredType.PLAYER)
050public class Add extends Command {
051
052    private final EventDispatcher eventDispatcher;
053
054    @Inject
055    public Add(final @NonNull EventDispatcher eventDispatcher) {
056        super(MainCommand.getInstance(), true);
057        this.eventDispatcher = eventDispatcher;
058    }
059
060    @Override
061    public CompletableFuture<Boolean> execute(
062            final PlotPlayer<?> player,
063            String[] args,
064            RunnableVal3<Command, Runnable, Runnable> confirm,
065            RunnableVal2<Command, CommandResult> whenDone
066    ) throws CommandException {
067        final Plot plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
068        checkTrue(plot.hasOwner(), TranslatableCaption.of("info.plot_unowned"));
069        checkTrue(
070                plot.isOwner(player.getUUID()) || player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_TRUST),
071                TranslatableCaption.of("permission.no_plot_perms")
072        );
073        checkTrue(
074                args.length == 1,
075                TranslatableCaption.of("commandconfig.command_syntax"),
076                TagResolver.resolver("value", Tag.inserting(Component.text("/plot add <player | *>")))
077        );
078        final CompletableFuture<Boolean> future = new CompletableFuture<>();
079        PlayerManager.getUUIDsFromString(args[0], (uuids, throwable) -> {
080            if (throwable != null) {
081                if (throwable instanceof TimeoutException) {
082                    player.sendMessage(TranslatableCaption.of("players.fetching_players_timeout"));
083                } else {
084                    player.sendMessage(
085                            TranslatableCaption.of("errors.invalid_player"),
086                            TagResolver.resolver("value", Tag.inserting(Component.text(args[0])))
087                    );
088                }
089                future.completeExceptionally(throwable);
090                return;
091            } else {
092                try {
093                    checkTrue(!uuids.isEmpty(), TranslatableCaption.of("errors.invalid_player"),
094                            TagResolver.resolver("value", Tag.inserting(Component.text(args[0])))
095                    );
096                    Iterator<UUID> iterator = uuids.iterator();
097                    int size = plot.getTrusted().size() + plot.getMembers().size();
098                    while (iterator.hasNext()) {
099                        UUID uuid = iterator.next();
100                        if (uuid == DBFunc.EVERYONE && !(player.hasPermission(Permission.PERMISSION_TRUST_EVERYONE) || player.hasPermission(
101                                Permission.PERMISSION_ADMIN_COMMAND_TRUST))) {
102                            player.sendMessage(
103                                    TranslatableCaption.of("errors.invalid_player"),
104                                    TagResolver.resolver("value", Tag.inserting(
105                                            PlayerManager.resolveName(uuid).toComponent(player)
106                                    ))
107                            );
108                            iterator.remove();
109                            continue;
110                        }
111                        if (plot.isOwner(uuid)) {
112                            player.sendMessage(
113                                    TranslatableCaption.of("member.already_added"),
114                                    TagResolver.resolver("player", Tag.inserting(
115                                            PlayerManager.resolveName(uuid).toComponent(player)
116                                    ))
117                            );
118                            iterator.remove();
119                            continue;
120                        }
121                        if (plot.getMembers().contains(uuid)) {
122                            player.sendMessage(
123                                    TranslatableCaption.of("member.already_added"),
124                                    TagResolver.resolver("player", Tag.inserting(
125                                            PlayerManager.resolveName(uuid).toComponent(player)
126                                    ))
127                            );
128                            iterator.remove();
129                            continue;
130                        }
131                        size += plot.getTrusted().contains(uuid) ? 0 : 1;
132                    }
133                    checkTrue(!uuids.isEmpty(), null);
134                    int localAddSize = plot.getMembers().size();
135                    int maxAddSize = player.hasPermissionRange(Permission.PERMISSION_ADD, Settings.Limit.MAX_PLOTS);
136                    if (localAddSize >= maxAddSize) {
137                        player.sendMessage(
138                                TranslatableCaption.of("members.plot_max_members_added"),
139                                TagResolver.resolver("amount", Tag.inserting(Component.text(localAddSize)))
140                        );
141                        return;
142                    }
143                    // Success
144                    confirm.run(this, () -> {
145                        for (UUID uuid : uuids) {
146                            if (uuid != DBFunc.EVERYONE) {
147                                if (!plot.removeTrusted(uuid)) {
148                                    if (plot.getDenied().contains(uuid)) {
149                                        plot.removeDenied(uuid);
150                                    }
151                                }
152                            }
153                            plot.addMember(uuid);
154                            this.eventDispatcher.callMember(player, plot, uuid, true);
155                            player.sendMessage(TranslatableCaption.of("member.member_added"));
156                        }
157                    }, null);
158                } catch (final Throwable exception) {
159                    future.completeExceptionally(exception);
160                    return;
161                }
162            }
163            future.complete(true);
164        });
165        return future;
166    }
167
168    @Override
169    public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
170        return TabCompletions.completePlayers(player, String.join(",", args).trim(), Collections.emptyList());
171    }
172
173}