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 net.kyori.adventure.text.Component;
026import net.kyori.adventure.text.minimessage.tag.Tag;
027import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
028
029@CommandDeclaration(command = "copy",
030        permission = "plots.copy",
031        aliases = {"copypaste"},
032        category = CommandCategory.CLAIMING,
033        usage = "/plot copy <X;Z>",
034        requiredType = RequiredType.NONE)
035public class Copy extends SubCommand {
036
037    @Override
038    public boolean onCommand(final PlotPlayer<?> player, String[] args) {
039        Plot plot1 = player.getCurrentPlot();
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                    TagResolver.resolver("value", Tag.inserting(Component.text("/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(
071                        TranslatableCaption.of("move.copy_success"),
072                        TagResolver.builder()
073                                .tag("origin", Tag.inserting(Component.text(plot1.toString())))
074                                .tag("target", Tag.inserting(Component.text(plot2.toString())))
075                                .build()
076                );
077            } else {
078                player.sendMessage(TranslatableCaption.of("move.requires_unowned"));
079            }
080        });
081
082        return true;
083    }
084
085}