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.events.PlayerPlotAddRemoveEvent;
024import com.plotsquared.core.events.Result;
025import com.plotsquared.core.player.PlotPlayer;
026import com.plotsquared.core.plot.Plot;
027import com.plotsquared.core.util.EventDispatcher;
028import com.plotsquared.core.util.task.RunnableVal2;
029import com.plotsquared.core.util.task.RunnableVal3;
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.UUID;
036import java.util.concurrent.CompletableFuture;
037
038@CommandDeclaration(command = "leave",
039        permission = "plots.leave",
040        usage = "/plot leave",
041        category = CommandCategory.CLAIMING,
042        requiredType = RequiredType.PLAYER)
043public class Leave extends Command {
044
045    private final EventDispatcher eventDispatcher;
046
047    @Inject
048    public Leave(final @NonNull EventDispatcher eventDispatcher) {
049        super(MainCommand.getInstance(), true);
050        this.eventDispatcher = eventDispatcher;
051    }
052
053    @Override
054    public CompletableFuture<Boolean> execute(
055            PlotPlayer<?> player, String[] args,
056            RunnableVal3<Command, Runnable, Runnable> confirm,
057            RunnableVal2<Command, CommandResult> whenDone
058    ) throws CommandException {
059        final Plot plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
060        checkTrue(plot.hasOwner(), TranslatableCaption.of("info.plot_unowned"));
061        if (plot.isOwner(player.getUUID())) {
062            player.sendMessage(TranslatableCaption.of("member.plot_cant_leave_owner"));
063        } else {
064            UUID uuid = player.getUUID();
065            if (plot.isAdded(uuid)) {
066                if (this.eventDispatcher
067                        .callPlayerRemove(player, plot, uuid, PlayerPlotAddRemoveEvent.Reason.COMMAND)
068                        .getEventResult() == Result.DENY) {
069                    player.sendMessage(
070                            TranslatableCaption.of("events.event_denied"),
071                            TagResolver.resolver("value", Tag.inserting(Component.text("Leave")))
072                    );
073                    return  CompletableFuture.completedFuture(true);
074                }
075                if (plot.removeTrusted(uuid)) {
076                    this.eventDispatcher.callTrusted(player, plot, uuid, false);
077                    this.eventDispatcher.callPostTrusted(player, plot, uuid, false, PlayerPlotAddRemoveEvent.Reason.COMMAND);
078                }
079                if (plot.removeMember(uuid)) {
080                    this.eventDispatcher.callMember(player, plot, uuid, false);
081                    this.eventDispatcher.callPostAdded(player, plot, uuid, false, PlayerPlotAddRemoveEvent.Reason.COMMAND);
082                }
083                player.sendMessage(
084                        TranslatableCaption.of("member.plot_left"),
085                        TagResolver.resolver("player", Tag.inserting(Component.text(player.getName())))
086                );
087            } else {
088                player.sendMessage(
089                        TranslatableCaption.of("members.not_added_trusted")
090                );
091            }
092        }
093        return CompletableFuture.completedFuture(true);
094    }
095
096}