Luaj使用java运行lua脚本,并且调用Java对象时返回NULL

1

Player playerNow将返回null,就像这段代码运行时任何变量一样。

我使用调试,我很确定代码将执行到那里(GetBlock函数)

但是

调用lua script中的原始Java似乎是不可能的,而是创建一个全新的JVM吗?

否则,为什么我调用所有变量都是null,即使它是静态的。

我是否错过了任何重要的代码?还是我有问题? 这是我在stackoverflow上的第一个问题,如果您需要任何信息,请告诉我。

Java运行lua脚本

 public void LuaTest(String UUID,int x,int y,int z)
    {
        String Path = "C:\\ForgeProject\\1.18.1\\Hello\\src\\main\\java\\com\\xtx\\hello\\res\\lua\\test.lua";
        Globals globals = JsePlatform.standardGlobals();
        globals.loadfile(Path).call();

        LuaTable table= LuaTable.tableOf();
        table.set("x",x);
        table.set("y",y);
        table.set("z",z);
        table.set("uuid",UUID);
        LuaValue func1 = globals.get(LuaValue.valueOf("main"));
        String data = func1.call(table).toString();
        System.out.println(data);
    }

lua脚本

function main(pos)
    print(pos["x"])
    local LuaFunction = luajava.bindClass("com.xtx.hello.LuaFunction")

    local tmp=LuaFunction:GetBlock(pos["x"],pos["y"],pos["z"])

    return 'haha'
end

回调函数在java中返回lua

    public static LuaTable GetBlock(int x, int y, int z) {
        Player playerNow = ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayer(Player.createPlayerUUID("Dev"));
        Level level = playerNow.getLevel();
        BlockState tmp = level.getBlockState(new BlockPos(x, y, z));
        LuaTable table = LuaTable.tableOf();
        table.set("DescriptionId", tmp.getBlock().getDescriptionId());
        return table;
    }

原文链接 https://stackoverflow.com/questions/71265534

点赞