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