minecraftminecraft-fabric

(fabricmc) Does someone know how I'm able to run some code after the player has respawned in a client side mod?


I have a client side fabric mod. It uses SimpleConfig to store it's configuration and the state of the inbuilt client-side night vision. I want to re-enable the client side night vision as soon as the player has respawned, like this:

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

Does anybody know how I can achieve this?

I've already tried to use mixins to inject this code into the requestRespawn method of the class ClientPlayerEntity, which didn't work. I also didn't find any client-side event listeners for it.


Solution

  • For anybody looking at this in the future, I solved it after days (I know, I created the question only 1 day ago but at that point I'd already been trying to figure it out for a good 4 days) of work. The solution is:

    @Enviroment(EnvType.CLIENT)
    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
        }
    });