Search in sources :

Example 1 with Expression

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())));
    }
}
Also used : InputParseException(com.sk89q.worldedit.extension.input.InputParseException) WorldEditExpressionEnvironment(com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment) Expression(com.sk89q.worldedit.internal.expression.Expression) IntSupplier(java.util.function.IntSupplier) SessionOwner(com.sk89q.worldedit.session.SessionOwner) ExpressionException(com.sk89q.worldedit.internal.expression.ExpressionException) ExpressionMask(com.sk89q.worldedit.function.mask.ExpressionMask)

Example 2 with Expression

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);
}
Also used : Expression(com.sk89q.worldedit.internal.expression.Expression)

Example 3 with Expression

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())));
    }
}
Also used : InputParseException(com.sk89q.worldedit.extension.input.InputParseException) WorldEditExpressionEnvironment(com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment) Expression(com.sk89q.worldedit.internal.expression.Expression) ExpressionPattern(com.fastasyncworldedit.core.function.pattern.ExpressionPattern) ExpressionException(com.sk89q.worldedit.internal.expression.ExpressionException)

Example 4 with Expression

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);
}
Also used : Expression(com.sk89q.worldedit.internal.expression.Expression) ExpressionException(com.sk89q.worldedit.internal.expression.ExpressionException) Command(org.enginehub.piston.annotation.Command) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 5 with Expression

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;
}
Also used : BiomeType(com.sk89q.worldedit.world.biome.BiomeType) WorldEditExpressionEnvironment(com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment) Expression(com.sk89q.worldedit.internal.expression.Expression) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArbitraryBiomeShape(com.sk89q.worldedit.regions.shape.ArbitraryBiomeShape) ExpressionTimeoutException(com.sk89q.worldedit.internal.expression.ExpressionTimeoutException) ExpressionException(com.sk89q.worldedit.internal.expression.ExpressionException) EvaluationException(com.sk89q.worldedit.internal.expression.EvaluationException) IOException(java.io.IOException) ExpressionTimeoutException(com.sk89q.worldedit.internal.expression.ExpressionTimeoutException)

Aggregations

Expression (com.sk89q.worldedit.internal.expression.Expression)7 ExpressionException (com.sk89q.worldedit.internal.expression.ExpressionException)5 WorldEditExpressionEnvironment (com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment)4 InputParseException (com.sk89q.worldedit.extension.input.InputParseException)2 EvaluationException (com.sk89q.worldedit.internal.expression.EvaluationException)2 ExpressionTimeoutException (com.sk89q.worldedit.internal.expression.ExpressionTimeoutException)2 IOException (java.io.IOException)2 ExpressionPattern (com.fastasyncworldedit.core.function.pattern.ExpressionPattern)1 MutableBlockVector3 (com.fastasyncworldedit.core.math.MutableBlockVector3)1 MutableVector3 (com.fastasyncworldedit.core.math.MutableVector3)1 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)1 ExpressionMask (com.sk89q.worldedit.function.mask.ExpressionMask)1 Variable (com.sk89q.worldedit.internal.expression.LocalSlot.Variable)1 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)1 Vector3 (com.sk89q.worldedit.math.Vector3)1 ArbitraryBiomeShape (com.sk89q.worldedit.regions.shape.ArbitraryBiomeShape)1 ArbitraryShape (com.sk89q.worldedit.regions.shape.ArbitraryShape)1 SessionOwner (com.sk89q.worldedit.session.SessionOwner)1 BiomeType (com.sk89q.worldedit.world.biome.BiomeType)1 BaseBlock (com.sk89q.worldedit.world.block.BaseBlock)1