use of com.sk89q.worldedit.internal.expression.Expression in project FastAsyncWorldEdit by IntellectualSites.
the class ExpressionMaskParser method parseFromInput.
@Override
public Mask parseFromInput(String input, ParserContext context) throws InputParseException {
if (!input.startsWith("=")) {
return null;
}
// FAWE start - richer parsing
if (input.charAt(1) == '[') {
int end = input.lastIndexOf(']');
if (end == -1) {
return null;
}
input = input.substring(2, end);
} else {
input = input.substring(1);
}
try {
// FAWE start - richer parsing
Expression exp = Expression.compile(input, "x", "y", "z");
// FAWE end
WorldEditExpressionEnvironment env = new WorldEditExpressionEnvironment(context.requireExtent(), Vector3.ONE, Vector3.ZERO);
exp.setEnvironment(env);
if (context.getActor() != null) {
SessionOwner owner = context.getActor();
IntSupplier timeout = () -> WorldEdit.getInstance().getSessionManager().get(owner).getTimeout();
return new ExpressionMask(exp, timeout);
}
return new ExpressionMask(exp);
} catch (ExpressionException e) {
throw new InputParseException(Caption.of("worldedit.error.parser.invalid-expression", TextComponent.of(e.getMessage())));
}
}
use of com.sk89q.worldedit.internal.expression.Expression in project FastAsyncWorldEdit by IntellectualSites.
the class EditSession method deformRegion.
/**
* Deforms the region by a given expression. A deform provides a block's x, y, and z coordinates (possibly scaled)
* to an expression, and then sets the block to the block given by the resulting values of the variables, if they
* have changed.
*
* @param region the region to deform
* @param zero the origin of the coordinate system
* @param unit the scale of the coordinate system
* @param expressionString the expression to evaluate for each block
* @param timeout maximum time for the expression to evaluate for each block. -1 for unlimited.
* @return number of blocks changed
* @throws ExpressionException thrown on invalid expression input
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final String expressionString, final int timeout) throws ExpressionException, MaxChangedBlocksException {
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
expression.optimize();
return deformRegion(region, zero, unit, expression, timeout);
}
use of com.sk89q.worldedit.internal.expression.Expression in project FastAsyncWorldEdit by IntellectualSites.
the class ExpressionPatternParser method parseFromSimpleInput.
@Override
public Pattern parseFromSimpleInput(String input, ParserContext context) throws InputParseException {
try {
Expression exp = Expression.compile(input.substring(1), "x", "y", "z");
WorldEditExpressionEnvironment env = new WorldEditExpressionEnvironment(context.requireExtent(), Vector3.ONE, Vector3.ZERO);
exp.setEnvironment(env);
return new ExpressionPattern(exp);
} catch (ExpressionException e) {
throw new InputParseException(Caption.of("worldedit.error.parser.invalid-expression", TextComponent.of(e.getMessage())));
}
}
use of com.sk89q.worldedit.internal.expression.Expression in project FastAsyncWorldEdit by IntellectualSites.
the class UtilityCommands method calc.
@Command(name = "/calculate", aliases = { "/calc", "/eval", "/evaluate", "/solve" }, desc = "Evaluate a mathematical expression")
@CommandPermissions("worldedit.calc")
public void calc(Actor actor, @Arg(desc = "Expression to evaluate", variable = true) List<String> input) {
Expression expression;
try {
expression = Expression.compile(String.join(" ", input));
} catch (ExpressionException e) {
actor.print(Caption.of("worldedit.calc.invalid.with-error", TextComponent.of(String.join(" ", input)), TextComponent.of(e.getMessage())));
return;
}
WorldEditAsyncCommandBuilder.createAndSendMessage(actor, () -> {
double result = expression.evaluate(new double[] {}, WorldEdit.getInstance().getSessionManager().get(actor).getTimeout());
String formatted = Double.isNaN(result) ? "NaN" : formatForLocale(actor.getLocale()).format(result);
return SubtleFormat.wrap(input + " = ").append(TextComponent.of(formatted, TextColor.LIGHT_PURPLE));
}, (Component) null);
}
use of com.sk89q.worldedit.internal.expression.Expression in project FastAsyncWorldEdit by IntellectualSites.
the class EditSession method makeBiomeShape.
public int makeBiomeShape(final Region region, final Vector3 zero, final Vector3 unit, final BiomeType biomeType, final String expressionString, final boolean hollow, final int timeout) throws ExpressionException {
final Expression expression = Expression.compile(expressionString, "x", "y", "z");
expression.optimize();
final EditSession editSession = this;
final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(editSession, unit, zero);
expression.setEnvironment(environment);
AtomicInteger timedOut = new AtomicInteger();
final ArbitraryBiomeShape shape = new ArbitraryBiomeShape(region) {
@Override
protected BiomeType getBiome(int x, int y, int z, BiomeType defaultBiomeType) {
environment.setCurrentBlock(x, y, z);
double scaledX = (x - zero.getX()) / unit.getX();
double scaledY = (y - zero.getY()) / unit.getY();
double scaledZ = (z - zero.getZ()) / unit.getZ();
try {
if (expression.evaluate(new double[] { scaledX, scaledY, scaledZ }, timeout) <= 0) {
return null;
}
// TODO: Allow biome setting via a script variable (needs BiomeType<->int mapping)
return defaultBiomeType;
} catch (ExpressionTimeoutException e) {
timedOut.getAndIncrement();
return null;
} catch (Exception e) {
LOGGER.warn("Failed to create shape", e);
return null;
}
}
};
int changed = shape.generate(this, biomeType, hollow);
if (timedOut.get() > 0) {
throw new ExpressionTimeoutException(String.format("%d biomes changed. %d biomes took too long to evaluate (increase time with //timeout)", changed, timedOut.get()));
}
return changed;
}
Aggregations