Search in sources :

Example 1 with ConsolePlayer

use of com.plotsquared.core.player.ConsolePlayer in project PlotSquared by IntellectualSites.

the class MainCommand method execute.

@Override
public CompletableFuture<Boolean> execute(final PlotPlayer<?> player, String[] args, RunnableVal3<Command, Runnable, Runnable> confirm, RunnableVal2<Command, CommandResult> whenDone) {
    // Optional command scope //
    Location location = null;
    Plot plot = null;
    boolean tp = false;
    if (args.length >= 2) {
        PlotArea area = player.getApplicablePlotArea();
        Plot newPlot = Plot.fromString(area, args[0]);
        if (newPlot != null && (player instanceof ConsolePlayer || newPlot.getArea().equals(area) || Permissions.hasPermission(player, Permission.PERMISSION_ADMIN) || Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_AREA_SUDO)) && !newPlot.isDenied(player.getUUID())) {
            final Location newLoc;
            if (newPlot.getArea() instanceof SinglePlotArea) {
                newLoc = newPlot.isLoaded() ? newPlot.getCenterSynchronous() : Location.at("", 0, 0, 0);
            } else {
                newLoc = newPlot.getCenterSynchronous();
            }
            if (player.canTeleport(newLoc)) {
                // Save meta
                try (final MetaDataAccess<Location> locationMetaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LOCATION)) {
                    location = locationMetaDataAccess.get().orElse(null);
                    locationMetaDataAccess.set(newLoc);
                }
                try (final MetaDataAccess<Plot> plotMetaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
                    plot = plotMetaDataAccess.get().orElse(null);
                    plotMetaDataAccess.set(newPlot);
                }
                tp = true;
            } else {
                player.sendMessage(TranslatableCaption.of("border.denied"));
            }
            // Trim command
            args = Arrays.copyOfRange(args, 1, args.length);
        }
        if (args.length >= 2 && !args[0].isEmpty() && args[0].charAt(0) == '-') {
            if ("f".equals(args[0].substring(1))) {
                confirm = new RunnableVal3<>() {

                    @Override
                    public void run(Command cmd, Runnable success, Runnable failure) {
                        if (area != null && PlotSquared.platform().econHandler().isEnabled(area)) {
                            PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
                            double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
                            if (price != 0d && PlotSquared.platform().econHandler().getMoney(player) < price) {
                                if (failure != null) {
                                    failure.run();
                                }
                                return;
                            }
                        }
                        if (success != null) {
                            success.run();
                        }
                    }
                };
                args = Arrays.copyOfRange(args, 1, args.length);
            } else {
                player.sendMessage(TranslatableCaption.of("errors.invalid_command_flag"));
                return CompletableFuture.completedFuture(false);
            }
        }
    }
    try {
        super.execute(player, args, confirm, whenDone);
    } catch (CommandException e) {
        throw e;
    } catch (Throwable e) {
        e.printStackTrace();
        String message = e.getMessage();
        if (message != null) {
            player.sendMessage(TranslatableCaption.of("errors.error"), net.kyori.adventure.text.minimessage.Template.of("value", message));
        } else {
            player.sendMessage(TranslatableCaption.of("errors.error_console"));
        }
    }
    // Reset command scope //
    if (tp && !(player instanceof ConsolePlayer)) {
        try (final MetaDataAccess<Location> locationMetaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LOCATION)) {
            if (location == null) {
                locationMetaDataAccess.remove();
            } else {
                locationMetaDataAccess.set(location);
            }
        }
        try (final MetaDataAccess<Plot> plotMetaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_LAST_PLOT)) {
            if (plot == null) {
                plotMetaDataAccess.remove();
            } else {
                plotMetaDataAccess.set(plot);
            }
        }
    }
    return CompletableFuture.completedFuture(true);
}
Also used : SinglePlotArea(com.plotsquared.core.plot.world.SinglePlotArea) PlotArea(com.plotsquared.core.plot.PlotArea) Plot(com.plotsquared.core.plot.Plot) ConsolePlayer(com.plotsquared.core.player.ConsolePlayer) SinglePlotArea(com.plotsquared.core.plot.world.SinglePlotArea) PlotExpression(com.plotsquared.core.util.PlotExpression) Location(com.plotsquared.core.location.Location)

Aggregations

Location (com.plotsquared.core.location.Location)1 ConsolePlayer (com.plotsquared.core.player.ConsolePlayer)1 Plot (com.plotsquared.core.plot.Plot)1 PlotArea (com.plotsquared.core.plot.PlotArea)1 SinglePlotArea (com.plotsquared.core.plot.world.SinglePlotArea)1 PlotExpression (com.plotsquared.core.util.PlotExpression)1