Skip to content

Commit e54d84d

Browse files
committed
Add inputExtent parameter and separate in/outputTransform for deformRegion
1 parent b13cf29 commit e54d84d

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;
@@ -2437,6 +2438,28 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24372438
return deformRegion(region, transform, expressionString, timeout);
24382439
}
24392440

2441+
/**
2442+
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
2443+
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
2444+
* have changed.
2445+
*
2446+
* @param region the region to deform
2447+
* @param outputTransform the output coordinate system
2448+
* @param expressionString the expression to evaluate for each block
2449+
* @param timeout maximum time for the expression to evaluate for each block. -1 for unlimited.
2450+
* @param inputExtent the InputExtent to fetch blocks from, for instance a World or a Clipboard
2451+
* @param inputTransform the input coordinate system
2452+
* @return number of blocks changed
2453+
* @throws ExpressionException thrown on invalid expression input
2454+
* @throws MaxChangedBlocksException thrown if too many blocks are changed
2455+
*/
2456+
public int deformRegion(final Region region, final Transform outputTransform, final String expressionString,
2457+
final int timeout, InputExtent inputExtent, Transform inputTransform) throws ExpressionException, MaxChangedBlocksException {
2458+
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
2459+
expression.optimize();
2460+
return deformRegion(region, outputTransform, expression, timeout, inputExtent, inputTransform);
2461+
}
2462+
24402463
/**
24412464
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
24422465
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
@@ -2450,11 +2473,10 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24502473
* @throws ExpressionException thrown on invalid expression input
24512474
* @throws MaxChangedBlocksException thrown if too many blocks are changed
24522475
*/
2476+
@Deprecated
24532477
public int deformRegion(final Region region, final Transform transform, final String expressionString,
24542478
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2455-
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
2456-
expression.optimize();
2457-
return deformRegion(region, transform, expression, timeout);
2479+
return deformRegion(region, transform, expressionString, timeout, world, transform);
24582480
}
24592481

24602482
/**
@@ -2467,7 +2489,8 @@ public int deformRegion(final Region region, final Transform transform, final St
24672489
@Deprecated
24682490
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final Expression expression,
24692491
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2470-
return deformRegion(region, new SimpleTransform(zero, unit), expression, timeout);
2492+
final Transform transform = new SimpleTransform(zero, unit);
2493+
return deformRegion(region, transform, expression, timeout, world, transform);
24712494
}
24722495

24732496
/**
@@ -2477,33 +2500,33 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24772500
* The Expression class is subject to change. Expressions should be provided via the string overload.
24782501
* </p>
24792502
*/
2480-
public int deformRegion(final Region region, final Transform transform, final Expression expression,
2481-
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2503+
public int deformRegion(final Region region, final Transform outputTransform, final Expression expression,
2504+
final int timeout, InputExtent inputExtent, final Transform inputTransform) throws ExpressionException, MaxChangedBlocksException {
24822505
final Variable x = expression.getSlots().getVariable("x")
24832506
.orElseThrow(IllegalStateException::new);
24842507
final Variable y = expression.getSlots().getVariable("y")
24852508
.orElseThrow(IllegalStateException::new);
24862509
final Variable z = expression.getSlots().getVariable("z")
24872510
.orElseThrow(IllegalStateException::new);
24882511

2489-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
2512+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, outputTransform);
24902513
expression.setEnvironment(environment);
24912514

24922515
final DoubleArrayList<BlockVector3, BaseBlock> queue = new DoubleArrayList<>(false);
24932516

2494-
final Transform transformInverse = transform.inverse();
2517+
final Transform outputTransformInverse = outputTransform.inverse();
24952518
for (BlockVector3 position : region) {
24962519
// transform
2497-
final Vector3 scaled = transformInverse.apply(position.toVector3());
2520+
final Vector3 scaled = outputTransformInverse.apply(position.toVector3());
24982521

24992522
// deform
25002523
expression.evaluate(new double[]{ scaled.x(), scaled.y(), scaled.z() }, timeout);
25012524

25022525
// untransform, round-nearest
2503-
final BlockVector3 sourcePosition = transform.apply(Vector3.at(x.value(), y.value(), z.value())).add(0.5, 0.5, 0.5).toBlockPoint();
2526+
final BlockVector3 sourcePosition = inputTransform.apply(Vector3.at(x.value(), y.value(), z.value())).add(0.5, 0.5, 0.5).toBlockPoint();
25042527

2505-
// read block from world
2506-
final BaseBlock material = world.getFullBlock(sourcePosition);
2528+
// read block from world/clipboard
2529+
final BaseBlock material = inputExtent.getFullBlock(sourcePosition);
25072530

25082531
// queue operation
25092532
queue.put(position, 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)