Directly integrate a formula through a config file

Discussion in 'Plugin Development' started by silthus, Jun 7, 2011.

Thread Status:
Not open for further replies.
  1. Offline



    I already tried to google and find something about this topic but came up empty handed.
    How do I take a String from the config and make it into code so that it can be parsed as a formula?

    I have this method and I want to make the formula configurable but just cant figure it out:

    public int getExpToLevel(int level) {
            return ((((level) * (level)) - ((level) * 5) + 20));
    So basically I just need to give the user one variable in the config to work with (level) and I want to parse all the rest like brackets, numbers and signs.

    Any ideas anyone?
  2. Offline


    do you mean get the level from the file and make it an int so it can be used? I'm confused, but if so, just get the property normaly and go:

    int level = Integer.parseInt(string);

    and that will return the intiger value of the string:

  3. Offline


    No I am getting the level from a database.
    I want the User/Admin to be able to configure the complete formula so e.g.:

    Instead of
    ((((level) * (level)) - ((level) * 5) + 20))
    He would write:

    level * level
    in the config or :

    (level * 5) + 2
    And that gets parsed exactly like that into the method behind the return statement.
  4. Offline


    There are several math parser libraries available
    JEP and JBC are commercial packages but there are free trials available.
    So I think the best(and legal) option is JEPLight

    Of course you can also write your own parser
  5. Offline


    • Java has a built-in scripting engine for javascript you could use.
    • You could force the user to input the expression in reversed polish notation, which you can interpret VERY easily.
    • You could implement a shunting yard to convert an expression into reversed polish notation
  6. Offline


    Thanks for your tips. I ended up using JEPLight since it is exactly what I need =)
Thread Status:
Not open for further replies.

Share This Page