|
|
A friend of mine has a server, and his players wanted a math command. So I wrote this up relatively quick for him and though I would release to anyone who wants to use it. It's pretty neat.
In your Misc class add this at the top:
Then somewhere else in the class add:Code:import javax.script.ScriptEngine; import javax.script.ScriptEngineManager;
Then in your Commands class add:Code:private static ScriptEngineManager manager = new ScriptEngineManager(); private static ScriptEngine engine = manager.getEngineByName("JavaScript"); public static int parse(String text){ try{ Double d = (Double) engine.eval(text); return d.intValue(); }catch(Exception e){ throw new NumberFormatException(e.getLocalizedMessage()); } }
Thanks Matt123337 for the improved code above ^Code:if(playerCommand.startsWith("math")){ String expression = playerCommand.substring(playerCommand.indexOf(' ') + 1); c.sendMessage(expression + " = " + Misc.parse(expression)); }
And you are done![]()
This is kinda neat, I'll put it in my server![]()
Whats it do exactly?
Cute

Good job. Could you please release a command that helps to correct a players spelling mistakes?
| « Simple probability base | SendWeapon » |
| Thread Information |
Users Browsing this ThreadThere are currently 1 users browsing this thread. (0 members and 1 guests) |