@ExceptionMatch public void convert(ExpressionException e) throws CommandException { throw new CommandException(e.getMessage()); }
/** * 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())); } } }
@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())); } }
player.print(affected + " block(s) have been deformed."); } catch (ExpressionException e) { player.printError(e.getMessage());
player.print("" + affected + " columns affected."); } catch (ExpressionException e) { player.printError(e.getMessage());
player.print(affected + " block(s) have been created."); } catch (ExpressionException e) { player.printError(e.getMessage());