fix: set and clone Expression Environment after Expression cloning (#2617)

- Fixes #2616
This commit is contained in:
MineFact 2024-03-15 11:27:25 +01:00 committed by GitHub
parent facd31ce31
commit df9527b0b7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 4 deletions

View File

@ -24,6 +24,7 @@ import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.antlr.ExpressionLexer;
import com.sk89q.worldedit.antlr.ExpressionParser;
import com.sk89q.worldedit.internal.expression.invoke.ExpressionCompiler;
import com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
@ -199,7 +200,9 @@ public class Expression implements Cloneable {
//FAWE start
public Expression clone() {
return new Expression(initialExpression, new HashSet<>(providedSlots));
Expression expression = new Expression(initialExpression, new HashSet<>(providedSlots));
expression.setEnvironment(getEnvironment().clone());
return expression;
}
//FAWE end

View File

@ -22,7 +22,7 @@ package com.sk89q.worldedit.internal.expression;
/**
* Represents a way to access blocks in a world. Has to accept non-rounded coordinates.
*/
public interface ExpressionEnvironment {
public interface ExpressionEnvironment extends Cloneable {
int getBlockType(double x, double y, double z);
@ -36,4 +36,7 @@ public interface ExpressionEnvironment {
int getBlockDataRel(double x, double y, double z);
// FAWE start
ExpressionEnvironment clone();
// FAWE end
}

View File

@ -28,6 +28,8 @@ import com.sk89q.worldedit.math.Vector3;
public class WorldEditExpressionEnvironment implements ExpressionEnvironment {
private static final Vector3 BLOCK_CENTER_OFFSET = Vector3.at(0.5, 0.5, 0.5);
private final Vector3 unit;
private final Vector3 zero2;
//FAWE start - MutableVector3
@ -42,7 +44,7 @@ public class WorldEditExpressionEnvironment implements ExpressionEnvironment {
public WorldEditExpressionEnvironment(Extent extent, Vector3 unit, Vector3 zero) {
this.extent = extent;
this.unit = unit;
this.zero2 = zero.add(0.5, 0.5, 0.5);
this.zero2 = zero.add(BLOCK_CENTER_OFFSET);
}
public BlockVector3 toWorld(double x, double y, double z) {
@ -94,10 +96,13 @@ public class WorldEditExpressionEnvironment implements ExpressionEnvironment {
public Vector3 toWorldRel(double x, double y, double z) {
return current.add(x, y, z);
}
public WorldEditExpressionEnvironment clone() {
return new WorldEditExpressionEnvironment(extent, unit, zero2.subtract(BLOCK_CENTER_OFFSET));
}
//FAWe end
public void setCurrentBlock(Vector3 current) {
this.current = current;
}
}

View File

@ -114,6 +114,11 @@ class BaseExpressionTest {
public int getBlockDataRel(double x, double y, double z) {
return (int) y * 100;
}
@Override
public ExpressionEnvironment clone() {
return this;
}
});
return expression.evaluate();