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

    silthus

    Hello,

    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:

    Code:
    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

    DreadKyller

    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:

    "5"->5
    "165"->165
     
  3. Offline

    silthus

    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
    Code:
    ((((level) * (level)) - ((level) * 5) + 20))
    He would write:

    Code:
    level * level
    in the config or :

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

    masteroftime

    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

    Shamebot

    • 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

    silthus

    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