Jump to content

[1.19.2] Overriding/setting ambient music in a specific dimension?


Recommended Posts

Posted (edited)

As the title suggests, I am trying to set (in this case override) the ambient music in the Nether with a custom song but cannot get looping to work. I have registered the .ogg file, set up correct configurations for a streamed sound in sounds.json and ensured everything is hooked up within the main mod file for the custom SoundEvent to be recognized. Which of event.setSound and play.playSound is needed? Is there a simple flag to do this or is there a more appropriate method/approach?

@Mod.EventBusSubscriber(modid = FirstMod.MOD_ID)
public static class ForgeEvents {
    @SubscribeEvent
    public static void onPlaySound(PlaySoundEvent event) {
        if (Minecraft.getInstance().player != null) {
            if (Minecraft.getInstance().level != null) {
                if (Minecraft.getInstance().level.dimension() == Level.NETHER) {
                    LocalPlayer player = Minecraft.getInstance().player;
                    if (!isCustomSongPlaying) {
                        isCustomSongPlaying = true;
                        event.setSound(SimpleSoundInstance.forLocalAmbience(ModSounds.CUSTOM_SONG.get(), 1.0f, 1.0f));
                        player.playSound(ModSounds.CUSTOM_SONG.get(), 1.0f, 1.0f);
                    }
                }
                else {
                    isCustomSongPlaying = false;
                }
            }
        }
    }
}

 

Edited by Gedeon
Sound working but not looping like with vanilla music.
  • Gedeon changed the title to [1.19.2] Overriding/setting ambient music in a specific dimension?
Posted

Unfortunately, there's no easy method to do this. However, your logic would indicate that it's impossible for the sound to loop as it assumes the custom sound is play for as long as the player is in the nether. You are better to check whether the SoundManager finds the current sound instance active (via SoundManager#isActive). You will need to hold a current instance of the SoundInstance being played.

Posted
9 hours ago, ChampionAsh5357 said:

Unfortunately, there's no easy method to do this. However, your logic would indicate that it's impossible for the sound to loop as it assumes the custom sound is play for as long as the player is in the nether. You are better to check whether the SoundManager finds the current sound instance active (via SoundManager#isActive). You will need to hold a current instance of the SoundInstance being played.

Thank you for the feedback. Is another event subscription needed (onTick or other) to periodically call the isActive method? Could you please elaborate some more on your recommendation for a current instance of SoundInstance? Does this tie into the current subscription I have to PlaySoundEvent or something else?

Posted
14 minutes ago, Gedeon said:

Is another event subscription needed (onTick or other) to periodically call the isActive method?

Yep, probably ClientTickEvent

14 minutes ago, Gedeon said:

Could you please elaborate some more on your recommendation for a current instance of SoundInstance? Does this tie into the current subscription I have to PlaySoundEvent or something else?

See how MusicManager works since this is essentially what you are trying to design.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.