Search in sources :

Example 1 with RunnableVal3

use of com.plotsquared.core.util.task.RunnableVal3 in project PlotSquared by IntellectualSites.

the class MainCommand method onCommand.

public static boolean onCommand(final PlotPlayer<?> player, String... args) {
    final EconHandler econHandler = PlotSquared.platform().econHandler();
    if (args.length >= 1 && args[0].contains(":")) {
        String[] split2 = args[0].split(":");
        if (split2.length == 2) {
            // Ref: c:v, this will push value to the last spot in the array
            // ex. /p h:2 SomeUsername
            // > /p h SomeUsername 2
            String[] tmp = new String[args.length + 1];
            tmp[0] = split2[0];
            tmp[args.length] = split2[1];
            if (args.length >= 2) {
                System.arraycopy(args, 1, tmp, 1, args.length - 1);
            }
            args = tmp;
        }
    }
    try {
        getInstance().execute(player, args, new RunnableVal3<>() {

            @Override
            public void run(final Command cmd, final Runnable success, final Runnable failure) {
                if (cmd.hasConfirmation(player)) {
                    CmdConfirm.addPending(player, cmd.getUsage(), () -> {
                        PlotArea area = player.getApplicablePlotArea();
                        if (area != null && econHandler.isEnabled(area)) {
                            PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
                            double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
                            if (econHandler.getMoney(player) < price) {
                                if (failure != null) {
                                    failure.run();
                                }
                                return;
                            }
                        }
                        if (success != null) {
                            success.run();
                        }
                    });
                    return;
                }
                PlotArea area = player.getApplicablePlotArea();
                if (area != null && econHandler.isEnabled(area)) {
                    PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
                    double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
                    if (price != 0d && econHandler.getMoney(player) < price) {
                        if (failure != null) {
                            failure.run();
                        }
                        return;
                    }
                }
                if (success != null) {
                    success.run();
                }
            }
        }, new RunnableVal2<>() {

            @Override
            public void run(Command cmd, CommandResult result) {
            // Post command stuff!?
            }
        }).thenAccept(result -> {
        // TODO: Something with the command result
        });
    } catch (CommandException e) {
        e.perform(player);
    }
    // Always true
    return true;
}
Also used : EconHandler(com.plotsquared.core.util.EconHandler) PlotExpression(com.plotsquared.core.util.PlotExpression) SinglePlotArea(com.plotsquared.core.plot.world.SinglePlotArea) PlotArea(com.plotsquared.core.plot.PlotArea) RunnableVal2(com.plotsquared.core.util.task.RunnableVal2) RunnableVal3(com.plotsquared.core.util.task.RunnableVal3)

Example 2 with RunnableVal3

use of com.plotsquared.core.util.task.RunnableVal3 in project PlotSquared by IntellectualSites.

the class Clear method execute.

@Override
public CompletableFuture<Boolean> execute(final PlotPlayer<?> player, String[] args, RunnableVal3<Command, Runnable, Runnable> confirm, RunnableVal2<Command, CommandResult> whenDone) throws CommandException {
    if (args.length != 0) {
        sendUsage(player);
        return CompletableFuture.completedFuture(false);
    }
    final Plot plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
    Result eventResult = this.eventDispatcher.callClear(plot).getEventResult();
    if (eventResult == Result.DENY) {
        player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Clear"));
        return CompletableFuture.completedFuture(true);
    }
    if (plot.getVolume() > Integer.MAX_VALUE) {
        player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
        return CompletableFuture.completedFuture(true);
    }
    boolean force = eventResult == Result.FORCE;
    checkTrue(force || plot.isOwner(player.getUUID()) || Permissions.hasPermission(player, "plots.admin.command.clear"), TranslatableCaption.of("permission.no_plot_perms"));
    checkTrue(plot.getRunning() == 0, TranslatableCaption.of("errors.wait_for_timer"));
    checkTrue(force || !Settings.Done.RESTRICT_BUILDING || !DoneFlag.isDone(plot) || Permissions.hasPermission(player, "plots.continue"), TranslatableCaption.of("done.done_already_done"));
    confirm.run(this, () -> {
        if (Settings.Teleport.ON_CLEAR) {
            plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND_CLEAR, result -> {
            }));
        }
        BackupManager.backup(player, plot, () -> {
            final long start = System.currentTimeMillis();
            boolean result = plot.getPlotModificationManager().clear(true, false, player, () -> {
                plot.getPlotModificationManager().unlink();
                TaskManager.runTask(() -> {
                    plot.removeRunning();
                    // If the state changes, then mark it as no longer done
                    if (DoneFlag.isDone(plot)) {
                        PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(DoneFlag.class);
                        PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
                        if (event.getEventResult() != Result.DENY) {
                            plot.removeFlag(event.getFlag());
                        }
                    }
                    if (!plot.getFlag(AnalysisFlag.class).isEmpty()) {
                        PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(AnalysisFlag.class);
                        PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
                        if (event.getEventResult() != Result.DENY) {
                            plot.removeFlag(event.getFlag());
                        }
                    }
                    player.sendMessage(TranslatableCaption.of("working.clearing_done"), Template.of("amount", String.valueOf(System.currentTimeMillis() - start)), Template.of("plot", plot.getId().toString()));
                });
            });
            if (!result) {
                player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
            } else {
                plot.addRunning();
            }
        });
    }, null);
    return CompletableFuture.completedFuture(true);
}
Also used : TeleportCause(com.plotsquared.core.events.TeleportCause) GlobalBlockQueue(com.plotsquared.core.queue.GlobalBlockQueue) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent) Permissions(com.plotsquared.core.util.Permissions) NonNull(org.checkerframework.checker.nullness.qual.NonNull) Plot(com.plotsquared.core.plot.Plot) Inject(com.google.inject.Inject) TaskManager(com.plotsquared.core.util.task.TaskManager) CompletableFuture(java.util.concurrent.CompletableFuture) PlotFlag(com.plotsquared.core.plot.flag.PlotFlag) DoneFlag(com.plotsquared.core.plot.flag.implementations.DoneFlag) BackupManager(com.plotsquared.core.backup.BackupManager) Result(com.plotsquared.core.events.Result) RunnableVal3(com.plotsquared.core.util.task.RunnableVal3) PlotPlayer(com.plotsquared.core.player.PlotPlayer) TranslatableCaption(com.plotsquared.core.configuration.caption.TranslatableCaption) RunnableVal2(com.plotsquared.core.util.task.RunnableVal2) Settings(com.plotsquared.core.configuration.Settings) AnalysisFlag(com.plotsquared.core.plot.flag.implementations.AnalysisFlag) Template(net.kyori.adventure.text.minimessage.Template) EventDispatcher(com.plotsquared.core.util.EventDispatcher) Plot(com.plotsquared.core.plot.Plot) PlotFlagRemoveEvent(com.plotsquared.core.events.PlotFlagRemoveEvent) Result(com.plotsquared.core.events.Result)

Aggregations

RunnableVal2 (com.plotsquared.core.util.task.RunnableVal2)2 RunnableVal3 (com.plotsquared.core.util.task.RunnableVal3)2 Inject (com.google.inject.Inject)1 BackupManager (com.plotsquared.core.backup.BackupManager)1 Settings (com.plotsquared.core.configuration.Settings)1 TranslatableCaption (com.plotsquared.core.configuration.caption.TranslatableCaption)1 PlotFlagRemoveEvent (com.plotsquared.core.events.PlotFlagRemoveEvent)1 Result (com.plotsquared.core.events.Result)1 TeleportCause (com.plotsquared.core.events.TeleportCause)1 PlotPlayer (com.plotsquared.core.player.PlotPlayer)1 Plot (com.plotsquared.core.plot.Plot)1 PlotArea (com.plotsquared.core.plot.PlotArea)1 PlotFlag (com.plotsquared.core.plot.flag.PlotFlag)1 AnalysisFlag (com.plotsquared.core.plot.flag.implementations.AnalysisFlag)1 DoneFlag (com.plotsquared.core.plot.flag.implementations.DoneFlag)1 SinglePlotArea (com.plotsquared.core.plot.world.SinglePlotArea)1 GlobalBlockQueue (com.plotsquared.core.queue.GlobalBlockQueue)1 EconHandler (com.plotsquared.core.util.EconHandler)1 EventDispatcher (com.plotsquared.core.util.EventDispatcher)1 Permissions (com.plotsquared.core.util.Permissions)1