tweaking your script engine for Java 8

Joined
Nov 14, 2008
Messages
1,016
Reaction score
629
With the release of Java 8 comes the new nashorn script engine that replaces the old deprecated piece of garbage that is Rhino. It is orders of magnitude faster and will benefit you in your journey. If you haven't already updated to Java 8, then what the **** are you waiting for? We get lambdas, the new awesome date API, the collection stream API, etc. This is the great Java update in a while

now if you do update to Java 8, you'll see that the whole source collapses upon starting and trying to load event scripts, and a good amount of npc scripts. That's because importPackage was removed and you need to import mozilla_compat.js from nashorn. Now i'm not going to add that one line of code to all the damn scripts, it would be annoying.

this is for moopledev but I don't think the shitty script interface from odinms was ever touched again so you should be good with this rls

go to AbstractScriptManager

under
PHP:
if (c != null) {
				c.setScriptEngine(path, engine);
			}

replace old **** with
PHP:
try (Stream<String> stream = Files.lines(scriptFile.toPath())) {
				String lines = "load('nashorn:mozilla_compat.js');";
				lines += stream.collect(Collectors.joining(System.lineSeparator()));
				engine.eval(lines);
			} catch (final ScriptException | IOException t) {
				if (ServerConstants.VPS)
					FilePrinter.printError(FilePrinter.INVOCABLE + path.substring(12, path.length()), t, path);
				else 
					System.out.println(t);
					return null;
			}

notice how I use the great collection's stream to my advantage; look how elegant that code is

now you can get all the benefits of Java 8 without touching your code :junglejane:

if you wish to know the list of awesome changes that were included in Java 8, this may be of interest
 
Last edited:


:P
rice : how's C# doing?

I already use Groovy because it is the superior JVM language. Java looks weak and useless and for being the main JVM language, it looks quite miserable in front of Groovy. I am in fact working on a Groovy source which will all be on github when I get some actual work done. (it's still in the thought process I guess you could say) It'll be a flexible maplestory emulator where adding functionality will be done in a process similar to the Bukkit plugin API from Minecraft.
 
It lets someone use java 8 without getting a error from the script engine change. No? No fucking clue then.

Like oxysoft mentioned, Java 8 introduced the new scripting interface called Nashorn (instead of Rhino). The new interface removed the usage of "importPackage" (which exists in a lot of the event scripts in awful Odin), and therefore you had to remove it manually for them to function (and compile correctly (or load, god knows what Java does with them)). oxysoft's piece of code does it automatically, additionally, it uses new elements from Java 8 (Stream/collection).

I couldn't have explained it better.
 
Like oxysoft mentioned, Java 8 introduced the new scripting interface called Nashorn (instead of Rhino). The new interface removed the usage of "importPackage" (which exists in a lot of the event scripts in awful Odin), and therefore you had to remove it manually for them to function (and compile correctly (or load, god knows what Java does with them)). oxysoft's piece of code does it automatically, additionally, it uses new elements from Java 8 (Stream/collection).

I couldn't have explained it better.
PLEASE tell me if I am wrong about this..but I don't think it needs to remove them, because the new engine seems to have those functions in them and code implements code so Nashorn recognizes ImportPackage? I'm not sure.. I was reading up on it and that's what I saw.
 
it doesn't remove importPackage, nashorn doesn't like importPackage for security reasons I believe, you need to import nashorn:mozilla_compat.js for importPackage to be valid

actually, notepad++ has the feature to "replace in files"

but everyone uses sublime text because it doesn't look like **** unlike notepad++
 
oxysoft - tweaking your script engine for Java 8 - RaGEZONE Forums

Am i doing it right? o;
 
You can probably simply do

Code:
                engine.eval("load('nashorn:mozilla_compat.js');");
Before loading the script file.

However search/replace seems like a much better idea as it does not **** with line numbers in the script.

Also: I don't get the Groovy fandom but whatever. Scala is a very solid JVM language though.
 
Back