Search in sources :

Example 21 with Pattern

use of com.sk89q.worldedit.function.pattern.Pattern in project PlotSquared by IntellectualSites.

the class ComponentPresetManager method buildInventory.

/**
 * Build the component inventory for a player. This also checks
 * if the player is in a compatible plot, and sends appropriate
 * error messages if not
 *
 * @param player player
 * @return Build inventory, if it could be created
 */
@Nullable
public PlotInventory buildInventory(final PlotPlayer<?> player) {
    final Plot plot = player.getCurrentPlot();
    if (plot == null) {
        player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
        return null;
    } else if (!plot.hasOwner()) {
        player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
        return null;
    } else if (!plot.isOwner(player.getUUID()) && !plot.getTrusted().contains(player.getUUID()) && !Permissions.hasPermission(player, Permission.PERMISSION_ADMIN_COMPONENTS_OTHER)) {
        player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
        return null;
    } else if (plot.getVolume() > Integer.MAX_VALUE) {
        player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
        return null;
    }
    final List<ComponentPreset> allowedPresets = new ArrayList<>(this.presets.size());
    for (final ComponentPreset componentPreset : this.presets) {
        if (!componentPreset.getPermission().isEmpty() && !Permissions.hasPermission(player, componentPreset.getPermission())) {
            continue;
        }
        allowedPresets.add(componentPreset);
    }
    if (allowedPresets.isEmpty()) {
        player.sendMessage(TranslatableCaption.of("preset.empty"));
        return null;
    }
    final int size = (int) Math.ceil((double) allowedPresets.size() / 9.0D);
    final PlotInventory plotInventory = new PlotInventory(this.inventoryUtil, player, size, TranslatableCaption.of("preset.title").getComponent(player)) {

        @Override
        public boolean onClick(final int index) {
            if (!getPlayer().getCurrentPlot().equals(plot)) {
                return false;
            }
            if (index < 0 || index >= allowedPresets.size()) {
                return false;
            }
            final ComponentPreset componentPreset = allowedPresets.get(index);
            if (componentPreset == null) {
                return false;
            }
            if (plot.getRunning() > 0) {
                getPlayer().sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
                return false;
            }
            final Pattern pattern = PatternUtil.parse(null, componentPreset.getPattern(), false);
            if (pattern == null) {
                getPlayer().sendMessage(TranslatableCaption.of("preset.preset_invalid"));
                return false;
            }
            if (componentPreset.getCost() > 0.0D) {
                if (!econHandler.isEnabled(plot.getArea())) {
                    getPlayer().sendMessage(TranslatableCaption.of("preset.economy_disabled"), Template.of("preset", componentPreset.getDisplayName()));
                    return false;
                }
                if (econHandler.getMoney(getPlayer()) < componentPreset.getCost()) {
                    getPlayer().sendMessage(TranslatableCaption.of("preset.preset_cannot_afford"));
                    return false;
                } else {
                    econHandler.withdrawMoney(getPlayer(), componentPreset.getCost());
                    getPlayer().sendMessage(TranslatableCaption.of("economy.removed_balance"), Template.of("money", econHandler.format(componentPreset.getCost())));
                }
            }
            BackupManager.backup(getPlayer(), plot, () -> {
                plot.addRunning();
                QueueCoordinator queue = plot.getArea().getQueue();
                queue.setCompleteTask(plot::removeRunning);
                for (Plot current : plot.getConnectedPlots()) {
                    current.getPlotModificationManager().setComponent(componentPreset.getComponent().name(), pattern, player, queue);
                }
                queue.enqueue();
                getPlayer().sendMessage(TranslatableCaption.of("working.generating_component"));
            });
            return false;
        }
    };
    for (int i = 0; i < allowedPresets.size(); i++) {
        final ComponentPreset preset = allowedPresets.get(i);
        final List<String> lore = new ArrayList<>();
        if (preset.getCost() > 0) {
            if (!this.econHandler.isEnabled(plot.getArea())) {
                lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_economy_disabled").getComponent(player))));
            } else {
                lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_cost").getComponent(player), Template.of("cost", String.format("%.2f", preset.getCost())))));
            }
        }
        lore.add(MINI_MESSAGE.serialize(MINI_MESSAGE.parse(TranslatableCaption.of("preset.preset_lore_component").getComponent(player), Template.of("component", preset.getComponent().name().toLowerCase()), Template.of("prefix", TranslatableCaption.of("core.prefix").getComponent(player)))));
        lore.removeIf(String::isEmpty);
        lore.addAll(preset.getDescription());
        plotInventory.setItem(i, new PlotItemStack(preset.getIcon().getId().replace("minecraft:", ""), 1, preset.getDisplayName(), lore.toArray(new String[0])));
    }
    return plotInventory;
}
Also used : Pattern(com.sk89q.worldedit.function.pattern.Pattern) Plot(com.plotsquared.core.plot.Plot) ArrayList(java.util.ArrayList) PlotItemStack(com.plotsquared.core.plot.PlotItemStack) QueueCoordinator(com.plotsquared.core.queue.QueueCoordinator) PlotInventory(com.plotsquared.core.plot.PlotInventory) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 22 with Pattern

use of com.sk89q.worldedit.function.pattern.Pattern in project PlotSquared by IntellectualSites.

the class HybridPlotManager method clearPlot.

@Override
public boolean clearPlot(@NonNull final Plot plot, @Nullable final Runnable whenDone, @Nullable PlotPlayer<?> actor, @Nullable QueueCoordinator queue) {
    if (this.regionManager.notifyClear(this)) {
        // If this returns false, the clear didn't work
        if (this.regionManager.handleClear(plot, whenDone, this, actor)) {
            return true;
        }
    }
    final Location pos1 = plot.getBottomAbs();
    final Location pos2 = plot.getExtendedTopAbs();
    // If augmented
    final boolean canRegen = (hybridPlotWorld.getType() == PlotAreaType.AUGMENTED) && (hybridPlotWorld.getTerrain() != PlotAreaTerrainType.NONE) && REGENERATIVE_CLEAR;
    // The component blocks
    final Pattern plotfloor = hybridPlotWorld.TOP_BLOCK.toPattern();
    final Pattern filling = hybridPlotWorld.MAIN_BLOCK.toPattern();
    final Pattern bedrock;
    if (hybridPlotWorld.PLOT_BEDROCK) {
        bedrock = BlockTypes.BEDROCK.getDefaultState();
    } else {
        bedrock = hybridPlotWorld.MAIN_BLOCK.toPattern();
    }
    final BiomeType biome = hybridPlotWorld.getPlotBiome();
    boolean enqueue = false;
    if (queue == null) {
        enqueue = true;
        queue = hybridPlotWorld.getQueue();
    }
    if (actor != null && Settings.QUEUE.NOTIFY_PROGRESS) {
        queue.addProgressSubscriber(subscriberFactory.createWithActor(actor));
    }
    if (whenDone != null) {
        queue.setCompleteTask(whenDone);
    }
    if (!canRegen) {
        queue.setCuboid(pos1.withY(hybridPlotWorld.getMinGenHeight()), pos2.withY(hybridPlotWorld.getMinGenHeight()), hybridPlotWorld.PLOT_BEDROCK ? bedrock : filling);
        // Each component has a different layer
        queue.setCuboid(pos1.withY(hybridPlotWorld.getMinGenHeight() + 1), pos2.withY(hybridPlotWorld.PLOT_HEIGHT - 1), filling);
        queue.setCuboid(pos1.withY(hybridPlotWorld.PLOT_HEIGHT), pos2.withY(hybridPlotWorld.PLOT_HEIGHT), plotfloor);
        queue.setCuboid(pos1.withY(hybridPlotWorld.PLOT_HEIGHT + 1), pos2.withY(hybridPlotWorld.getMaxGenHeight()), BlockTypes.AIR.getDefaultState());
        queue.setBiomeCuboid(pos1, pos2, biome);
    } else {
        queue.setRegenRegion(new CuboidRegion(pos1.getBlockVector3(), pos2.getBlockVector3()));
    }
    pastePlotSchematic(queue, pos1, pos2);
    return !enqueue || queue.enqueue();
}
Also used : Pattern(com.sk89q.worldedit.function.pattern.Pattern) BiomeType(com.sk89q.worldedit.world.biome.BiomeType) CuboidRegion(com.sk89q.worldedit.regions.CuboidRegion) Location(com.plotsquared.core.location.Location)

Example 23 with Pattern

use of com.sk89q.worldedit.function.pattern.Pattern in project PrivateMinesOOP by UntouchedOdin0.

the class WE7Adapter method fillRegion.

@Override
public void fillRegion(CuboidRegion region, Map<Material, Double> materials) {
    World world = new BukkitWorld(region.getWorld());
    Utils utils = new Utils();
    try (final EditSession editSession = WorldEdit.getInstance().newEditSessionBuilder().world(world).build()) {
        editSession.setReorderMode(EditSession.ReorderMode.MULTI_STAGE);
        final RandomPattern randomPattern = new RandomPattern();
        materials.forEach((material, chance) -> {
            Pattern pattern = BukkitAdapter.adapt(material.createBlockData()).toBaseBlock();
            randomPattern.add(pattern, chance);
        });
        final com.sk89q.worldedit.regions.CuboidRegion worldEditRegion = new com.sk89q.worldedit.regions.CuboidRegion(BukkitAdapter.asBlockVector(region.getStart()), BukkitAdapter.asBlockVector(region.getEnd()));
        editSession.setBlocks((Region) worldEditRegion, randomPattern);
        editSession.flushQueue();
    } catch (MaxChangedBlocksException e) {
        e.printStackTrace();
    // this shouldn't happen
    }
}
Also used : RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) Pattern(com.sk89q.worldedit.function.pattern.Pattern) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) CuboidRegion(redempt.redlib.region.CuboidRegion) World(com.sk89q.worldedit.world.World) BukkitWorld(com.sk89q.worldedit.bukkit.BukkitWorld) MaxChangedBlocksException(com.sk89q.worldedit.MaxChangedBlocksException) RandomPattern(com.sk89q.worldedit.function.pattern.RandomPattern) EditSession(com.sk89q.worldedit.EditSession)

Aggregations

Pattern (com.sk89q.worldedit.function.pattern.Pattern)23 RandomPattern (com.sk89q.worldedit.function.pattern.RandomPattern)11 EditSession (com.sk89q.worldedit.EditSession)7 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)7 Region (com.sk89q.worldedit.regions.Region)7 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)6 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)6 ParserContext (com.sk89q.worldedit.extension.input.ParserContext)4 World (com.sk89q.worldedit.world.World)4 IOException (java.io.IOException)4 MineData (me.untouchedodin0.privatemines.mine.data.MineData)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Caption (com.fastasyncworldedit.core.configuration.Caption)2 MaxChangedBlocksException (com.sk89q.worldedit.MaxChangedBlocksException)2 WorldEdit (com.sk89q.worldedit.WorldEdit)2 BukkitWorld (com.sk89q.worldedit.bukkit.BukkitWorld)2 NoMatchException (com.sk89q.worldedit.extension.input.NoMatchException)2 Extent (com.sk89q.worldedit.extent.Extent)2 Clipboard (com.sk89q.worldedit.extent.clipboard.Clipboard)2