Skip to content

Commit cb59fab

Browse files
committed
Add inputExtent parameter and separate in/outputTransform for deformRegion
1 parent 17e28bc commit cb59fab

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

worldedit-core/src/main/java/com/sk89q/worldedit/EditSession.java

+35-12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.sk89q.worldedit.extension.platform.Watchdog;
2929
import com.sk89q.worldedit.extent.ChangeSetExtent;
3030
import com.sk89q.worldedit.extent.Extent;
31+
import com.sk89q.worldedit.extent.InputExtent;
3132
import com.sk89q.worldedit.extent.MaskingExtent;
3233
import com.sk89q.worldedit.extent.NullExtent;
3334
import com.sk89q.worldedit.extent.TracingExtent;
@@ -2451,6 +2452,28 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24512452
return deformRegion(region, transform, expressionString, timeout);
24522453
}
24532454

2455+
/**
2456+
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
2457+
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
2458+
* have changed.
2459+
*
2460+
* @param region the region to deform
2461+
* @param targetTransform the target coordinate system
2462+
* @param expressionString the expression to evaluate for each block
2463+
* @param timeout maximum time for the expression to evaluate for each block. -1 for unlimited.
2464+
* @param sourceExtent the InputExtent to fetch blocks from, for instance a World or a Clipboard
2465+
* @param sourceTransform the source coordinate system
2466+
* @return number of blocks changed
2467+
* @throws ExpressionException thrown on invalid expression input
2468+
* @throws MaxChangedBlocksException thrown if too many blocks are changed
2469+
*/
2470+
public int deformRegion(final Region region, final Transform targetTransform, final String expressionString,
2471+
final int timeout, InputExtent sourceExtent, Transform sourceTransform) throws ExpressionException, MaxChangedBlocksException {
2472+
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
2473+
expression.optimize();
2474+
return deformRegion(region, targetTransform, expression, timeout, sourceExtent, sourceTransform);
2475+
}
2476+
24542477
/**
24552478
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
24562479
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
@@ -2464,11 +2487,10 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24642487
* @throws ExpressionException thrown on invalid expression input
24652488
* @throws MaxChangedBlocksException thrown if too many blocks are changed
24662489
*/
2490+
@Deprecated
24672491
public int deformRegion(final Region region, final Transform transform, final String expressionString,
24682492
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2469-
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
2470-
expression.optimize();
2471-
return deformRegion(region, transform, expression, timeout);
2493+
return deformRegion(region, transform, expressionString, timeout, world, transform);
24722494
}
24732495

24742496
/**
@@ -2481,7 +2503,8 @@ public int deformRegion(final Region region, final Transform transform, final St
24812503
@Deprecated
24822504
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final Expression expression,
24832505
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2484-
return deformRegion(region, new ScaleAndTranslateTransform(zero, unit), expression, timeout);
2506+
final Transform transform = new ScaleAndTranslateTransform(zero, unit);
2507+
return deformRegion(region, transform, expression, timeout, world, transform);
24852508
}
24862509

24872510
/**
@@ -2491,37 +2514,37 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24912514
* The Expression class is subject to change. Expressions should be provided via the string overload.
24922515
* </p>
24932516
*/
2494-
public int deformRegion(final Region region, final Transform transform, final Expression expression,
2495-
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2517+
public int deformRegion(final Region region, final Transform targetTransform, final Expression expression,
2518+
final int timeout, InputExtent sourceExtent, final Transform sourceTransform) throws ExpressionException, MaxChangedBlocksException {
24962519
final Variable x = expression.getSlots().getVariable("x")
24972520
.orElseThrow(IllegalStateException::new);
24982521
final Variable y = expression.getSlots().getVariable("y")
24992522
.orElseThrow(IllegalStateException::new);
25002523
final Variable z = expression.getSlots().getVariable("z")
25012524
.orElseThrow(IllegalStateException::new);
25022525

2503-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
2526+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, targetTransform);
25042527
expression.setEnvironment(environment);
25052528

25062529
final DoubleArrayList<BlockVector3, BaseBlock> queue = new DoubleArrayList<>(false);
25072530

2508-
final Transform transformInverse = transform.inverse();
2531+
final Transform targetTransformInverse = targetTransform.inverse();
25092532
for (BlockVector3 targetBlockPosition : region) {
25102533
final Vector3 targetPosition = targetBlockPosition.toVector3();
25112534
environment.setCurrentBlock(targetPosition);
25122535

25132536
// transform from target coordinates
2514-
final Vector3 inputPosition = transformInverse.apply(targetPosition);
2537+
final Vector3 inputPosition = targetTransformInverse.apply(targetPosition);
25152538

25162539
// deform
25172540
expression.evaluate(new double[]{ inputPosition.x(), inputPosition.y(), inputPosition.z() }, timeout);
25182541
final Vector3 outputPosition = Vector3.at(x.value(), y.value(), z.value());
25192542

25202543
// transform to source coordinates, round-nearest
2521-
final BlockVector3 sourcePosition = transform.apply(outputPosition).add(0.5, 0.5, 0.5).toBlockPoint();
2544+
final BlockVector3 sourcePosition = sourceTransform.apply(outputPosition).add(0.5, 0.5, 0.5).toBlockPoint();
25222545

2523-
// read block from world
2524-
final BaseBlock material = world.getFullBlock(sourcePosition);
2546+
// read block from source extent (e.g. world/clipboard)
2547+
final BaseBlock material = sourceExtent.getFullBlock(sourcePosition);
25252548

25262549
// queue operation
25272550
queue.put(targetBlockPosition, material);

worldedit-core/src/main/java/com/sk89q/worldedit/command/RegionCommands.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.sk89q.worldedit.entity.Player;
3232
import com.sk89q.worldedit.extension.platform.Actor;
3333
import com.sk89q.worldedit.extent.Extent;
34+
import com.sk89q.worldedit.extent.InputExtent;
3435
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
3536
import com.sk89q.worldedit.function.GroundFunction;
3637
import com.sk89q.worldedit.function.RegionFunction;
@@ -501,9 +502,10 @@ public int deform(Actor actor, LocalSession session, EditSession editSession,
501502
boolean offsetCenter) throws WorldEditException {
502503

503504
final Transform transform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);
505+
final InputExtent inputExtent = editSession.getWorld();
504506

505507
try {
506-
final int affected = editSession.deformRegion(region, transform, String.join(" ", expression), session.getTimeout());
508+
final int affected = editSession.deformRegion(region, transform, String.join(" ", expression), session.getTimeout(), inputExtent, transform);
507509
if (actor instanceof Player) {
508510
((Player) actor).findFreePosition();
509511
}

worldedit-core/src/main/java/com/sk89q/worldedit/function/factory/Deform.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,8 @@ private record DeformOperation(
170170
public Operation resume(RunContext run) throws WorldEditException {
171171
try {
172172
// TODO: Move deformation code
173-
((EditSession) destination).deformRegion(region, transform, expression, timeout);
173+
final EditSession editSession = (EditSession) destination;
174+
editSession.deformRegion(region, transform, expression, timeout, editSession.getWorld(), transform);
174175
return null;
175176
} catch (ExpressionException e) {
176177
throw new RuntimeException("Failed to execute expression", e); // TODO: Better exception to throw here?

0 commit comments

Comments
 (0)