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.permissions.Permission;
023import com.plotsquared.core.player.PlotPlayer;
024import com.plotsquared.core.plot.Plot;
025import com.plotsquared.core.util.task.RunnableVal2;
026import com.plotsquared.core.util.task.RunnableVal3;
027import net.kyori.adventure.text.Component;
028import net.kyori.adventure.text.minimessage.tag.Tag;
029import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
030
031import java.util.concurrent.CompletableFuture;
032
033@CommandDeclaration(usage = "/plot swap <X;Z>",
034        command = "swap",
035        aliases = {"switch"},
036        category = CommandCategory.CLAIMING,
037        requiredType = RequiredType.PLAYER)
038public class Swap extends SubCommand {
039
040    @Override
041    public CompletableFuture<Boolean> execute(
042            PlotPlayer<?> player, String[] args,
043            RunnableVal3<Command, Runnable, Runnable> confirm,
044            RunnableVal2<Command, CommandResult> whenDone
045    ) {
046        Plot plot1 = player.getCurrentPlot();
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(
081                plot2, player, () -> {
082                }, true
083        ).thenApply(result -> {
084            if (result) {
085                player.sendMessage(
086                        TranslatableCaption.of("swap.swap_success"),
087                        TagResolver.builder()
088                                .tag("origin", Tag.inserting(Component.text(p1)))
089                                .tag("target", Tag.inserting(Component.text(p2)))
090                                .build()
091                );
092                return true;
093            } else {
094                player.sendMessage(TranslatableCaption.of("swap.swap_overlap"));
095                return false;
096            }
097        });
098    }
099
100    @Override
101    public boolean onCommand(final PlotPlayer<?> player, String[] args) {
102        return true;
103    }
104
105}