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.Component;
029import net.kyori.adventure.text.minimessage.tag.Tag;
030import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
031
032import java.util.concurrent.CompletableFuture;
033
034@CommandDeclaration(usage = "/plot swap <X;Z>",
035        command = "swap",
036        aliases = {"switch"},
037        category = CommandCategory.CLAIMING,
038        requiredType = RequiredType.PLAYER)
039public class Swap extends SubCommand {
040
041    @Override
042    public CompletableFuture<Boolean> execute(
043            PlotPlayer<?> player, String[] args,
044            RunnableVal3<Command, Runnable, Runnable> confirm,
045            RunnableVal2<Command, CommandResult> whenDone
046    ) {
047        Location location = player.getLocation();
048        Plot plot1 = location.getPlotAbs();
049        if (plot1 == null) {
050            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
051            return CompletableFuture.completedFuture(false);
052        }
053        if (!plot1.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN)) {
054            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
055            return CompletableFuture.completedFuture(false);
056        }
057        if (args.length != 1) {
058            sendUsage(player);
059            return CompletableFuture.completedFuture(false);
060        }
061        Plot plot2 = Plot.getPlotFromString(player, args[0], true);
062        if (plot2 == null) {
063            return CompletableFuture.completedFuture(false);
064        }
065        if (plot1.equals(plot2)) {
066            player.sendMessage(TranslatableCaption.of("invalid.origin_cant_be_target"));
067            return CompletableFuture.completedFuture(false);
068        }
069        if (!plot1.getArea().isCompatible(plot2.getArea())) {
070            player.sendMessage(TranslatableCaption.of("errors.plotworld_incompatible"));
071            return CompletableFuture.completedFuture(false);
072        }
073        if (plot1.isMerged() || plot2.isMerged()) {
074            player.sendMessage(TranslatableCaption.of("swap.swap_merged"));
075            return CompletableFuture.completedFuture(false);
076        }
077
078        // Set strings here as the plot objects are mutable (the PlotID changes after being moved).
079        String p1 = plot1.toString();
080        String p2 = plot2.toString();
081
082        return plot1.getPlotModificationManager().move(plot2, player, () -> {
083        }, true).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}