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
}
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;
}
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();
}
Aggregations