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