From e83b88d3207be98e67fe50c69a3d552fe841dde5 Mon Sep 17 00:00:00 2001
From: Lasercar <64717068+Lasercar@users.noreply.github.com>
Date: Sun, 16 Mar 2025 16:38:46 +1000
Subject: [PATCH] I'm so done with this stupid bug Eric wuz here!

---
 source/funkin/play/PlayState.hx | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/source/funkin/play/PlayState.hx b/source/funkin/play/PlayState.hx
index 3b221fdd5..d32dc8402 100644
--- a/source/funkin/play/PlayState.hx
+++ b/source/funkin/play/PlayState.hx
@@ -918,8 +918,12 @@ class PlayState extends MusicBeatSubState
 
       Conductor.instance.update(Conductor.instance.songPosition + elapsed * 1000, false); // Normal conductor update.
 
-      // Fallback in case music's onComplete function doesn't get called.
-      if (FlxG.sound.music.time >= (FlxG.sound.music.endTime ?? FlxG.sound.music.length) && mayPauseGame) endSong(skipEndingTransition);
+      // If, after updating the conductor, the instrumental has finished, end the song immediately.
+      // This helps prevent a major bug where the level suddenly loops back to the start or middle.
+      if (Conductor.instance.songPosition >= (FlxG.sound.music.endTime ?? FlxG.sound.music.length))
+      {
+        if (mayPauseGame) endSong(skipEndingTransition);
+      }
     }
 
     var androidPause:Bool = false;
@@ -2062,7 +2066,7 @@ class PlayState extends MusicBeatSubState
     }
 
     FlxG.sound.music.onComplete = function() {
-      endSong(skipEndingTransition);
+      if (mayPauseGame) endSong(skipEndingTransition);
     };
     // A negative instrumental offset means the song skips the first few milliseconds of the track.
     // This just gets added into the startTimestamp behavior so we don't need to do anything extra.