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 net.kyori.adventure.text.minimessage.Template;
027
028@CommandDeclaration(command = "copy",
029        permission = "plots.copy",
030        aliases = {"copypaste"},
031        category = CommandCategory.CLAIMING,
032        usage = "/plot copy <X;Z>",
033        requiredType = RequiredType.NONE)
034public class Copy extends SubCommand {
035
036    @Override
037    public boolean onCommand(final PlotPlayer<?> player, String[] args) {
038        Location location = player.getLocation();
039        Plot plot1 = location.getPlotAbs();
040        if (plot1 == null) {
041            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
042            return false;
043        }
044        if (!plot1.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN.toString())) {
045            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
046            return false;
047        }
048        if (args.length != 1) {
049            player.sendMessage(
050                    TranslatableCaption.of("commandconfig.command_syntax"),
051                    Template.of("value", "/plot copy <X;Z>")
052            );
053            return false;
054        }
055        Plot plot2 = Plot.getPlotFromString(player, args[0], true);
056        if (plot2 == null) {
057            return false;
058        }
059        if (plot1.equals(plot2)) {
060            player.sendMessage(TranslatableCaption.of("invalid.origin_cant_be_target"));
061            return false;
062        }
063        if (!plot1.getArea().isCompatible(plot2.getArea())) {
064            player.sendMessage(TranslatableCaption.of("errors.plotworld_incompatible"));
065            return false;
066        }
067
068        plot1.getPlotModificationManager().copy(plot2, player).thenAccept(result -> {
069            if (result) {
070                player.sendMessage(TranslatableCaption.of("move.copy_success"), Template.of("origin", String.valueOf(plot1)),
071                        Template.of("target", String.valueOf(plot2))
072                );
073            } else {
074                player.sendMessage(TranslatableCaption.of("move.requires_unowned"));
075            }
076        });
077
078        return true;
079    }
080
081}