Search in sources :

Example 6 with RegionVisitor

use of com.sk89q.worldedit.function.visitor.RegionVisitor in project FastAsyncWorldEdit by IntellectualSites.

the class EditSession method deformRegion.

/**
 * Internal version of {@link EditSession#deformRegion(Region, Vector3, Vector3, String, int)}.
 *
 * <p>
 * The Expression class is subject to change. Expressions should be provided via the string overload.
 * </p>
 */
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final Expression expression, final int timeout) throws ExpressionException, MaxChangedBlocksException {
    final Variable x = expression.getSlots().getVariable("x").orElseThrow(IllegalStateException::new);
    final Variable y = expression.getSlots().getVariable("y").orElseThrow(IllegalStateException::new);
    final Variable z = expression.getSlots().getVariable("z").orElseThrow(IllegalStateException::new);
    final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero);
    expression.setEnvironment(environment);
    // FAWE start
    final Vector3 zero2 = zero.add(0.5, 0.5, 0.5);
    RegionVisitor visitor = new RegionVisitor(region, position -> {
        try {
            // offset, scale
            final Vector3 scaled = position.toVector3().subtract(zero).divide(unit);
            // transform
            expression.evaluate(new double[] { scaled.getX(), scaled.getY(), scaled.getZ() }, timeout);
            int xv = (int) (x.getValue() * unit.getX() + zero2.getX());
            int yv = (int) (y.getValue() * unit.getY() + zero2.getY());
            int zv = (int) (z.getValue() * unit.getZ() + zero2.getZ());
            BlockState get;
            if (yv >= minY && yv <= maxY) {
                get = getBlock(xv, yv, zv);
            } else {
                get = BlockTypes.AIR.getDefaultState();
            }
            // read block from world
            return setBlock(position, get);
        } catch (EvaluationException e) {
            throw new RuntimeException(e);
        }
    }, this);
    Operations.completeBlindly(visitor);
    changes += visitor.getAffected();
    return changes;
// FAWE end
}
Also used : Variable(com.sk89q.worldedit.internal.expression.LocalSlot.Variable) RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) FlatRegionVisitor(com.sk89q.worldedit.function.visitor.FlatRegionVisitor) BlockState(com.sk89q.worldedit.world.block.BlockState) WorldEditExpressionEnvironment(com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment) MutableBlockVector3(com.fastasyncworldedit.core.math.MutableBlockVector3) MutableVector3(com.fastasyncworldedit.core.math.MutableVector3) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) EvaluationException(com.sk89q.worldedit.internal.expression.EvaluationException)

Example 7 with RegionVisitor

use of com.sk89q.worldedit.function.visitor.RegionVisitor in project FastAsyncWorldEdit by IntellectualSites.

the class EditSession method fall.

public <B extends BlockStateHolder<B>> int fall(final Region region, boolean fullHeight, final B replace) {
    FlatRegion flat = asFlatRegion(region);
    final int startPerformY = region.getMinimumPoint().getBlockY();
    final int startCheckY = fullHeight ? getMinY() : startPerformY;
    final int endY = region.getMaximumPoint().getBlockY();
    RegionVisitor visitor = new RegionVisitor(flat, pos -> {
        int x = pos.getX();
        int z = pos.getZ();
        int freeSpot = startCheckY;
        for (int y = startCheckY; y <= endY; y++) {
            if (y < startPerformY) {
                if (!getBlockType(x, y, z).getMaterial().isAir()) {
                    freeSpot = y + 1;
                }
                continue;
            }
            BlockType block = getBlockType(x, y, z);
            if (!block.getMaterial().isAir()) {
                if (freeSpot != y) {
                    setBlock(x, freeSpot, z, block);
                    setBlock(x, y, z, replace);
                }
                freeSpot++;
            }
        }
        return true;
    }, this);
    Operations.completeBlindly(visitor);
    return this.changes;
}
Also used : RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) FlatRegionVisitor(com.sk89q.worldedit.function.visitor.FlatRegionVisitor) BlockType(com.sk89q.worldedit.world.block.BlockType) FlatRegion(com.sk89q.worldedit.regions.FlatRegion) Regions.asFlatRegion(com.sk89q.worldedit.regions.Regions.asFlatRegion)

Example 8 with RegionVisitor

use of com.sk89q.worldedit.function.visitor.RegionVisitor in project FastAsyncWorldEdit by IntellectualSites.

the class Extent method replaceBlocks.

/**
 * Replaces all the blocks matching a given mask, within a given region, to a block
 * returned by a given pattern.
 *
 * @param region  the region to replace the blocks within
 * @param mask    the mask that blocks must match
 * @param pattern the pattern that provides the new blocks
 * @return number of blocks affected
 * @throws MaxChangedBlocksException thrown if too many blocks are changed
 */
default int replaceBlocks(Region region, Mask mask, Pattern pattern) throws MaxChangedBlocksException {
    checkNotNull(region);
    checkNotNull(mask);
    checkNotNull(pattern);
    BlockReplace replace = new BlockReplace(this, pattern);
    RegionMaskingFilter filter = new RegionMaskingFilter(this, mask, replace);
    // FAWE start > add extent to RegionVisitor to allow chunk preloading
    RegionVisitor visitor = new RegionVisitor(region, filter, this);
    // FAWE end
    Operations.completeLegacy(visitor);
    return visitor.getAffected();
}
Also used : RegionVisitor(com.sk89q.worldedit.function.visitor.RegionVisitor) BlockReplace(com.sk89q.worldedit.function.block.BlockReplace) RegionMaskingFilter(com.sk89q.worldedit.function.RegionMaskingFilter)

Aggregations

RegionVisitor (com.sk89q.worldedit.function.visitor.RegionVisitor)8 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)4 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)3 RegionMaskingFilter (com.sk89q.worldedit.function.RegionMaskingFilter)3 Command (org.enginehub.piston.annotation.Command)3 Logging (com.sk89q.worldedit.command.util.Logging)2 RegionFunction (com.sk89q.worldedit.function.RegionFunction)2 FlatRegionVisitor (com.sk89q.worldedit.function.visitor.FlatRegionVisitor)2 CuboidRegion (com.sk89q.worldedit.regions.CuboidRegion)2 FlatRegion (com.sk89q.worldedit.regions.FlatRegion)2 BlockType (com.sk89q.worldedit.world.block.BlockType)2 BlockTranslateExtent (com.fastasyncworldedit.core.extent.BlockTranslateExtent)1 PositionTransformExtent (com.fastasyncworldedit.core.extent.PositionTransformExtent)1 RegionMaskTestFunction (com.fastasyncworldedit.core.function.RegionMaskTestFunction)1 BiomeCopy (com.fastasyncworldedit.core.function.block.BiomeCopy)1 CombinedBlockCopy (com.fastasyncworldedit.core.function.block.CombinedBlockCopy)1 SimpleBlockCopy (com.fastasyncworldedit.core.function.block.SimpleBlockCopy)1 IntersectRegionFunction (com.fastasyncworldedit.core.function.visitor.IntersectRegionFunction)1 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)1 MutableVector3 (com.fastasyncworldedit.core.math.MutableVector3)1