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.google.inject.Inject;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.events.PlotUnlinkEvent;
024import com.plotsquared.core.events.Result;
025import com.plotsquared.core.location.Location;
026import com.plotsquared.core.permissions.Permission;
027import com.plotsquared.core.player.PlotPlayer;
028import com.plotsquared.core.plot.Plot;
029import com.plotsquared.core.util.EventDispatcher;
030import com.plotsquared.core.util.StringMan;
031import com.plotsquared.core.util.task.TaskManager;
032import net.kyori.adventure.text.Component;
033import net.kyori.adventure.text.minimessage.tag.Tag;
034import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
035import org.checkerframework.checker.nullness.qual.NonNull;
036
037@CommandDeclaration(command = "unlink",
038        aliases = {"u", "unmerge"},
039        usage = "/plot unlink [createroads]",
040        requiredType = RequiredType.PLAYER,
041        category = CommandCategory.SETTINGS,
042        confirmation = true)
043public class Unlink extends SubCommand {
044
045    private final EventDispatcher eventDispatcher;
046
047    @Inject
048    public Unlink(final @NonNull EventDispatcher eventDispatcher) {
049        this.eventDispatcher = eventDispatcher;
050    }
051
052    @Override
053    public boolean onCommand(final PlotPlayer<?> player, String[] args) {
054        Location location = player.getLocation();
055        final Plot plot = location.getPlotAbs();
056        if (plot == null) {
057            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
058            return false;
059        }
060        if (!plot.hasOwner()) {
061            player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
062            return false;
063        }
064        if (plot.getVolume() > Integer.MAX_VALUE) {
065            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
066            return false;
067        }
068        if (!plot.isMerged()) {
069            player.sendMessage(TranslatableCaption.of("merge.unlink_impossible"));
070            return false;
071        }
072        final boolean createRoad;
073        if (args.length != 0) {
074            if (args.length != 1 || !StringMan.isEqualIgnoreCaseToAny(args[0], "true", "false")) {
075                sendUsage(player);
076                return false;
077            }
078            createRoad = Boolean.parseBoolean(args[0]);
079        } else {
080            createRoad = true;
081        }
082
083        PlotUnlinkEvent event = this.eventDispatcher
084                .callUnlink(plot.getArea(), plot, createRoad, createRoad,
085                        PlotUnlinkEvent.REASON.PLAYER_COMMAND
086                );
087        if (event.getEventResult() == Result.DENY) {
088            player.sendMessage(
089                    TranslatableCaption.of("events.event_denied"),
090                    TagResolver.resolver("value", Tag.inserting(Component.text("Unlink")))
091            );
092            return true;
093        }
094        boolean force = event.getEventResult() == Result.FORCE;
095        if (!force && !plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_UNLINK)) {
096            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
097            return true;
098        }
099        Runnable runnable = () -> {
100            if (!plot.getPlotModificationManager().unlinkPlot(createRoad, createRoad)) {
101                player.sendMessage(TranslatableCaption.of("merge.unmerge_cancelled"));
102                return;
103            }
104            player.sendMessage(TranslatableCaption.of("merge.unlink_success"));
105            eventDispatcher.callPostUnlink(plot, PlotUnlinkEvent.REASON.PLAYER_COMMAND);
106        };
107        if (hasConfirmation(player)) {
108            CmdConfirm.addPending(player, "/plot unlink " + plot.getId(), runnable);
109        } else {
110            TaskManager.runTask(runnable);
111        }
112        return true;
113    }
114
115}