Search in sources :

Example 1 with ExpressionException

use of com.sk89q.worldedit.internal.expression.ExpressionException in project FastAsyncWorldEdit by IntellectualSites.

the class GenerationCommands method generateBiome.

@Command(name = "/generatebiome", aliases = { "/genbiome", "/gb" }, desc = "Sets biome according to a formula.", descFooter = "Formula must return positive numbers (true) if the point is inside the shape\n" + "Sets the biome of blocks in that shape.\n" + "For details, see https://ehub.to/we/expr")
@CommandPermissions("worldedit.generation.shape.biome")
@Logging(ALL)
@Preload(Preload.PreloadCheck.PRELOAD)
@Confirm(Confirm.Processor.REGION)
public int generateBiome(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "The biome type to set") BiomeType target, @Arg(desc = "Expression to test block placement locations and set biome type. Requires x,y,z variables be present", variable = true) List<String> expression, @Switch(name = 'h', desc = "Generate a hollow shape") boolean hollow, @Switch(name = 'r', desc = "Use the game's coordinate origin") boolean useRawCoords, @Switch(name = 'o', desc = "Use the placement's coordinate origin") boolean offset, @Switch(name = 'c', desc = "Use the selection's center as origin") boolean offsetCenter) throws WorldEditException {
    final Vector3 zero;
    Vector3 unit;
    if (useRawCoords) {
        zero = Vector3.ZERO;
        unit = Vector3.ONE;
    } else if (offset) {
        zero = session.getPlacementPosition(actor).toVector3();
        unit = Vector3.ONE;
    } else if (offsetCenter) {
        final Vector3 min = region.getMinimumPoint().toVector3();
        final Vector3 max = region.getMaximumPoint().toVector3();
        zero = max.add(min).multiply(0.5);
        unit = Vector3.ONE;
    } else {
        final Vector3 min = region.getMinimumPoint().toVector3();
        final Vector3 max = region.getMaximumPoint().toVector3();
        zero = max.add(min).multiply(0.5);
        unit = max.subtract(zero);
        if (unit.getX() == 0) {
            unit = unit.withX(1.0);
        }
        if (unit.getY() == 0) {
            unit = unit.withY(1.0);
        }
        if (unit.getZ() == 0) {
            unit = unit.withZ(1.0);
        }
    }
    final Vector3 unit1 = unit;
    try {
        final int affected = editSession.makeBiomeShape(region, zero, unit1, target, String.join(" ", expression), hollow, session.getTimeout());
        actor.print(Caption.of("worldedit.generatebiome.changed", TextComponent.of(affected)));
        return affected;
    } catch (ExpressionException e) {
        actor.printError(TextComponent.of(e.getMessage()));
        return 0;
    }
}
Also used : BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) ExpressionException(com.sk89q.worldedit.internal.expression.ExpressionException) Logging(com.sk89q.worldedit.command.util.Logging) Preload(com.sk89q.worldedit.command.util.annotation.Preload) Command(org.enginehub.piston.annotation.Command) Confirm(com.sk89q.worldedit.command.util.annotation.Confirm) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 2 with ExpressionException

use of com.sk89q.worldedit.internal.expression.ExpressionException in project FastAsyncWorldEdit by IntellectualSites.

the class RegionCommands method deform.

@Command(name = "/deform", desc = "Deforms a selected region with an expression", descFooter = "The expression is executed for each block and is expected\n" + "to modify the variables x, y and z to point to a new block\n" + "to fetch. For details, see https://ehub.to/we/expr")
@CommandPermissions("worldedit.region.deform")
@Logging(ALL)
@Preload(Preload.PreloadCheck.PRELOAD)
@Confirm(Confirm.Processor.REGION)
public int deform(Actor actor, LocalSession session, EditSession editSession, @Selection Region region, @Arg(desc = "The expression to use", variable = true) List<String> expression, @Switch(name = 'r', desc = "Use the game's coordinate origin") boolean useRawCoords, @Switch(name = 'o', desc = "Use the placement's coordinate origin") boolean offset, @Switch(name = 'c', desc = "Use the selection's center as origin") boolean offsetCenter) throws WorldEditException {
    final Vector3 zero;
    Vector3 unit;
    if (useRawCoords) {
        zero = Vector3.ZERO;
        unit = Vector3.ONE;
    } else if (offsetCenter) {
        final Vector3 min = region.getMinimumPoint().toVector3();
        final Vector3 max = region.getMaximumPoint().toVector3();
        zero = max.add(min).multiply(0.5);
        unit = Vector3.ONE;
    } else if (offset) {
        zero = session.getPlacementPosition(actor).toVector3();
        unit = Vector3.ONE;
    } else {
        final Vector3 min = region.getMinimumPoint().toVector3();
        final Vector3 max = region.getMaximumPoint().toVector3();
        zero = max.add(min).divide(2);
        unit = max.subtract(zero);
        if (unit.getX() == 0) {
            unit = unit.withX(1.0);
        }
        if (unit.getY() == 0) {
            unit = unit.withY(1.0);
        }
        if (unit.getZ() == 0) {
            unit = unit.withZ(1.0);
        }
    }
    final Vector3 unit1 = unit;
    try {
        final int affected = editSession.deformRegion(region, zero, unit1, String.join(" ", expression), session.getTimeout());
        if (actor instanceof Player) {
            ((Player) actor).findFreePosition();
        }
        actor.print(Caption.of("worldedit.deform.deformed", TextComponent.of(affected)));
        return affected;
    } catch (ExpressionException e) {
        actor.printError(TextComponent.of(e.getMessage()));
        return 0;
    }
}
Also used : Player(com.sk89q.worldedit.entity.Player) BlockVector3(com.sk89q.worldedit.math.BlockVector3) Vector3(com.sk89q.worldedit.math.Vector3) ExpressionException(com.sk89q.worldedit.internal.expression.ExpressionException) Logging(com.sk89q.worldedit.command.util.Logging) Preload(com.sk89q.worldedit.command.util.annotation.Preload) Command(org.enginehub.piston.annotation.Command) Confirm(com.sk89q.worldedit.command.util.annotation.Confirm) CommandPermissions(com.sk89q.worldedit.command.util.CommandPermissions)

Example 3 with ExpressionException

use of com.sk89q.worldedit.internal.expression.ExpressionException 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 4 with ExpressionException

use of com.sk89q.worldedit.internal.expression.ExpressionException 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 5 with ExpressionException

use of com.sk89q.worldedit.internal.expression.ExpressionException 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)

Aggregations

ExpressionException (com.sk89q.worldedit.internal.expression.ExpressionException)8 Expression (com.sk89q.worldedit.internal.expression.Expression)5 CommandPermissions (com.sk89q.worldedit.command.util.CommandPermissions)4 BlockVector3 (com.sk89q.worldedit.math.BlockVector3)4 Vector3 (com.sk89q.worldedit.math.Vector3)4 WorldEditExpressionEnvironment (com.sk89q.worldedit.regions.shape.WorldEditExpressionEnvironment)4 Command (org.enginehub.piston.annotation.Command)4 Logging (com.sk89q.worldedit.command.util.Logging)3 Confirm (com.sk89q.worldedit.command.util.annotation.Confirm)3 Preload (com.sk89q.worldedit.command.util.annotation.Preload)2 Player (com.sk89q.worldedit.entity.Player)2 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 ExpressionMask (com.sk89q.worldedit.function.mask.ExpressionMask)1 Variable (com.sk89q.worldedit.internal.expression.LocalSlot.Variable)1