Search in sources :

Example 11 with Location

use of com.plotsquared.core.location.Location 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)

Example 12 with Location

use of com.plotsquared.core.location.Location in project PlotSquared by IntellectualSites.

the class Kick method tab.

@Override
public Collection<Command> tab(final PlotPlayer<?> player, final String[] args, final boolean space) {
    Location location = player.getLocation();
    Plot plot = location.getPlotAbs();
    if (plot == null) {
        return Collections.emptyList();
    }
    return TabCompletions.completePlayersInPlot(plot, String.join(",", args).trim(), Collections.singletonList(player.getName()));
}
Also used : Plot(com.plotsquared.core.plot.Plot) Location(com.plotsquared.core.location.Location)

Example 13 with Location

use of com.plotsquared.core.location.Location in project PlotSquared by IntellectualSites.

the class Claim method onCommand.

@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
    String schematic = null;
    if (args.length >= 1) {
        schematic = args[0];
    }
    Location location = player.getLocation();
    Plot plot = location.getPlotAbs();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return false;
    }
    final PlayerClaimPlotEvent event = this.eventDispatcher.callClaim(player, plot, schematic);
    schematic = event.getSchematic();
    if (event.getEventResult() == Result.DENY) {
        player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Claim"));
        return true;
    }
    boolean force = event.getEventResult() == Result.FORCE;
    int currentPlots = Settings.Limit.GLOBAL ? player.getPlotCount() : player.getPlotCount(location.getWorldName());
    final PlotArea area = plot.getArea();
    try (final MetaDataAccess<Integer> metaDataAccess = player.accessPersistentMetaData(PlayerMetaDataKeys.PERSISTENT_GRANTED_PLOTS)) {
        int grants = 0;
        if (currentPlots >= player.getAllowedPlots() && !force) {
            if (metaDataAccess.isPresent()) {
                grants = metaDataAccess.get().orElse(0);
                if (grants <= 0) {
                    player.sendMessage(TranslatableCaption.of("permission.cant_claim_more_plots"), Template.of("amount", String.valueOf(grants)));
                    metaDataAccess.remove();
                }
            } else {
                player.sendMessage(TranslatableCaption.of("permission.cant_claim_more_plots"), Template.of("amount", String.valueOf(player.getAllowedPlots())));
                return false;
            }
        }
        if (!plot.canClaim(player)) {
            player.sendMessage(TranslatableCaption.of("working.plot_is_claimed"));
            return false;
        }
        if (schematic != null && !schematic.isEmpty()) {
            if (area.isSchematicClaimSpecify()) {
                if (!area.hasSchematic(schematic)) {
                    player.sendMessage(TranslatableCaption.of("schematics.schematic_invalid_named"), Template.of("schemname", schematic), Template.of("reason", "non-existent"));
                }
                if (!Permissions.hasPermission(player, Permission.PERMISSION_CLAIM_SCHEMATIC.format(schematic)) && !Permissions.hasPermission(player, "plots.admin.command.schematic") && !force) {
                    player.sendMessage(TranslatableCaption.of("permission.no_schematic_permission"), Template.of("value", schematic));
                }
            }
        }
        if (this.econHandler.isEnabled(area) && !force) {
            PlotExpression costExr = area.getPrices().get("claim");
            double cost = costExr.evaluate(currentPlots);
            if (cost > 0d) {
                if (!this.econHandler.isSupported()) {
                    player.sendMessage(TranslatableCaption.of("economy.vault_or_consumer_null"));
                    return false;
                }
                if (this.econHandler.getMoney(player) < cost) {
                    player.sendMessage(TranslatableCaption.of("economy.cannot_afford_plot"), Template.of("money", this.econHandler.format(cost)), Template.of("balance", this.econHandler.format(this.econHandler.getMoney(player))));
                    return false;
                }
                this.econHandler.withdrawMoney(player, cost);
                player.sendMessage(TranslatableCaption.of("economy.removed_balance"), Template.of("money", this.econHandler.format(cost)), Template.of("balance", this.econHandler.format(this.econHandler.getMoney(player))));
            }
        }
        if (grants > 0) {
            if (grants == 1) {
                metaDataAccess.remove();
            } else {
                metaDataAccess.set(grants - 1);
            }
            player.sendMessage(TranslatableCaption.of("economy.removed_granted_plot"), Template.of("usedGrants", String.valueOf((grants - 1))), Template.of("remainingGrants", String.valueOf(grants)));
        }
    }
    if (!Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_BYPASS_BORDER)) {
        int border = area.getBorder();
        if (border != Integer.MAX_VALUE && plot.getDistanceFromOrigin() > border && !force) {
            player.sendMessage(TranslatableCaption.of("border.denied"));
            return false;
        }
    }
    plot.setOwnerAbs(player.getUUID());
    final String finalSchematic = schematic;
    DBFunc.createPlotSafe(plot, () -> {
        try {
            TaskManager.getPlatformImplementation().sync(() -> {
                if (!plot.claim(player, true, finalSchematic, false, false)) {
                    LOGGER.info("Failed to claim plot {}", plot.getId().toCommaSeparatedString());
                    player.sendMessage(TranslatableCaption.of("working.plot_not_claimed"));
                    plot.setOwnerAbs(null);
                } else if (area.isAutoMerge()) {
                    PlotMergeEvent mergeEvent = Claim.this.eventDispatcher.callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player);
                    if (mergeEvent.getEventResult() == Result.DENY) {
                        player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Auto merge on claim"));
                    } else {
                        if (plot.getPlotModificationManager().autoMerge(mergeEvent.getDir(), mergeEvent.getMax(), player.getUUID(), player, true)) {
                            eventDispatcher.callPostMerge(player, plot);
                        }
                    }
                }
                return null;
            });
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }, () -> {
        LOGGER.info("Failed to add plot to database: {}", plot.getId().toCommaSeparatedString());
        player.sendMessage(TranslatableCaption.of("working.plot_not_claimed"));
        plot.setOwnerAbs(null);
    });
    return true;
}
Also used : PlotArea(com.plotsquared.core.plot.PlotArea) Plot(com.plotsquared.core.plot.Plot) PlotExpression(com.plotsquared.core.util.PlotExpression) PlayerClaimPlotEvent(com.plotsquared.core.events.PlayerClaimPlotEvent) PlotMergeEvent(com.plotsquared.core.events.PlotMergeEvent) Location(com.plotsquared.core.location.Location)

Example 14 with Location

use of com.plotsquared.core.location.Location in project PlotSquared by IntellectualSites.

the class Target method onCommand.

@Override
public boolean onCommand(PlotPlayer<?> player, String[] args) {
    Location location = player.getLocation();
    if (!location.isPlotArea()) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
        return false;
    }
    if (args.length == 0) {
        player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", "/plot target <<X;Z> | nearest>"));
        return false;
    }
    Plot target = null;
    if (StringMan.isEqualIgnoreCaseToAny(args[0], "near", "nearest")) {
        int distance = Integer.MAX_VALUE;
        for (Plot plot : PlotQuery.newQuery().inWorld(location.getWorldName())) {
            double current = plot.getCenterSynchronous().getEuclideanDistanceSquared(location);
            if (current < distance) {
                distance = (int) current;
                target = plot;
            }
        }
        if (target == null) {
            player.sendMessage(TranslatableCaption.of("invalid.found_no_plots"));
            return false;
        }
    } else if ((target = Plot.getPlotFromString(player, args[0], true)) == null) {
        return false;
    }
    target.getCenter(player::setCompassTarget);
    player.sendMessage(TranslatableCaption.of("compass.compass_target"), Template.of("target", target.toString()));
    return true;
}
Also used : Plot(com.plotsquared.core.plot.Plot) Location(com.plotsquared.core.location.Location)

Example 15 with Location

use of com.plotsquared.core.location.Location in project PlotSquared by IntellectualSites.

the class SetCommand method onCommand.

@Override
public boolean onCommand(PlotPlayer<?> player, String[] args) {
    Location location = player.getLocation();
    Plot plot = location.getPlotAbs();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return false;
    }
    if (!plot.hasOwner()) {
        if (!Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND.format(getFullId()))) {
            player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", Permission.PERMISSION_ADMIN_COMMAND.format(getFullId())));
            player.sendMessage(TranslatableCaption.of("working.plot_not_claimed"));
            return false;
        }
    }
    if (!plot.isOwner(player.getUUID())) {
        if (!Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMMAND.format(getFullId()))) {
            player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", Permission.PERMISSION_ADMIN_COMMAND.format(getFullId())));
            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
            return false;
        }
    }
    if (args.length == 0) {
        return set(player, plot, "");
    }
    return set(player, plot, StringMan.join(args, " "));
}
Also used : Plot(com.plotsquared.core.plot.Plot) Location(com.plotsquared.core.location.Location)

Aggregations

Location (com.plotsquared.core.location.Location)82 Plot (com.plotsquared.core.plot.Plot)41 PlotArea (com.plotsquared.core.plot.PlotArea)26 EventHandler (org.bukkit.event.EventHandler)15 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)14 PlotPlayer (com.plotsquared.core.player.PlotPlayer)12 UUID (java.util.UUID)12 HashSet (java.util.HashSet)10 TranslatableCaption (com.plotsquared.core.configuration.caption.TranslatableCaption)9 NonNull (org.checkerframework.checker.nullness.qual.NonNull)9 QueueCoordinator (com.plotsquared.core.queue.QueueCoordinator)8 Caption (com.plotsquared.core.configuration.caption.Caption)7 BlockLoc (com.plotsquared.core.location.BlockLoc)7 Template (net.kyori.adventure.text.minimessage.Template)7 Entity (org.bukkit.entity.Entity)7 Settings (com.plotsquared.core.configuration.Settings)6 ClassicPlotWorld (com.plotsquared.core.generator.ClassicPlotWorld)6 PlotFlag (com.plotsquared.core.plot.flag.PlotFlag)6 SinglePlotArea (com.plotsquared.core.plot.world.SinglePlotArea)6 PlotSquared (com.plotsquared.core.PlotSquared)5