@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()); } } }
private double simpleEval(String expressionString) throws ExpressionException { final Expression expression = compile(expressionString); expression.setEnvironment(new ExpressionEnvironment() { @Override public int getBlockType(double x, double y, double z) { return (int) x; } @Override public int getBlockData(double x, double y, double z) { return (int) y; } @Override public int getBlockTypeAbs(double x, double y, double z) { return (int) x*10; } @Override public int getBlockDataAbs(double x, double y, double z) { return (int) y*10; } @Override public int getBlockTypeRel(double x, double y, double z) { return (int) x*100; } @Override public int getBlockDataRel(double x, double y, double z) { return (int) y*100; } }); return expression.evaluate(); }
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); }
expression.setEnvironment(environment);