Skip to content

Commit b13cf29

Browse files
committed
Use SimpleTransform in everything using WorldEditExpressionEnvironment
1 parent dca803b commit b13cf29

File tree

6 files changed

+188
-127
lines changed

6 files changed

+188
-127
lines changed

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

+78-11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@
9494
import com.sk89q.worldedit.math.interpolation.Node;
9595
import com.sk89q.worldedit.math.noise.RandomNoise;
9696
import com.sk89q.worldedit.math.transform.AffineTransform;
97+
import com.sk89q.worldedit.math.transform.SimpleTransform;
98+
import com.sk89q.worldedit.math.transform.Transform;
9799
import com.sk89q.worldedit.regions.CuboidRegion;
98100
import com.sk89q.worldedit.regions.CylinderRegion;
99101
import com.sk89q.worldedit.regions.EllipsoidRegion;
@@ -2267,6 +2269,7 @@ public List<Countable<BlockState>> getBlockDistribution(Region region, boolean s
22672269
* @throws ExpressionException if there is a problem with the expression
22682270
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
22692271
*/
2272+
@Deprecated
22702273
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
22712274
final Pattern pattern, final String expressionString, final boolean hollow)
22722275
throws ExpressionException, MaxChangedBlocksException {
@@ -2287,12 +2290,32 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
22872290
* @throws ExpressionException if there is a problem with the expression
22882291
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
22892292
*/
2293+
@Deprecated
22902294
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
22912295
final Pattern pattern, final String expressionString, final boolean hollow, final int timeout)
22922296
throws ExpressionException, MaxChangedBlocksException {
2297+
return makeShape(region, new SimpleTransform(zero, unit), pattern, expressionString, hollow, timeout);
2298+
}
2299+
2300+
/**
2301+
* Generate a shape for the given expression.
2302+
*
2303+
* @param region the region to generate the shape in
2304+
* @param transform the transformation for x/y/z variables
2305+
* @param pattern the default material to make the shape from
2306+
* @param expressionString the expression defining the shape
2307+
* @param hollow whether the shape should be hollow
2308+
* @param timeout the time, in milliseconds, to wait for each expression evaluation before halting it. -1 to disable
2309+
* @return number of blocks changed
2310+
* @throws ExpressionException if there is a problem with the expression
2311+
* @throws MaxChangedBlocksException if the maximum block change limit is exceeded
2312+
*/
2313+
public int makeShape(final Region region,
2314+
Transform transform, final Pattern pattern, final String expressionString, final boolean hollow, final int timeout)
2315+
throws ExpressionException, MaxChangedBlocksException {
22932316
final Expression expression = Expression.compile(expressionString, "x", "y", "z", "type", "data");
22942317
expression.optimize();
2295-
return makeShape(region, zero, unit, pattern, expression, hollow, timeout);
2318+
return makeShape(region, transform, pattern, expression, hollow, timeout);
22962319
}
22972320

22982321
/**
@@ -2302,7 +2325,7 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
23022325
* The Expression class is subject to change. Expressions should be provided via the string overload.
23032326
* </p>
23042327
*/
2305-
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit,
2328+
public int makeShape(final Region region, Transform transform,
23062329
final Pattern pattern, final Expression expression, final boolean hollow, final int timeout)
23072330
throws ExpressionException, MaxChangedBlocksException {
23082331

@@ -2318,16 +2341,17 @@ public int makeShape(final Region region, final Vector3 zero, final Vector3 unit
23182341
final Variable dataVariable = expression.getSlots().getVariable("data")
23192342
.orElseThrow(IllegalStateException::new);
23202343

2321-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero);
2344+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
23222345
expression.setEnvironment(environment);
23232346

23242347
final int[] timedOut = {0};
2348+
final Transform transformInverse = transform.inverse();
23252349
final ArbitraryShape shape = new ArbitraryShape(region) {
23262350
@Override
23272351
protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial) {
23282352
final Vector3 current = Vector3.at(x, y, z);
23292353
environment.setCurrentBlock(current);
2330-
final Vector3 scaled = current.subtract(zero).divide(unit);
2354+
final Vector3 scaled = transformInverse.apply(current);
23312355

23322356
try {
23332357
int[] legacy = LegacyMapper.getInstance().getLegacyFromBlock(defaultMaterial.toImmutableState());
@@ -2384,9 +2408,10 @@ protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial)
23842408
* @throws ExpressionException thrown on invalid expression input
23852409
* @throws MaxChangedBlocksException thrown if too many blocks are changed
23862410
*/
2411+
@Deprecated
23872412
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final String expressionString)
23882413
throws ExpressionException, MaxChangedBlocksException {
2389-
return deformRegion(region, zero, unit, expressionString, WorldEdit.getInstance().getConfiguration().calculationTimeout);
2414+
return deformRegion(region, new SimpleTransform(zero, unit), expressionString, WorldEdit.getInstance().getConfiguration().calculationTimeout);
23902415
}
23912416

23922417
/**
@@ -2405,11 +2430,31 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24052430
* @throws ExpressionException thrown on invalid expression input
24062431
* @throws MaxChangedBlocksException thrown if too many blocks are changed
24072432
*/
2433+
@Deprecated
24082434
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final String expressionString,
24092435
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2436+
final Transform transform = new SimpleTransform(zero, unit);
2437+
return deformRegion(region, transform, expressionString, timeout);
2438+
}
2439+
2440+
/**
2441+
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
2442+
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
2443+
* have changed.
2444+
*
2445+
* @param region the region to deform
2446+
* @param transform the coordinate system
2447+
* @param expressionString the expression to evaluate for each block
2448+
* @param timeout maximum time for the expression to evaluate for each block. -1 for unlimited.
2449+
* @return number of blocks changed
2450+
* @throws ExpressionException thrown on invalid expression input
2451+
* @throws MaxChangedBlocksException thrown if too many blocks are changed
2452+
*/
2453+
public int deformRegion(final Region region, final Transform transform, final String expressionString,
2454+
final int timeout) throws ExpressionException, MaxChangedBlocksException {
24102455
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
24112456
expression.optimize();
2412-
return deformRegion(region, zero, unit, expression, timeout);
2457+
return deformRegion(region, transform, expression, timeout);
24132458
}
24142459

24152460
/**
@@ -2419,29 +2464,43 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
24192464
* The Expression class is subject to change. Expressions should be provided via the string overload.
24202465
* </p>
24212466
*/
2467+
@Deprecated
24222468
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final Expression expression,
24232469
final int timeout) throws ExpressionException, MaxChangedBlocksException {
2470+
return deformRegion(region, new SimpleTransform(zero, unit), expression, timeout);
2471+
}
2472+
2473+
/**
2474+
* Internal version of {@link EditSession#deformRegion(Region, Vector3, Vector3, String, int)}.
2475+
*
2476+
* <p>
2477+
* The Expression class is subject to change. Expressions should be provided via the string overload.
2478+
* </p>
2479+
*/
2480+
public int deformRegion(final Region region, final Transform transform, final Expression expression,
2481+
final int timeout) throws ExpressionException, MaxChangedBlocksException {
24242482
final Variable x = expression.getSlots().getVariable("x")
24252483
.orElseThrow(IllegalStateException::new);
24262484
final Variable y = expression.getSlots().getVariable("y")
24272485
.orElseThrow(IllegalStateException::new);
24282486
final Variable z = expression.getSlots().getVariable("z")
24292487
.orElseThrow(IllegalStateException::new);
24302488

2431-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero);
2489+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, transform);
24322490
expression.setEnvironment(environment);
24332491

24342492
final DoubleArrayList<BlockVector3, BaseBlock> queue = new DoubleArrayList<>(false);
24352493

2494+
final Transform transformInverse = transform.inverse();
24362495
for (BlockVector3 position : region) {
24372496
// transform
2438-
final Vector3 scaled = position.toVector3().subtract(zero).divide(unit);
2497+
final Vector3 scaled = transformInverse.apply(position.toVector3());
24392498

24402499
// deform
24412500
expression.evaluate(new double[]{ scaled.x(), scaled.y(), scaled.z() }, timeout);
24422501

24432502
// untransform, round-nearest
2444-
final BlockVector3 sourcePosition = environment.toWorld(x.value(), y.value(), z.value());
2503+
final BlockVector3 sourcePosition = transform.apply(Vector3.at(x.value(), y.value(), z.value())).add(0.5, 0.5, 0.5).toBlockPoint();
24452504

24462505
// read block from world
24472506
final BaseBlock material = world.getFullBlock(sourcePosition);
@@ -2749,28 +2808,36 @@ private void recurseHollow(Region region, BlockVector3 origin, Set<BlockVector3>
27492808
}
27502809
}
27512810

2811+
@Deprecated
27522812
public int makeBiomeShape(final Region region, final Vector3 zero, final Vector3 unit, final BiomeType biomeType,
27532813
final String expressionString, final boolean hollow) throws ExpressionException {
27542814
return makeBiomeShape(region, zero, unit, biomeType, expressionString, hollow, WorldEdit.getInstance().getConfiguration().calculationTimeout);
27552815
}
27562816

2817+
@Deprecated
27572818
public int makeBiomeShape(final Region region, final Vector3 zero, final Vector3 unit, final BiomeType biomeType,
27582819
final String expressionString, final boolean hollow, final int timeout) throws ExpressionException {
2820+
return makeBiomeShape(region, new SimpleTransform(zero, unit), biomeType, expressionString, hollow, timeout);
2821+
}
2822+
2823+
public int makeBiomeShape(final Region region, Transform transform, final BiomeType biomeType,
2824+
final String expressionString, final boolean hollow, final int timeout) throws ExpressionException {
27592825

27602826
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
27612827
expression.optimize();
27622828

27632829
final EditSession editSession = this;
2764-
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(editSession, unit, zero);
2830+
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(editSession, transform);
27652831
expression.setEnvironment(environment);
27662832

27672833
AtomicInteger timedOut = new AtomicInteger();
2834+
final Transform transformInverse = transform.inverse();
27682835
final ArbitraryBiomeShape shape = new ArbitraryBiomeShape(region) {
27692836
@Override
27702837
protected BiomeType getBiome(int x, int y, int z, BiomeType defaultBiomeType) {
27712838
final Vector3 current = Vector3.at(x, y, z);
27722839
environment.setCurrentBlock(current);
2773-
final Vector3 scaled = current.subtract(zero).divide(unit);
2840+
final Vector3 scaled = transformInverse.apply(current);
27742841

27752842
try {
27762843
if (expression.evaluate(new double[]{ scaled.x(), scaled.y(), scaled.z() }, timeout) <= 0) {

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

+6-68
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) {

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

+4-33
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,14 @@
4747
import com.sk89q.worldedit.internal.annotation.Offset;
4848
import com.sk89q.worldedit.internal.annotation.Selection;
4949
import com.sk89q.worldedit.internal.expression.ExpressionException;
50+
import com.sk89q.worldedit.internal.util.TransformUtil;
5051
import com.sk89q.worldedit.math.BlockVector3;
51-
import com.sk89q.worldedit.math.Vector3;
5252
import com.sk89q.worldedit.math.convolution.GaussianKernel;
5353
import com.sk89q.worldedit.math.convolution.HeightMap;
5454
import com.sk89q.worldedit.math.convolution.HeightMapFilter;
5555
import com.sk89q.worldedit.math.convolution.SnowHeightMap;
5656
import com.sk89q.worldedit.math.noise.RandomNoise;
57+
import com.sk89q.worldedit.math.transform.Transform;
5758
import com.sk89q.worldedit.regions.ConvexPolyhedralRegion;
5859
import com.sk89q.worldedit.regions.CuboidRegion;
5960
import com.sk89q.worldedit.regions.Region;
@@ -498,41 +499,11 @@ public int deform(Actor actor, LocalSession session, EditSession editSession,
498499
boolean offsetPlacement,
499500
@Switch(name = 'c', desc = "Use the selection's center as origin")
500501
boolean offsetCenter) throws WorldEditException {
501-
final Vector3 zero;
502-
Vector3 unit;
503-
504-
if (useRawCoords) {
505-
zero = Vector3.ZERO;
506-
unit = Vector3.ONE;
507-
} else if (offsetPlacement) {
508-
zero = session.getPlacementPosition(actor).toVector3();
509-
unit = Vector3.ONE;
510-
} else if (offsetCenter) {
511-
final Vector3 min = region.getMinimumPoint().toVector3();
512-
final Vector3 max = region.getMaximumPoint().toVector3();
513-
514-
zero = max.add(min).multiply(0.5);
515-
unit = Vector3.ONE;
516-
} else {
517-
final Vector3 min = region.getMinimumPoint().toVector3();
518-
final Vector3 max = region.getMaximumPoint().toVector3();
519-
520-
zero = max.add(min).divide(2);
521-
unit = max.subtract(zero);
522502

523-
if (unit.x() == 0) {
524-
unit = unit.withX(1.0);
525-
}
526-
if (unit.y() == 0) {
527-
unit = unit.withY(1.0);
528-
}
529-
if (unit.z() == 0) {
530-
unit = unit.withZ(1.0);
531-
}
532-
}
503+
final Transform transform = TransformUtil.createTransformForExpressionCommand(actor, session, region, useRawCoords, offsetPlacement, offsetCenter);
533504

534505
try {
535-
final int affected = editSession.deformRegion(region, zero, unit, String.join(" ", expression), session.getTimeout());
506+
final int affected = editSession.deformRegion(region, transform, String.join(" ", expression), session.getTimeout());
536507
if (actor instanceof Player) {
537508
((Player) actor).findFreePosition();
538509
}

0 commit comments

Comments
 (0)