Grouped BGM Problem. Help in developing plugin.

Regarding this request post of mine. I actually developed my own plugin with this idea, yay~ lol.
A bunch of thanks for Caethyril for helping me.

It works alright 😤
It will play BGM of the referenced map, even if the current has no BGM. If the current map has BGM it will get played instead.

A bit of problem though, this plugin invalidates BGM change through auto run event (unless if "wait: ~10 frames" are added before changing). It can still do it with other trigger though.
It only invalidates the auto run event at the same time it detect a change in <bgm_reference: [MapID]>.
If the <bgm_reference: [MapID]> is the same as the map before, it will let you change BGM through auto run event like normal.

Do you guys have any idea how to solve it? I have tried what I know. 😢

Spoiler: The plugin code
JavaScript:
(function($) {

  var currentGroup;
  
  const BGM_Group_Autoplay = Game_Map.prototype.autoplay;
  Game_Map.prototype.autoplay = function() {
  BGM_Group_Autoplay.call(this);
    var BGMGroup = parseInt($dataMap.meta.bgm_reference);
    var mapRef = 'Map%1.json'.format(BGMGroup.padZero(3));
    loadReference = function(src) {
      var xhr = new XMLHttpRequest();
      var url = 'data/' + src;
      xhr.open('GET', url);
      xhr.overrideMimeType('application/json');
      xhr.onload = function() {
        if (xhr.status < 400) {
          dataMapRef = JSON.parse(xhr.responseText);
          DataManager.extractMetadata(dataMapRef);
          if (BGMGroup != currentGroup) {
            currentGroup = BGMGroup;
            AudioManager.playBgm(dataMapRef.bgm);  
          }
        }
      };
      xhr.onerror = ResourceHandler.createLoader(url, loadReference.bind($, src));
      dataMapRef = null;
      xhr.send();
    };
    if (BGMGroup > 0 && !$dataMap.autoplayBgm)  {
      if ($gamePlayer.isInVehicle()) {
          $gameSystem.saveWalkingBgm2();
      } else {
          loadReference(mapRef);
      }    
    }
  };

})(eqD.groupedBGM);

Original thread: https://forums.rpgmakerweb.com/threads/grouped-bgm-problem-help-in-developing-plugin.123386/