mirror of
https://github.com/EngineHub/WorldEdit.git
synced 2025-03-01 13:36:46 +08:00
Pre-compile the expression used in the deform brush.
This lets the command fail early, rather than throwing wrapped runtime exceptions on every brush usage.
This commit is contained in:
parent
3fdb10ad15
commit
cd9a45bd6b
@ -2139,7 +2139,11 @@ public int deformRegion(final Region region, final Vector3 zero, final Vector3 u
|
||||
final int timeout) throws ExpressionException, MaxChangedBlocksException {
|
||||
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
|
||||
expression.optimize();
|
||||
return deformRegion(region, zero, unit, expression, timeout);
|
||||
}
|
||||
|
||||
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")
|
||||
|
@ -33,6 +33,7 @@
|
||||
import com.sk89q.worldedit.function.EditContext;
|
||||
import com.sk89q.worldedit.function.operation.Operation;
|
||||
import com.sk89q.worldedit.function.operation.RunContext;
|
||||
import com.sk89q.worldedit.internal.expression.Expression;
|
||||
import com.sk89q.worldedit.internal.expression.ExpressionException;
|
||||
import com.sk89q.worldedit.math.Vector3;
|
||||
import com.sk89q.worldedit.regions.NullRegion;
|
||||
@ -46,8 +47,9 @@ public class Deform implements Contextual<Operation> {
|
||||
|
||||
private Extent destination;
|
||||
private Region region;
|
||||
private String expression;
|
||||
private Mode mode = Mode.UNIT_CUBE;
|
||||
private String expressionString;
|
||||
private Expression expression;
|
||||
private Mode mode;
|
||||
private Vector3 offset = Vector3.ZERO;
|
||||
|
||||
public Deform(String expression) {
|
||||
@ -65,14 +67,20 @@ public Deform(Extent destination, Region region, String expression) {
|
||||
public Deform(Extent destination, Region region, String expression, Mode mode) {
|
||||
checkNotNull(destination, "destination");
|
||||
checkNotNull(region, "region");
|
||||
checkNotNull(expression, "expression");
|
||||
checkNotNull(mode, "mode");
|
||||
checkAndSetExpression(expression);
|
||||
this.destination = destination;
|
||||
this.region = region;
|
||||
this.expression = expression;
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
private void checkAndSetExpression(String expressionString) {
|
||||
checkNotNull(expressionString, "expression");
|
||||
this.expression = Expression.compile(expressionString, "x", "y", "z");
|
||||
this.expression.optimize();
|
||||
this.expressionString = expressionString;
|
||||
}
|
||||
|
||||
public Extent getDestination() {
|
||||
return destination;
|
||||
}
|
||||
@ -91,13 +99,12 @@ public void setRegion(Region region) {
|
||||
this.region = region;
|
||||
}
|
||||
|
||||
public String getExpression() {
|
||||
return expression;
|
||||
public String getExpressionString() {
|
||||
return expressionString;
|
||||
}
|
||||
|
||||
public void setExpression(String expression) {
|
||||
checkNotNull(expression, "expression");
|
||||
this.expression = expression;
|
||||
public void setExpressionString(String expressionString) {
|
||||
checkAndSetExpression(expressionString);
|
||||
}
|
||||
|
||||
public Mode getMode() {
|
||||
@ -120,7 +127,7 @@ public void setOffset(Vector3 offset) {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "deformation of " + expression;
|
||||
return "deformation of " + expressionString;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -153,7 +160,7 @@ public Operation createFromContext(final EditContext context) {
|
||||
}
|
||||
|
||||
LocalSession session = context.getSession();
|
||||
return new DeformOperation(context.getDestination(), region, zero, unit, expression,
|
||||
return new DeformOperation(context.getDestination(), region, zero, unit, expression, expressionString,
|
||||
session == null ? WorldEdit.getInstance().getConfiguration().calculationTimeout : session.getTimeout());
|
||||
}
|
||||
|
||||
@ -162,15 +169,18 @@ private static final class DeformOperation implements Operation {
|
||||
private final Region region;
|
||||
private final Vector3 zero;
|
||||
private final Vector3 unit;
|
||||
private final String expression;
|
||||
private final Expression expression;
|
||||
private final String expressionString;
|
||||
private final int timeout;
|
||||
|
||||
private DeformOperation(Extent destination, Region region, Vector3 zero, Vector3 unit, String expression, int timeout) {
|
||||
private DeformOperation(Extent destination, Region region, Vector3 zero, Vector3 unit, Expression expression,
|
||||
String expressionString, int timeout) {
|
||||
this.destination = destination;
|
||||
this.region = region;
|
||||
this.zero = zero;
|
||||
this.unit = unit;
|
||||
this.expression = expression;
|
||||
this.expressionString = expressionString;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
@ -193,7 +203,7 @@ public void cancel() {
|
||||
@Override
|
||||
public Iterable<Component> getStatusMessages() {
|
||||
return ImmutableList.of(TranslatableComponent.of("worldedit.operation.deform.expression",
|
||||
TextComponent.of(expression).color(TextColor.LIGHT_PURPLE)));
|
||||
TextComponent.of(expressionString).color(TextColor.LIGHT_PURPLE)));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user