Search in sources :

Example 1 with PlotAutoMergeEvent

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

the class EventDispatcher method callAutoMerge.

public PlotAutoMergeEvent callAutoMerge(Plot plot, List<PlotId> plots) {
    PlotAutoMergeEvent event = new PlotAutoMergeEvent(plot.getWorldName(), plot, plots);
    callEvent(event);
    return event;
}
Also used : PlotAutoMergeEvent(com.plotsquared.core.events.PlotAutoMergeEvent)

Example 2 with PlotAutoMergeEvent

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

the class Auto method onCommand.

@Override
public boolean onCommand(final PlotPlayer<?> player, String[] args) {
    PlotArea plotarea = player.getApplicablePlotArea();
    if (plotarea == null) {
        final PermissionHandler permissionHandler = PlotSquared.platform().permissionHandler();
        if (permissionHandler.hasCapability(PermissionHandler.PermissionHandlerCapability.PER_WORLD_PERMISSIONS)) {
            for (final PlotArea area : this.plotAreaManager.getAllPlotAreas()) {
                if (player.hasPermission(area.getWorldName(), "plots.auto")) {
                    if (plotarea != null) {
                        plotarea = null;
                        break;
                    }
                    plotarea = area;
                }
            }
        }
        if (this.plotAreaManager.getAllPlotAreas().length == 1) {
            plotarea = this.plotAreaManager.getAllPlotAreas()[0];
        }
        if (plotarea == null) {
            player.sendMessage(TranslatableCaption.of("errors.not_in_plot_world"));
            return false;
        }
    }
    int sizeX = 1;
    int sizeZ = 1;
    String schematic = null;
    boolean mega = false;
    if (args.length > 0) {
        try {
            String[] split = args[0].split("[,;]");
            if (split.length == 2) {
                sizeX = Integer.parseInt(split[0]);
                sizeZ = Integer.parseInt(split[1]);
            } else {
                player.sendMessage(TranslatableCaption.of("commandconfig.command_syntax"), Template.of("value", getUsage()));
                return true;
            }
            if (sizeX < 1 || sizeZ < 1) {
                player.sendMessage(TranslatableCaption.of("error.plot_size_negative"));
                return true;
            }
            if (args.length > 1) {
                schematic = args[1];
            }
            mega = true;
        } catch (NumberFormatException ignored) {
            sizeX = 1;
            sizeZ = 1;
            schematic = args[0];
        }
    }
    PlayerAutoPlotEvent event = this.eventDispatcher.callAuto(player, plotarea, schematic, sizeX, sizeZ);
    if (event.getEventResult() == Result.DENY) {
        player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Auto claim"));
        return true;
    }
    boolean force = event.getEventResult() == Result.FORCE;
    sizeX = event.getSizeX();
    sizeZ = event.getSizeZ();
    schematic = event.getSchematic();
    if (!force && mega && !Permissions.hasPermission(player, Permission.PERMISSION_AUTO_MEGA)) {
        player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", String.valueOf(Permission.PERMISSION_AUTO_MEGA)));
    }
    if (!force && sizeX * sizeZ > Settings.Claim.MAX_AUTO_AREA) {
        player.sendMessage(TranslatableCaption.of("permission.cant_claim_more_plots_num"), Template.of("amount", String.valueOf(Settings.Claim.MAX_AUTO_AREA)));
        return false;
    }
    final int allowed_plots = player.getAllowedPlots();
    try (final MetaDataAccess<Boolean> metaDataAccess = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_AUTO)) {
        if (!force && (metaDataAccess.get().orElse(false) || !checkAllowedPlots(player, plotarea, allowed_plots, sizeX, sizeZ))) {
            return false;
        }
    }
    if (schematic != null && !schematic.isEmpty()) {
        if (!plotarea.hasSchematic(schematic)) {
            player.sendMessage(TranslatableCaption.of("schematics.schematic_invalid_named"), Template.of("schemname", schematic), Template.of("reason", "non-existent"));
            return true;
        }
        if (!force && !Permissions.hasPermission(player, Permission.PERMISSION_CLAIM_SCHEMATIC.format(schematic)) && !Permissions.hasPermission(player, "plots.admin.command.schematic")) {
            player.sendMessage(TranslatableCaption.of("permission.no_permission"), Template.of("node", "plots.claim.%s0"));
            return true;
        }
    }
    if (this.econHandler != null && plotarea.useEconomy()) {
        PlotExpression costExp = plotarea.getPrices().get("claim");
        double cost = costExp.evaluate(Settings.Limit.GLOBAL ? player.getPlotCount() : player.getPlotCount(plotarea.getWorldName()));
        cost = (sizeX * sizeZ) * cost;
        if (cost > 0d) {
            if (!this.econHandler.isSupported()) {
                player.sendMessage(TranslatableCaption.of("economy.vault_or_consumer_null"));
                return false;
            }
            if (!force && 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)));
        }
    }
    List<Plot> plots = this.servicePipeline.pump(new AutoService.AutoQuery(player, null, sizeX, sizeZ, plotarea)).through(AutoService.class).getResult();
    plots = this.eventDispatcher.callAutoPlotsChosen(player, plots).getPlots();
    if (plots.isEmpty()) {
        player.sendMessage(TranslatableCaption.of("errors.no_free_plots"));
        return false;
    } else if (plots.size() == 1) {
        this.claimSingle(player, plots.get(0), plotarea, schematic);
    } else {
        final Iterator<Plot> plotIterator = plots.iterator();
        while (plotIterator.hasNext()) {
            Plot plot = plotIterator.next();
            if (!plot.canClaim(player)) {
                continue;
            }
            plot.claim(player, !plotIterator.hasNext(), null, true, true);
            eventDispatcher.callPostAuto(player, plot);
        }
        final PlotAutoMergeEvent mergeEvent = this.eventDispatcher.callAutoMerge(plots.get(0), plots.stream().map(Plot::getId).collect(Collectors.toList()));
        if (!force && mergeEvent.getEventResult() == Result.DENY) {
            player.sendMessage(TranslatableCaption.of("events.event_denied"), Template.of("value", "Auto merge"));
            return false;
        }
        return plotarea.mergePlots(mergeEvent.getPlots(), true);
    }
    return true;
}
Also used : PlotArea(com.plotsquared.core.plot.PlotArea) PermissionHandler(com.plotsquared.core.permissions.PermissionHandler) AutoService(com.plotsquared.core.services.plots.AutoService) Plot(com.plotsquared.core.plot.Plot) PlayerAutoPlotEvent(com.plotsquared.core.events.PlayerAutoPlotEvent) PlotExpression(com.plotsquared.core.util.PlotExpression) PlotAutoMergeEvent(com.plotsquared.core.events.PlotAutoMergeEvent) Iterator(java.util.Iterator)

Aggregations

PlotAutoMergeEvent (com.plotsquared.core.events.PlotAutoMergeEvent)2 PlayerAutoPlotEvent (com.plotsquared.core.events.PlayerAutoPlotEvent)1 PermissionHandler (com.plotsquared.core.permissions.PermissionHandler)1 Plot (com.plotsquared.core.plot.Plot)1 PlotArea (com.plotsquared.core.plot.PlotArea)1 AutoService (com.plotsquared.core.services.plots.AutoService)1 PlotExpression (com.plotsquared.core.util.PlotExpression)1 Iterator (java.util.Iterator)1