(fabricmc)有人知道我如何能够在玩家在客户端模组中重生后运行一些代码吗?

问题描述 投票:0回答:1

我有一个客户端结构模型。它使用 SimpleConfig 来存储其配置和内置客户端夜视仪的状态。我想在玩家重生后立即重新启用客户端夜视,如下所示:

if (ModConfigs.NV_STATUS) {
    assert MinecraftClient.getInstance().player != null;
    MinecraftClient.getInstance().player.addStatusEffect(new StatusEffectInstance(StatusEffects.NIGHT_VISION, -1));
}

有人知道我怎样才能实现这个目标吗?

我已经尝试使用 mixins 将此代码注入到类

requestRespawn
ClientPlayerEntity
方法中,但没有成功。我也没有找到任何客户端事件侦听器。

minecraft minecraft-fabric
1个回答
0
投票

对于未来关注这个问题的任何人,我在几天后解决了这个问题(我知道,我只是在 1 天前创建了这个问题,但那时我已经花了 4 天的时间试图解决这个问题)。解决办法是:

ServerPlayerEvents.AFTER_RESPAWN.register((arg1, arg2, arg3) -> {
    try {
        // This is VERY important, as it keeps for example effects from resetting which happened to me sometimes during testing
        Thread.sleep(100);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
    }

    // Your custom logic here
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.