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