Search in sources :

Example 1 with EvaluationException

use of com.sk89q.worldedit.internal.expression.EvaluationException 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)

Aggregations

MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)1 MutableVector3 (com.fastasyncworldedit.core.math.MutableVector3)1 FlatRegionVisitor (com.sk89q.worldedit.function.visitor.FlatRegionVisitor)1 RegionVisitor (com.sk89q.worldedit.function.visitor.RegionVisitor)1 EvaluationException (com.sk89q.worldedit.internal.expression.EvaluationException)1 Variable (com.sk89q.worldedit.internal.expression.LocalSlot.Variable)1 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)1 Vector3 (com.sk89q.worldedit.math.Vector3)1 WorldEditExpressionEnvironment (com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment)1 BlockState (com.sk89q.worldedit.world.block.BlockState)1