Skip to content

Commit

Permalink
default imports java.lang, java.util and org.bukkit
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Mar 12, 2020
1 parent 2985a79 commit c791f21
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/mengcraft/script/ScriptBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static Object require(File required) {
case "js":
ScriptEngine ctx = jsEngine();
Bindings bindings = ctx.createBindings();
ctx.eval("exports = {}", bindings);
ctx.eval("exports = {}; load(\"nashorn:mozilla_compat.js\"); importPackage(java.lang, java.util, org.bukkit);", bindings);
ctx.eval(Files.newReader(required, StandardCharsets.UTF_8), bindings);
return ctx.eval("exports", bindings);
case "json":
Expand All @@ -84,7 +84,7 @@ public static ScriptEngine jsEngine() {
@Override
public void onLoad() {
plugin = this;
jsEngine = new ScriptEngineManager(getClassLoader()).getEngineByExtension("js");
jsEngine = new ScriptEngineManager(getClassLoader()).getEngineByName("nashorn");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public ScriptBinding load(ScriptInfo info) throws ScriptPluginException {
ctx.put("loader", info.loader);
Object scriptObj = null;
try {
ctx.eval("load(\"nashorn:mozilla_compat.js\"); importPackage(java.lang, java.util, org.bukkit);");
ctx.eval(info.contend);
scriptObj = ctx.eval("this");
} catch (ScriptException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ public void onEnable() {
Object global = ctx.eval("this", bindings);
Object jsObject = ctx.eval("Object", bindings);
((Invocable) ctx).invokeMethod(jsObject, "bindProperties", global, this);
ctx.eval("load(\"nashorn:mozilla_compat.js\"); importPackage(java.lang, java.util, org.bukkit);", bindings);
ctx.eval(new FileReader(jsFile), bindings);
}

Expand Down

0 comments on commit c791f21

Please sign in to comment.