if (entity instanceof net.minecraft.world.entity.player.Player player) {
    net.mcreator.[yourmodname].network.YourModVariables.PlayerVariables vars = player.getCapability(net.mcreator.[yourmodname].network.YourModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new net.mcreator.[yourmodname].network.YourModVariables.PlayerVariables());

    float currentBodyYaw = (float) [global_bodyYaw];
    if (currentBodyYaw == 0.0f && player.getYRot() != 0.0f) {
        currentBodyYaw = player.getYRot();
    }

    float headYaw = player.getYRot();

    double dx = player.getX() - [global_lastX];
    double dz = player.getZ() - [global_lastZ];

    int moveGrace = player.getPersistentData().getInt("move_grace");

    if (dx * dx + dz * dz > 0.001) {
        moveGrace = 5;
        float moveYaw = (float)(Math.atan2(-dx, dz) * (180.0 / Math.PI));
        float relativeYaw = net.minecraft.util.Mth.wrapDegrees(moveYaw - headYaw);
        player.getPersistentData().putFloat("last_rel_yaw", relativeYaw);
        
        [global_lastX] = player.getX();
        [global_lastZ] = player.getZ();
    } else if (moveGrace > 0) {
        moveGrace--;
    }

    player.getPersistentData().putInt("move_grace", moveGrace);
    boolean isMoving = moveGrace > 0;

    float idealBodyYaw = headYaw;

    if (isMoving) {
        float relYaw = player.getPersistentData().getFloat("last_rel_yaw");
        
        if (Math.abs(relYaw) < 135.0f) {
            if (relYaw > 50.0f) relYaw = 50.0f;
            if (relYaw < -50.0f) relYaw = -50.0f;
            idealBodyYaw = headYaw + relYaw;
        }
    }

    float diffIdeal = net.minecraft.util.Mth.wrapDegrees(idealBodyYaw - currentBodyYaw);
    boolean isInteracting = player.swinging || player.isUsingItem();

    if (isInteracting) {
        currentBodyYaw += diffIdeal * 0.4f;
    } else if (isMoving) {
        currentBodyYaw += diffIdeal * 0.4f;
    }

    float diffHeadBody = net.minecraft.util.Mth.wrapDegrees(headYaw - currentBodyYaw);

    if (diffHeadBody > 50.0f) {
        currentBodyYaw = headYaw - 50.0f;
    } else if (diffHeadBody < -50.0f) {
        currentBodyYaw = headYaw + 50.0f;
    }

    [global_bodyYaw] = (double) net.minecraft.util.Mth.wrapDegrees(currentBodyYaw);
    vars.syncPlayerVariables(player);
}
