Search in sources :

Example 1 with PlotUnlinkEvent

use of com.plotsquared.core.events.PlotUnlinkEvent in project PlotSquared by IntellectualSites.

the class EventDispatcher method callUnlink.

public PlotUnlinkEvent callUnlink(PlotArea area, Plot plot, boolean createRoad, boolean createSign, PlotUnlinkEvent.REASON reason) {
    PlotUnlinkEvent event = new PlotUnlinkEvent(area, plot, createRoad, createSign, reason);
    callEvent(event);
    return event;
}
Also used : PostPlotUnlinkEvent(com.plotsquared.core.events.post.PostPlotUnlinkEvent) PlotUnlinkEvent(com.plotsquared.core.events.PlotUnlinkEvent)

Example 2 with PlotUnlinkEvent

use of com.plotsquared.core.events.PlotUnlinkEvent in project PlotSquared by IntellectualSites.

the class Owner method set.

@Override
public boolean set(final PlotPlayer<?> player, final Plot plot, String value) {
    if (value == null || value.isEmpty()) {
        player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", "/plot setowner <owner>"));
        return false;
    }
    @Nullable final UUID oldOwner = plot.getOwnerAbs();
    Set<Plot> plots = plot.getConnectedPlots();
    final Consumer<UUID> uuidConsumer = uuid -> {
        if (uuid == null && !value.equalsIgnoreCase("none") && !value.equalsIgnoreCase("null") && !value.equalsIgnoreCase("-")) {
            player.sendMessage(TranslatableCaption.of("errors.invalid_player"), Template.of("value", value));
            return;
        }
        PlotChangeOwnerEvent event = this.eventDispatcher.callOwnerChange(player, plot, plot.hasOwner() ? plot.getOwnerAbs() : null, uuid, plot.hasOwner());
        if (event.getEventResult() == Result.DENY) {
            player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Owner change"));
            return;
        }
        uuid = event.getNewOwner();
        boolean force = event.getEventResult() == Result.FORCE;
        if (uuid == null) {
            if (!force && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_SET_OWNER, true)) {
                return;
            }
            PlotUnlinkEvent unlinkEvent = this.eventDispatcher.callUnlink(plot.getArea(), plot, false, false, PlotUnlinkEvent.REASON.NEW_OWNER);
            if (unlinkEvent.getEventResult() == Result.DENY) {
                player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Unlink on owner change"));
                return;
            }
            if (plot.getPlotModificationManager().unlinkPlot(unlinkEvent.isCreateRoad(), unlinkEvent.isCreateRoad())) {
                eventDispatcher.callPostUnlink(plot, PlotUnlinkEvent.REASON.NEW_OWNER);
            }
            Set<Plot> connected = plot.getConnectedPlots();
            for (Plot current : connected) {
                current.unclaim();
                current.getPlotModificationManager().removeSign();
            }
            eventDispatcher.callPostOwnerChange(player, plot, oldOwner);
            player.sendMessage(TranslatableCaption.of("owner.set_owner"));
            return;
        }
        final PlotPlayer<?> other = PlotSquared.platform().playerManager().getPlayerIfExists(uuid);
        if (plot.isOwner(uuid)) {
            player.sendMessage(TranslatableCaption.of("member.already_owner"), Template.of("player", PlayerManager.resolveName(uuid, false).getComponent(player)));
            return;
        }
        if (!force && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_SET_OWNER)) {
            if (other == null) {
                player.sendMessage(TranslatableCaption.of("errors.invalid_player_offline"), Template.of("player", PlayerManager.resolveName(uuid).getComponent(player)));
                return;
            }
            int size = plots.size();
            int currentPlots = (Settings.Limit.GLOBAL ? other.getPlotCount() : other.getPlotCount(plot.getWorldName())) + size;
            try (final MetaDataAccess<Integer> metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) {
                int grants;
                if (currentPlots >= other.getAllowedPlots()) {
                    if (metaDataAccess.isPresent()) {
                        grants = metaDataAccess.get().orElse(0);
                        if (grants <= 0) {
                            metaDataAccess.remove();
                            player.sendMessage(TranslatableCaption.of("permission.cant_transfer_more_plots"));
                            return;
                        }
                    }
                }
            }
        }
        final UUID finalUUID = uuid;
        PlotSquared.get().getImpromptuUUIDPipeline().getSingle(uuid, (finalName, throwable) -> {
            final boolean removeDenied = plot.isDenied(finalUUID);
            Runnable run = () -> {
                if (plot.setOwner(finalUUID, player)) {
                    if (removeDenied) {
                        plot.removeDenied(finalUUID);
                    }
                    plot.getPlotModificationManager().setSign(finalName);
                    player.sendMessage(TranslatableCaption.of("owner.set_owner"));
                    eventDispatcher.callPostOwnerChange(player, plot, oldOwner);
                    if (other != null) {
                        other.sendMessage(TranslatableCaption.of("owner.now_owner"), Template.of("plot", plot.getArea() + ";" + plot.getId()));
                    }
                } else {
                    player.sendMessage(TranslatableCaption.of("owner.set_owner_cancelled"));
                }
            };
            if (hasConfirmation(player)) {
                CmdConfirm.addPending(player, "/plot setowner " + value, run);
            } else {
                TaskManager.runTask(run);
            }
        });
    };
    if (value.length() == 36) {
        try {
            uuidConsumer.accept(UUID.fromString(value));
        } catch (Exception ignored) {
        }
    } else {
        PlotSquared.get().getImpromptuUUIDPipeline().getSingle(value, (uuid, throwable) -> uuidConsumer.accept(uuid));
    }
    return true;
}
Also used : MetaDataAccess(com.plotsquared.core.player.MetaDataAccess) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Inject(com.google.inject.Inject) Permission(com.plotsquared.core.permissions.Permission) PlotChangeOwnerEvent(com.plotsquared.core.events.PlotChangeOwnerEvent) Template(net.kyori.adventure.text.minimessage.Template) EventDispatcher(com.plotsquared.core.util.EventDispatcher) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Permissions(com.plotsquared.core.util.Permissions) Plot(com.plotsquared.core.plot.Plot) TaskManager(com.plotsquared.core.util.task.TaskManager) Collection(java.util.Collection) TabCompletions(com.plotsquared.core.util.TabCompletions) Set(java.util.Set) UUID(java.util.UUID) PlotUnlinkEvent(com.plotsquared.core.events.PlotUnlinkEvent) Result(com.plotsquared.core.events.Result) Consumer(java.util.function.Consumer) PlotSquared(com.plotsquared.core.PlotSquared) PlayerManager(com.plotsquared.core.util.PlayerManager) PlotPlayer(com.plotsquared.core.player.PlotPlayer) TranslatableCaption(com.plotsquared.core.configuration.caption.TranslatableCaption) Settings(com.plotsquared.core.configuration.Settings) PlayerMetaDataKeys(com.plotsquared.core.player.PlayerMetaDataKeys) Collections(java.util.Collections) Set(java.util.Set) Plot(com.plotsquared.core.plot.Plot) PlotPlayer(com.plotsquared.core.player.PlotPlayer) PlotUnlinkEvent(com.plotsquared.core.events.PlotUnlinkEvent) UUID(java.util.UUID) PlotChangeOwnerEvent(com.plotsquared.core.events.PlotChangeOwnerEvent) MetaDataAccess(com.plotsquared.core.player.MetaDataAccess) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 3 with PlotUnlinkEvent

use of com.plotsquared.core.events.PlotUnlinkEvent in project PlotSquared by IntellectualSites.

the class Unlink method onCommand.

@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
    Location location = player.getLocation();
    final Plot plot = location.getPlotAbs();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return false;
    }
    if (!plot.hasOwner()) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return false;
    }
    if (plot.getVolume() > Integer.MAX_VALUE) {
        player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
        return false;
    }
    if (!plot.isMerged()) {
        player.sendMessage(TranslatableCaption.of("merge.unlink_impossible"));
        return false;
    }
    final boolean createRoad;
    if (args.length != 0) {
        if (args.length != 1 || !StringMan.isEqualIgnoreCaseToAny(args[0], "true", "false")) {
            sendUsage(player);
            return false;
        }
        createRoad = Boolean.parseBoolean(args[0]);
    } else {
        createRoad = true;
    }
    PlotUnlinkEvent event = this.eventDispatcher.callUnlink(plot.getArea(), plot, createRoad, createRoad, PlotUnlinkEvent.REASON.PLAYER_COMMAND);
    if (event.getEventResult() == Result.DENY) {
        player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Unlink"));
        return true;
    }
    boolean force = event.getEventResult() == Result.FORCE;
    if (!force && !plot.isOwner(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND_UNLINK)) {
        player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
        return true;
    }
    Runnable runnable = () -> {
        if (!plot.getPlotModificationManager().unlinkPlot(createRoad, createRoad)) {
            player.sendMessage(TranslatableCaption.of("merge.unmerge_cancelled"));
            return;
        }
        player.sendMessage(TranslatableCaption.of("merge.unlink_success"));
        eventDispatcher.callPostUnlink(plot, PlotUnlinkEvent.REASON.PLAYER_COMMAND);
    };
    if (hasConfirmation(player)) {
        CmdConfirm.addPending(player, "/plot unlink " + plot.getId(), runnable);
    } else {
        TaskManager.runTask(runnable);
    }
    return true;
}
Also used : PlotUnlinkEvent(com.plotsquared.core.events.PlotUnlinkEvent) Plot(com.plotsquared.core.plot.Plot) Location(com.plotsquared.core.location.Location)

Example 4 with PlotUnlinkEvent

use of com.plotsquared.core.events.PlotUnlinkEvent in project PlotSquared by IntellectualSites.

the class PlotModificationManager method clear.

/**
 * Clear the plot
 *
 * <p>
 * Use {@link #deletePlot(PlotPlayer, Runnable)} to clear and delete a plot
 * </p>
 *
 * @param checkRunning Whether or not already executing tasks should be checked
 * @param isDelete     Whether or not the plot is being deleted
 * @param actor        The actor clearing the plot
 * @param whenDone     A runnable to execute when clearing finishes, or null
 */
public boolean clear(final boolean checkRunning, final boolean isDelete, @Nullable final PlotPlayer<?> actor, @Nullable final Runnable whenDone) {
    if (checkRunning && this.plot.getRunning() != 0) {
        return false;
    }
    final Set<CuboidRegion> regions = this.plot.getRegions();
    final Set<Plot> plots = this.plot.getConnectedPlots();
    final ArrayDeque<Plot> queue = new ArrayDeque<>(plots);
    if (isDelete) {
        this.removeSign();
    }
    PlotUnlinkEvent event = PlotSquared.get().getEventDispatcher().callUnlink(this.plot.getArea(), this.plot, true, !isDelete, isDelete ? PlotUnlinkEvent.REASON.DELETE : PlotUnlinkEvent.REASON.CLEAR);
    if (event.getEventResult() != Result.DENY && this.unlinkPlot(event.isCreateRoad(), event.isCreateSign())) {
        PlotSquared.get().getEventDispatcher().callPostUnlink(plot, event.getReason());
    }
    final PlotManager manager = this.plot.getArea().getPlotManager();
    Runnable run = new Runnable() {

        @Override
        public void run() {
            if (queue.isEmpty()) {
                Runnable run = () -> {
                    for (CuboidRegion region : regions) {
                        Location[] corners = Plot.getCorners(plot.getWorldName(), region);
                        PlotSquared.platform().regionManager().clearAllEntities(corners[0], corners[1]);
                    }
                    TaskManager.runTask(whenDone);
                };
                QueueCoordinator queue = plot.getArea().getQueue();
                for (Plot current : plots) {
                    if (isDelete || !current.hasOwner()) {
                        manager.unClaimPlot(current, null, queue);
                    } else {
                        manager.claimPlot(current, queue);
                    }
                }
                if (queue.size() > 0) {
                    queue.setCompleteTask(run);
                    queue.enqueue();
                    return;
                }
                run.run();
                return;
            }
            Plot current = queue.poll();
            if (plot.getArea().getTerrain() != PlotAreaTerrainType.NONE) {
                try {
                    PlotSquared.platform().regionManager().regenerateRegion(current.getBottomAbs(), current.getTopAbs(), false, this);
                } catch (UnsupportedOperationException exception) {
                    exception.printStackTrace();
                    return;
                }
                return;
            }
            manager.clearPlot(current, this, actor, null);
        }
    };
    run.run();
    return true;
}
Also used : PlotUnlinkEvent(com.plotsquared.core.events.PlotUnlinkEvent) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) QueueCoordinator(com.plotsquared.core.queue.QueueCoordinator) ArrayDeque(java.util.ArrayDeque)

Aggregations

PlotUnlinkEvent (com.plotsquared.core.events.PlotUnlinkEvent)4 Plot (com.plotsquared.core.plot.Plot)2 Inject (com.google.inject.Inject)1 PlotSquared (com.plotsquared.core.PlotSquared)1 Settings (com.plotsquared.core.configuration.Settings)1 TranslatableCaption (com.plotsquared.core.configuration.caption.TranslatableCaption)1 PlotChangeOwnerEvent (com.plotsquared.core.events.PlotChangeOwnerEvent)1 Result (com.plotsquared.core.events.Result)1 PostPlotUnlinkEvent (com.plotsquared.core.events.post.PostPlotUnlinkEvent)1 Location (com.plotsquared.core.location.Location)1 Permission (com.plotsquared.core.permissions.Permission)1 MetaDataAccess (com.plotsquared.core.player.MetaDataAccess)1 PlayerMetaDataKeys (com.plotsquared.core.player.PlayerMetaDataKeys)1 PlotPlayer (com.plotsquared.core.player.PlotPlayer)1 QueueCoordinator (com.plotsquared.core.queue.QueueCoordinator)1 EventDispatcher (com.plotsquared.core.util.EventDispatcher)1 Permissions (com.plotsquared.core.util.Permissions)1 PlayerManager (com.plotsquared.core.util.PlayerManager)1 TabCompletions (com.plotsquared.core.util.TabCompletions)1 TaskManager (com.plotsquared.core.util.task.TaskManager)1