LiteLDev / LegacyScriptEngine

A plugin engine for running LLSE plugins on LeviLamina
GNU General Public License v3.0
48 stars 8 forks source link

[Bug]: nodejs引擎async延时执行getPlayerNbt() api报错 #68

Closed Hurry1027 closed 6 months ago

Hurry1027 commented 8 months ago

Describe the bug

nodejs引擎中使用async异步,延时执行mc.getPlayerNbt()会报错。

另外,在有 GMLIB_MC = require("../GMLIB-LegacyRemoteCallApi/lib/GMLIB_API-JS").Minecraft 的情况下, ll reload test会刷屏报错。

To Reproduce

let GMLIB_MC;
mc.listen("onServerStarted", ()=>{
    GMLIB_MC = require("../GMLIB-LegacyRemoteCallApi/lib/GMLIB_API-JS").Minecraft;
});
mc.listen("onChat", (pl, msg)=>{
    if (msg == 't'){
        getNbt('ll3');
    }
    if (msg == 'tg'){
        getNbt('glib');
    }
}); 

async function getNbt(mode){
    await sleep(1000); // 等待1秒:有这个就会报错,没有不报错。
    if (mode == 'll3'){
        log(`获取离线玩家NBT LL3 api`);
        log(mc.getPlayerNbt("04996ee2-5864-36c4-a7b6-d1b0dfa030de")?.toSNBT()?.length);
    }else{
        log(`获取离线玩家NBT Glib api`);
        log(GMLIB_MC.getPlayerNbt("04996ee2-5864-36c4-a7b6-d1b0dfa030de")?.toSNBT()?.length);
    }
}

async function sleep(time) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, time);
    });
}

报错:

19:10:11.046 INFO [Server] Player connected: Wn1027, xuid: 2535427849428692
19:10:12.680 INFO [Server] Player Spawned: Wn1027 xuid: 2535427849428692, pfid: 118bd779cc337325
19:10:36.478 INFO [nodejstest] 获取离线玩家NBT LL3 api

19:10:36.480 ERROR [legacy-script-engine-nodejs] Fail in getPlayerNbt!
19:10:36.481 ERROR [legacy-script-engine-nodejs] Seh Exception, from <LeviLamina>:
19:10:36.482 ERROR [legacy-script-engine-nodejs] [0xC0000005:ntstatus] 0x%p 指令引用了 0x%p 内存。该内 存不能为 %s。
19:10:36.482 ERROR [legacy-script-engine-nodejs] Parameter 0: 0x1
19:10:36.482 ERROR [legacy-script-engine-nodejs] Parameter 1: 0x0
19:10:36.483 ERROR [legacy-script-engine-nodejs]
19:10:36.483 ERROR [legacy-script-engine-nodejs] In API: McClass::getPlayerNbt
19:10:36.483 ERROR [legacy-script-engine-nodejs] In Plugin: nodejstest
19:10:36.483 INFO [nodejstest] <Null>

19:10:56.531 INFO [nodejstest] 获取离线玩家NBT Glib api

19:10:56.538 ERROR [LeviLamina] Error in schedule thread [6680]:
19:10:56.538 ERROR [LeviLamina] Seh Exception, from <LeviLamina>:
19:10:56.538 ERROR [LeviLamina] [0xC0000005:ntstatus] 0x%p 指令引用了 0x%p 内存。该内存不能为 %s。
19:10:56.538 ERROR [LeviLamina] Parameter 0: 0x1
19:10:56.539 ERROR [LeviLamina] Parameter 1: 0x0

Expected behavior

修复此问题。

Screenshots

No response

Platform

Windows11

BDS Version

1.20.62

LeviLamina Version

0.9.2

LegacyScriptEngine Version

0.4.6 and 0.4.8

Additional context

No response

ShrBox commented 8 months ago

请不要在非MC_SERVER线程执行MCAPI

Hurry1027 commented 8 months ago

请不要在非MC_SERVER线程执行MCAPI

这种写法在LL2中是可用的。 我所了解的nodejs中的async应该是单线程。 而且在我的quickjs插件CustomGetMap中我也用到这种写法,用于延迟获取地图画,可以正常运行。 如果不能这样写,我只能使用回调函数?那在nodejs插件中就几乎无法使用async await语句了。