0

1

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

我使用调试,我很确定代码会执行到那里(GetBlock Function)

但是
在 Lua 脚本中向后调用原始 Java 似乎是不可能的,而是创建一个全新的 JVM?
否则为什么我称所有变量为空,即使它是静态的。
我错过了任何重要的代码吗?还是我的意图有问题?这是我在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;
    }
4

0 回答 0