I’m helping a friend work on a rpg mod in Java, we basically want to give the administrators of the server a chance to provide his/her own formulas to calculate the outcome of some events.
For instance right now we have an event that happens 1% of the time, and the chance increases by 1% every level of the character. How can we make it so that the server admin is able to specify a function based on the character level that returns a chance for that event to happen?
Is there a way to do that safely, since Java has no direct lambda support? Possibly without having to write a tokenizer that parses the string and executes the function?
In php I might use eval to do such a thing (once I sanitized the input) but in Java I’m stumped.
Why not use some scripting language, like Groovy, that can very nicely integrate with Java (you can pass parameters from Java and get the result back to Java). Then you don’t have to write your own parser and could also go beyond simple math formulas if the need arose. (Be aware that on the other hand you might have to restrict the use of Groovy, to prevent some security issues, see for instance here how to do this.)