Rhino - 使用自定义类参数调用javascript函数时发生错误

时间:2016-12-12 10:01:43

标签: javascript java rhino

我使用Java制作了一个Bukkit插件。 而且,我的插件基于Mozilla的Rhino。 我把参数放在函数回调“onPlayerJoin”中的“_Player”中。 我执行了它,但它无法正常工作;(

您对此错误有任何疑问吗?还是要修改的东西?

错误的简短信息就在这里。

TypeError: Cannot find default value for object. (test.js#1)

码(爪哇):

public static void callMethod(String functionName, Object... args) {
        for(String filename : Main.scripts) {
            Context context = Context.enter();
            Scriptable scope = context.initStandardObjects();

            try {
                ScriptableObject.defineClass(scope, _Bukkit.class);
                ScriptableObject.defineClass(scope, _Player.class, false, true);
                ScriptableObject.putProperty(scope, "ChatColor", constantsToObj(_ChatColor.class));
                context.evaluateReader(scope, new FileReader(FileSystem.LOC_SCRIPT+filename), filename, 0, null);
                Object object = scope.get(functionName, scope);

                if(object != null && object instanceof Function) {
                    Function function = (Function) object;
                    function.call(context, scope, scope, args);
                }
            } catch(RhinoException e) {
                Debug.danger(e.getMessage()+" ("+e.lineNumber()+", "+e.columnNumber()+")");
            } catch(IOException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
                Debug.danger("An error occured while compiling code.");
            } finally {
                Context.exit();
            }
        }
    }

@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
    callMethod("onPlayerJoin", new _Player(e.getPlayer()));
}

Code-1(Javascript,Not Working):

function onPlayerJoin(player) {
    player.sendMessage("hello!");
}

Code-2(Javascript,Working):

function onPlayerJoin(player) {
    new Player("playername").sendMessage("hello!");
}

0 个答案:

没有答案