Skip to content

Commit 2a70a30

Browse files
committed
Use ScaleAndTranslateTransform in everything using WorldEditExpressionEnvironment
1 parent d3a77db commit 2a70a30

6 files changed

Lines changed: 202 additions & 127 deletions

File tree

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

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@
9595
import com.sk89q.worldedit.math.interpolation.Node;
9696
import com.sk89q.worldedit.math.noise.RandomNoise;
9797
import com.sk89q.worldedit.math.transform.AffineTransform;
98+
import com.sk89q.worldedit.math.transform.ScaleAndTranslateTransform;
99+
import com.sk89q.worldedit.math.transform.Transform;
98100
import com.sk89q.worldedit.regions.CuboidRegion;
99101
import com.sk89q.worldedit.regions.CylinderRegion;
100102
import com.sk89q.worldedit.regions.EllipsoidRegion;
@@ -2311,6 +2313,7 @@ public List<Countable<BlockState>> getBlockDistribution(Region region, @Nullable
23112313
* @throws ExpressionException if there is a problem with the expression
23122314
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
23132315
*/
2316+
@Deprecated
23142317
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
23152318
final Pattern pattern, final String expressionString, final boolean hollow)
23162319
throws ExpressionException, MaxChangedBlocksException {
@@ -2331,12 +2334,32 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
23312334
* @throws ExpressionException if there is a problem with the expression
23322335
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
23332336
*/
2337+
@Deprecated
23342338
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
23352339
final Pattern pattern, final String expressionString, final boolean hollow, final int timeout)
23362340
throws ExpressionException, MaxChangedBlocksException {
2341+
return makeShape(region, new ScaleAndTranslateTransform(zero, unit), pattern, expressionString, hollow, timeout);
2342+
}
2343+
2344+
/**
2345+
* Generate a shape for the given expression.
2346+
*
2347+
* @param region the region to generate the shape in
2348+
* @param transform the transformation for x/y/z variables
2349+
* @param pattern the default material to make the shape from
2350+
* @param expressionString the expression defining the shape
2351+
* @param hollow whether the shape should be hollow
2352+
* @param timeout the time, in milliseconds, to wait for each expression evaluation before halting it. -1 to disable
2353+
* @return number of blocks changed
2354+
* @throws ExpressionException if there is a problem with the expression
2355+
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
2356+
*/
2357+
public int makeShape(final Region region,
2358+
Transform transform, final Pattern pattern, final String expressionString, final boolean hollow, final int timeout)
2359+
throws ExpressionException, MaxChangedBlocksException {
23372360
final Expression expression = Expression.compile(expressionString, "x", "y", "z", "type", "data");
23382361
expression.optimize();
2339-
return makeShape(region, zero, unit, pattern, expression, hollow, timeout);
2362+
return makeShape(region, transform, pattern, expression, hollow, timeout);
23402363
}
23412364

23422365
/**
@@ -2346,9 +2369,23 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
23462369
* The Expression class is subject to change. Expressions should be provided via the string overload.
23472370
* </p>
23482371
*/
2372+
@Deprecated
23492373
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
23502374
final Pattern pattern, final Expression expression, final boolean hollow, final int timeout)
23512375
throws ExpressionException, MaxChangedBlocksException {
2376+
return makeShape(region, new ScaleAndTranslateTransform(zero, unit), pattern, expression, hollow, timeout);
2377+
}
2378+
2379+
/**
2380+
* Internal version of {@link EditSession#makeShape(Region, Vector3, Vector3, Pattern, String, boolean, int)}.
2381+
*
2382+
* <p>
2383+
* The Expression class is subject to change. Expressions should be provided via the string overload.
2384+
* </p>
2385+
*/
2386+
public int makeShape(final Region region, Transform transform,
2387+
final Pattern pattern, final Expression expression, final boolean hollow, final int timeout)
2388+
throws ExpressionException, MaxChangedBlocksException {
23522389

23532390
expression.getSlots().getVariable("x")
23542391
.orElseThrow(IllegalStateException::new);
@@ -2362,16 +2399,17 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
23622399
final Variable dataVariable = expression.getSlots().getVariable("data")
23632400
.orElseThrow(IllegalStateException::new);
23642401

2365-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero);
2402+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
23662403
expression.setEnvironment(environment);
23672404

23682405
final int[] timedOut = {0};
2406+
final Transform transformInverse = transform.inverse();
23692407
final ArbitraryShape shape = new ArbitraryShape(region) {
23702408
@Override
23712409
protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial) {
23722410
final Vector3 current = Vector3.at(x, y, z);
23732411
environment.setCurrentBlock(current);
2374-
final Vector3 inputPosition = current.subtract(zero).divide(unit);
2412+
final Vector3 inputPosition = transformInverse.apply(current);
23752413

23762414
try {
23772415
int[] legacy = LegacyMapper.getInstance().getLegacyFromBlock(defaultMaterial.toImmutableState());
@@ -2428,9 +2466,10 @@ protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial)
24282466
* @throws ExpressionException thrown on invalid expression input
24292467
* @throws MaxChangedBlocksException thrown if too many blocks are changed
24302468
*/
2469+
@Deprecated
24312470
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final String expressionString)
24322471
throws ExpressionException, MaxChangedBlocksException {
2433-
return deformRegion(region, zero, unit, expressionString, WorldEdit.getInstance().getConfiguration().calculationTimeout);
2472+
return deformRegion(region, new ScaleAndTranslateTransform(zero, unit), expressionString, WorldEdit.getInstance().getConfiguration().calculationTimeout);
24342473
}
24352474

24362475
/**
@@ -2449,11 +2488,31 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24492488
* @throws ExpressionException thrown on invalid expression input
24502489
* @throws MaxChangedBlocksException thrown if too many blocks are changed
24512490
*/
2491+
@Deprecated
24522492
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final String expressionString,
24532493
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2494+
final Transform transform = new ScaleAndTranslateTransform(zero, unit);
2495+
return deformRegion(region, transform, expressionString, timeout);
2496+
}
2497+
2498+
/**
2499+
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
2500+
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
2501+
* have changed.
2502+
*
2503+
* @param region the region to deform
2504+
* @param transform the coordinate system
2505+
* @param expressionString the expression to evaluate for each block
2506+
* @param timeout maximum time for the expression to evaluate for each block. -1 for unlimited.
2507+
* @return number of blocks changed
2508+
* @throws ExpressionException thrown on invalid expression input
2509+
* @throws MaxChangedBlocksException thrown if too many blocks are changed
2510+
*/
2511+
public int deformRegion(final Region region, final Transform transform, final String expressionString,
2512+
final int timeout) throws ExpressionException, MaxChangedBlocksException {
24542513
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
24552514
expression.optimize();
2456-
return deformRegion(region, zero, unit, expression, timeout);
2515+
return deformRegion(region, transform, expression, timeout);
24572516
}
24582517

24592518
/**
@@ -2463,32 +2522,47 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24632522
* The Expression class is subject to change. Expressions should be provided via the string overload.
24642523
* </p>
24652524
*/
2525+
@Deprecated
24662526
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final Expression expression,
24672527
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2528+
return deformRegion(region, new ScaleAndTranslateTransform(zero, unit), expression, timeout);
2529+
}
2530+
2531+
/**
2532+
* Internal version of {@link EditSession#deformRegion(Region, Vector3, Vector3, String, int)}.
2533+
*
2534+
* <p>
2535+
* The Expression class is subject to change. Expressions should be provided via the string overload.
2536+
* </p>
2537+
*/
2538+
public int deformRegion(final Region region, final Transform transform, final Expression expression,
2539+
final int timeout) throws ExpressionException, MaxChangedBlocksException {
24682540
final Variable x = expression.getSlots().getVariable("x")
24692541
.orElseThrow(IllegalStateException::new);
24702542
final Variable y = expression.getSlots().getVariable("y")
24712543
.orElseThrow(IllegalStateException::new);
24722544
final Variable z = expression.getSlots().getVariable("z")
24732545
.orElseThrow(IllegalStateException::new);
24742546

2475-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero);
2547+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
24762548
expression.setEnvironment(environment);
24772549

24782550
final DoubleArrayList<BlockVector3, BaseBlock> queue = new DoubleArrayList<>(false);
24792551

2552+
final Transform transformInverse = transform.inverse();
24802553
for (BlockVector3 targetBlockPosition : region) {
24812554
final Vector3 targetPosition = targetBlockPosition.toVector3();
24822555
environment.setCurrentBlock(targetPosition);
24832556

24842557
// transform from target coordinates
2485-
final Vector3 inputPosition = targetPosition.subtract(zero).divide(unit);
2558+
final Vector3 inputPosition = transformInverse.apply(targetPosition);
24862559

24872560
// deform
24882561
expression.evaluate(new double[]{ inputPosition.x(), inputPosition.y(), inputPosition.z() }, timeout);
2562+
final Vector3 outputPosition = Vector3.at(x.value(), y.value(), z.value());
24892563

24902564
// transform to source coordinates, round-nearest
2491-
final BlockVector3 sourcePosition = environment.toWorld(x.value(), y.value(), z.value());
2565+
final BlockVector3 sourcePosition = transform.apply(outputPosition).add(0.5, 0.5, 0.5).toBlockPoint();
24922566

24932567
// read block from world
24942568
final BaseBlock material = world.getFullBlock(sourcePosition);
@@ -2796,28 +2870,36 @@ private void recurseHollow(Region region, BlockVector3 origin, Set<BlockVector3>
27962870
}
27972871
}
27982872

2873+
@Deprecated
27992874
public int makeBiomeShape(final Region region, final Vector3 zero, final Vector3 unit, final BiomeType biomeType,
28002875
final String expressionString, final boolean hollow) throws ExpressionException {
28012876
return makeBiomeShape(region, zero, unit, biomeType, expressionString, hollow, WorldEdit.getInstance().getConfiguration().calculationTimeout);
28022877
}
28032878

2879+
@Deprecated
28042880
public int makeBiomeShape(final Region region, final Vector3 zero, final Vector3 unit, final BiomeType biomeType,
28052881
final String expressionString, final boolean hollow, final int timeout) throws ExpressionException {
2882+
return makeBiomeShape(region, new ScaleAndTranslateTransform(zero, unit), biomeType, expressionString, hollow, timeout);
2883+
}
2884+
2885+
public int makeBiomeShape(final Region region, Transform transform, final BiomeType biomeType,
2886+
final String expressionString, final boolean hollow, final int timeout) throws ExpressionException {
28062887

28072888
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
28082889
expression.optimize();
28092890

28102891
final EditSession editSession = this;
2811-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(editSession, unit, zero);
2892+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(editSession, transform);
28122893
expression.setEnvironment(environment);
28132894

28142895
AtomicInteger timedOut = new AtomicInteger();
2896+
final Transform transformInverse = transform.inverse();
28152897
final ArbitraryBiomeShape shape = new ArbitraryBiomeShape(region) {
28162898
@Override
28172899
protected BiomeType getBiome(int x, int y, int z, BiomeType defaultBiomeType) {
28182900
final Vector3 current = Vector3.at(x, y, z);
28192901
environment.setCurrentBlock(current);
2820-
final Vector3 inputPosition = current.subtract(zero).divide(unit);
2902+
final Vector3 inputPosition = transformInverse.apply(current);
28212903

28222904
try {
28232905
if (expression.evaluate(new double[]{ inputPosition.x(), inputPosition.y(), inputPosition.z() }, timeout) <= 0) {

‎worldedit-core/src/main/java/com/sk89q/worldedit/command/GenerationCommands.java‎

Lines changed: 6 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
import com.sk89q.worldedit.internal.annotation.Radii;
3333
import com.sk89q.worldedit.internal.annotation.Selection;
3434
import com.sk89q.worldedit.internal.expression.ExpressionException;
35+
import com.sk89q.worldedit.internal.util.TransformUtil;
3536
import com.sk89q.worldedit.math.BlockVector3;
36-
import com.sk89q.worldedit.math.Vector3;
37+
import com.sk89q.worldedit.math.transform.Transform;
3738
import com.sk89q.worldedit.regions.Region;
3839
import com.sk89q.worldedit.util.TreeGenerator.TreeType;
3940
import com.sk89q.worldedit.util.formatting.text.TextComponent;
@@ -373,42 +374,10 @@ public int generate(Actor actor, LocalSession session, EditSession editSession,
373374
boolean offsetPlacement,
374375
@Switch(name = 'c', desc = "Use the selection's center as origin")
375376
boolean offsetCenter) throws WorldEditException {
376-
377-
final Vector3 zero;
378-
Vector3 unit;
379-
380-
if (useRawCoords) {
381-
zero = Vector3.ZERO;
382-
unit = Vector3.ONE;
383-
} else if (offsetPlacement) {
384-
zero = session.getPlacementPosition(actor).toVector3();
385-
unit = Vector3.ONE;
386-
} else if (offsetCenter) {
387-
final Vector3 min = region.getMinimumPoint().toVector3();
388-
final Vector3 max = region.getMaximumPoint().toVector3();
389-
390-
zero = max.add(min).multiply(0.5);
391-
unit = Vector3.ONE;
392-
} else {
393-
final Vector3 min = region.getMinimumPoint().toVector3();
394-
final Vector3 max = region.getMaximumPoint().toVector3();
395-
396-
zero = max.add(min).multiply(0.5);
397-
unit = max.subtract(zero);
398-
399-
if (unit.x() == 0) {
400-
unit = unit.withX(1.0);
401-
}
402-
if (unit.y() == 0) {
403-
unit = unit.withY(1.0);
404-
}
405-
if (unit.z() == 0) {
406-
unit = unit.withZ(1.0);
407-
}
408-
}
377+
final Transform transform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);
409378

410379
try {
411-
final int affected = editSession.makeShape(region, zero, unit, pattern, String.join(" ", expression), hollow, session.getTimeout());
380+
final int affected = editSession.makeShape(region, transform, pattern, String.join(" ", expression), hollow, session.getTimeout());
412381
if (actor instanceof Player) {
413382
((Player) actor).findFreePosition();
414383
}
@@ -442,41 +411,10 @@ public int generateBiome(Actor actor, LocalSession session, EditSession editSess
442411
boolean offsetPlacement,
443412
@Switch(name = 'c', desc = "Use the selection's center as origin")
444413
boolean offsetCenter) throws WorldEditException {
445-
final Vector3 zero;
446-
Vector3 unit;
447-
448-
if (useRawCoords) {
449-
zero = Vector3.ZERO;
450-
unit = Vector3.ONE;
451-
} else if (offsetPlacement) {
452-
zero = session.getPlacementPosition(actor).toVector3();
453-
unit = Vector3.ONE;
454-
} else if (offsetCenter) {
455-
final Vector3 min = region.getMinimumPoint().toVector3();
456-
final Vector3 max = region.getMaximumPoint().toVector3();
457-
458-
zero = max.add(min).multiply(0.5);
459-
unit = Vector3.ONE;
460-
} else {
461-
final Vector3 min = region.getMinimumPoint().toVector3();
462-
final Vector3 max = region.getMaximumPoint().toVector3();
463-
464-
zero = max.add(min).multiply(0.5);
465-
unit = max.subtract(zero);
466-
467-
if (unit.x() == 0) {
468-
unit = unit.withX(1.0);
469-
}
470-
if (unit.y() == 0) {
471-
unit = unit.withY(1.0);
472-
}
473-
if (unit.z() == 0) {
474-
unit = unit.withZ(1.0);
475-
}
476-
}
414+
final Transform transform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);
477415

478416
try {
479-
final int affected = editSession.makeBiomeShape(region, zero, unit, target, String.join(" ", expression), hollow, session.getTimeout());
417+
final int affected = editSession.makeBiomeShape(region, transform, target, String.join(" ", expression), hollow, session.getTimeout());
480418
actor.printInfo(TranslatableComponent.of("worldedit.generatebiome.changed", TextComponent.of(affected)));
481419
return affected;
482420
} catch (ExpressionException e) {

0 commit comments

Comments
 (0)