/** * Create a new instance. * * @param expression the expression * @throws ExpressionException thrown if there is an error with the expression */ public ExpressionMask(String expression) throws ExpressionException { checkNotNull(expression); this.expression = Expression.compile(expression, "x", "y", "z"); }
/** * Create a new instance. * * @param expression the expression * @throws ExpressionException thrown if there is an error with the expression */ public ExpressionMask2D(String expression) throws ExpressionException { checkNotNull(expression); this.expression = Expression.compile(expression, "x", "z"); }
/** * Try to parse numeric input as either a number or a mathematical expression. * * @param input input * @return a number * @throws ParameterException thrown on parse error */ private @Nullable Double parseNumericInput(@Nullable String input) throws ParameterException { if (input == null) { return null; } try { return Double.parseDouble(input); } catch (NumberFormatException e1) { try { Expression expression = Expression.compile(input); return expression.evaluate(); } catch (EvaluationException e) { throw new ParameterException(String.format( "Expected '%s' to be a valid number (or a valid mathematical expression)", input)); } catch (ExpressionException e) { throw new ParameterException(String.format( "Expected '%s' to be a number or valid math expression (error: %s)", input, e.getMessage())); } } }
private Expression compile(String expressionString, String... variableNames) throws ExpressionException, EvaluationException { final Expression expression = Expression.compile(expressionString, variableNames); expression.optimize(); return expression; } }
@Override public Mask parseFromInput(String input, ParserContext context) throws InputParseException { if (!input.startsWith("=")) { return null; } try { Expression exp = Expression.compile(input.substring(1), "x", "y", "z"); WorldEditExpressionEnvironment env = new WorldEditExpressionEnvironment( Request.request().getEditSession(), Vector3.ONE, Vector3.ZERO); exp.setEnvironment(env); return new ExpressionMask(exp); } catch (ExpressionException e) { throw new InputParseException("Invalid expression: " + e.getMessage()); } } }
@Command( aliases = { "/calc", "/calculate", "/eval", "/evaluate", "/solve" }, usage = "<expression>", desc = "Evaluate a mathematical expression" ) @CommandPermissions("worldedit.calc") public void calc(Actor actor, @Text String input) throws CommandException { try { Expression expression = Expression.compile(input); actor.print("= " + expression.evaluate()); } catch (EvaluationException e) { actor.printError(String.format( "'%s' could not be parsed as a valid expression", input)); } catch (ExpressionException e) { actor.printError(String.format( "'%s' could not be evaluated (error: %s)", input, e.getMessage())); } }
public int makeShape(final Region region, final Vector3 zero, final Vector3 unit, final Pattern pattern, final String expressionString, final boolean hollow) throws ExpressionException, MaxChangedBlocksException { final Expression expression = Expression.compile(expressionString, "x", "y", "z", "type", "data"); expression.optimize(); final RValue typeVariable = expression.getVariable("type", false); final RValue dataVariable = expression.getVariable("data", false); final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(this, unit, zero); expression.setEnvironment(environment); final ArbitraryShape shape = new ArbitraryShape(region) { @Override protected BaseBlock getMaterial(int x, int y, int z, BaseBlock defaultMaterial) { final Vector3 current = Vector3.at(x, y, z); environment.setCurrentBlock(current); final Vector3 scaled = current.subtract(zero).divide(unit); try { if (expression.evaluate(scaled.getX(), scaled.getY(), scaled.getZ(), defaultMaterial.getBlockType().getLegacyId(), 0) <= 0) { // TODO data return null; } return LegacyMapper.getInstance().getBlockFromLegacy((int) typeVariable.getValue(), (int) dataVariable.getValue()).toBaseBlock(); } catch (Exception e) { log.log(Level.WARNING, "Failed to create shape", e); return null; } } }; return shape.generate(this, pattern, hollow); }
public int makeBiomeShape(final Region region, final Vector3 zero, final Vector3 unit, final BaseBiome biomeType, final String expressionString, final boolean hollow) throws ExpressionException, MaxChangedBlocksException { final Vector2 zero2D = zero.toVector2(); final Vector2 unit2D = unit.toVector2(); final Expression expression = Expression.compile(expressionString, "x", "z"); expression.optimize(); final EditSession editSession = this; final WorldEditExpressionEnvironment environment = new WorldEditExpressionEnvironment(editSession, unit, zero); expression.setEnvironment(environment); final ArbitraryBiomeShape shape = new ArbitraryBiomeShape(region) { @Override protected BaseBiome getBiome(int x, int z, BaseBiome defaultBiomeType) { final Vector2 current = Vector2.at(x, z); environment.setCurrentBlock(current.toVector3(0)); final Vector2 scaled = current.subtract(zero2D).divide(unit2D); try { if (expression.evaluate(scaled.getX(), scaled.getZ()) <= 0) { return null; // TODO should return OUTSIDE? seems to cause issues otherwise, workedaround for now } // TODO: Allow biome setting via a script variable (needs BiomeType<->int mapping) return defaultBiomeType; } catch (Exception e) { log.log(Level.WARNING, "Failed to create shape", e); return null; } } }; return shape.generate(this, biomeType, hollow); }
public int deformRegion(final Region region, final Vector3 zero, final Vector3 unit, final String expressionString) throws ExpressionException, MaxChangedBlocksException { final Expression expression = Expression.compile(expressionString, "x", "y", "z"); expression.optimize();