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.plotsquared.core.configuration.caption.TranslatableCaption;
022import com.plotsquared.core.location.Location;
023import com.plotsquared.core.permissions.Permission;
024import com.plotsquared.core.player.PlotPlayer;
025import com.plotsquared.core.plot.Plot;
026import com.plotsquared.core.util.task.RunnableVal2;
027import com.plotsquared.core.util.task.RunnableVal3;
028import net.kyori.adventure.text.minimessage.Template;
029
030import java.util.concurrent.CompletableFuture;
031
032@CommandDeclaration(usage = "/plot swap <X;Z>",
033        command = "swap",
034        aliases = {"switch"},
035        category = CommandCategory.CLAIMING,
036        requiredType = RequiredType.PLAYER)
037public class Swap extends SubCommand {
038
039    @Override
040    public CompletableFuture<Boolean> execute(
041            PlotPlayer<?> player, String[] args,
042            RunnableVal3<Command, Runnable, Runnable> confirm,
043            RunnableVal2<Command, CommandResult> whenDone
044    ) {
045        Location location = player.getLocation();
046        Plot plot1 = location.getPlotAbs();
047        if (plot1 == null) {
048            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
049            return CompletableFuture.completedFuture(false);
050        }
051        if (!plot1.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN)) {
052            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
053            return CompletableFuture.completedFuture(false);
054        }
055        if (args.length != 1) {
056            sendUsage(player);
057            return CompletableFuture.completedFuture(false);
058        }
059        Plot plot2 = Plot.getPlotFromString(player, args[0], true);
060        if (plot2 == null) {
061            return CompletableFuture.completedFuture(false);
062        }
063        if (plot1.equals(plot2)) {
064            player.sendMessage(TranslatableCaption.of("invalid.origin_cant_be_target"));
065            return CompletableFuture.completedFuture(false);
066        }
067        if (!plot1.getArea().isCompatible(plot2.getArea())) {
068            player.sendMessage(TranslatableCaption.of("errors.plotworld_incompatible"));
069            return CompletableFuture.completedFuture(false);
070        }
071        if (plot1.isMerged() || plot2.isMerged()) {
072            player.sendMessage(TranslatableCaption.of("swap.swap_merged"));
073            return CompletableFuture.completedFuture(false);
074        }
075
076        // Set strings here as the plot objects are mutable (the PlotID changes after being moved).
077        String p1 = plot1.toString();
078        String p2 = plot2.toString();
079
080        return plot1.getPlotModificationManager().move(plot2, player, () -> {
081        }, true).thenApply(result -> {
082            if (result) {
083                player.sendMessage(
084                        TranslatableCaption.of("swap.swap_success"),
085                        Template.of("origin", p1),
086                        Template.of("target", p2)
087                );
088                return true;
089            } else {
090                player.sendMessage(TranslatableCaption.of("swap.swap_overlap"));
091                return false;
092            }
093        });
094    }
095
096    @Override
097    public boolean onCommand(final PlotPlayer<?> player, String[] args) {
098        return true;
099    }
100
101}