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