diff --git a/minecraft/src/main/kotlin/org/polyfrost/oneconfig/internal/ui/sound/McUiSoundService.kt b/minecraft/src/main/kotlin/org/polyfrost/oneconfig/internal/ui/sound/McUiSoundService.kt index e0e1660f4..4d7502de9 100644 --- a/minecraft/src/main/kotlin/org/polyfrost/oneconfig/internal/ui/sound/McUiSoundService.kt +++ b/minecraft/src/main/kotlin/org/polyfrost/oneconfig/internal/ui/sound/McUiSoundService.kt @@ -95,7 +95,7 @@ class McUiSoundService : UiSoundService { override fun startAmbience(theme: UiSoundTheme, volume: Float) { try { val current = ambience - if (current != null && !current.isStopped && current.theme == theme && mc.soundManager.isActive(current)) { + if (current != null && !current.isStopped && current.theme == theme && isAlive(current)) { current.updateTargetVolume(ambienceVolumeFor(theme, volume)) current.cancelFadeOut() UiSoundDucking.setActive(true) @@ -197,7 +197,8 @@ class McUiSoundService : UiSoundService { try { val current = ambience ?: return@execute if (current.isStopped || current.isFadingOut || current !== ambience) return@execute - if (!mc.soundManager.isActive(current)) { + if (!isAlive(current)) { + mc.soundManager.stop(current) playAmbienceInstance(current.theme, current.targetVolume, fadeIn = !mc.isPaused) } else if (current.isStalledFadeIn) { mc.soundManager.stop(current) @@ -217,6 +218,18 @@ class McUiSoundService : UiSoundService { thread.start() } + // 1.8.9 starts streams on Paulscode's threads, so a fresh stream reports inactive until its preload ends. + // Stopping it then is lost: preload finishes afterwards and starts the channel untracked. + // A zero-volume play is skipped by the engine, so it never starts and gets no grace. + private fun isAlive(instance: FadingLoopSoundInstance): Boolean { + if (mc.soundManager.isActive(instance)) { + instance.seenActive = true + return true + } + return !instance.startedSilent && !instance.seenActive && + System.currentTimeMillis() - instance.startedAt < START_GRACE_MS + } + private val musicRefreshUntil = AtomicLong(0) @Volatile @@ -280,6 +293,13 @@ class McUiSoundService : UiSoundService { @Volatile private var fadeOutStep = 0f + val startedAt = System.currentTimeMillis() + + val startedSilent = targetVolume <= 0f + + @Volatile + var seenActive = false + val isFadingOut: Boolean get() = fadingOut @@ -364,5 +384,6 @@ class McUiSoundService : UiSoundService { const val FADE_IN_TICKS = 30f const val FADE_IN_FLOOR = 0.0001f const val STALL_NANOS = 400_000_000L + const val START_GRACE_MS = 5000L } }