Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi, Im adding background music but Ive ran into a problem making sure no other music is playing before I play mine.

 

Heres what I got

 

@SubscribeEvent
public void playMyMusic(ClientTickEvent event)
{
	Random rand = new Random();
	ISound soundCache = null;
	SoundHandler handler = Minecraft.getMinecraft().getSoundHandler();
	int i = rand.nextInt(1000);
	if(soundCache == null || !handler.isSoundPlaying(soundCache))
	{
		if(i == 0)
		{
			System.out.println("playing sound");
			soundCache = new PositionedSoundRecord(new ResourceLocation(Arcticraft.MOD_ID + ":records.frozen_feelings"), 1.0F, 1.0F, 1.0F, 1.0F, 1.0F);
			handler.playSound(soundCache);
		}
	}
}

 

My sound plays but it will play over the top of any currently playing which I dont want. Obviously, its playing because soundCache does = null but thats because I have no idea how ISound works and didnt get any google results.

Okay, there is the Minecraft.getMinecraft().getSoundHandler().isSoundPlaying(ISound), stopSounds() and stopSound(ISound) methods available.

 

The stopSounds() will stop all sounds.

 

To stop a specific sound, you'd need to have the instance of the ISound to pass to the stopSound() method, which you probably won't have if it is a vanilla sound. But for your own sounds you can stop them this way.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

So it's just not possible to add your own music properly, you can't check if vanilla songs are playing, so you always run the risk of playing your music over the vanilla tracks.

So it's just not possible to add your own music properly, you can't check if vanilla songs are playing, so you always run the risk of playing your music over the vanilla tracks.

 

Unless you turn off all sounds yes.

 

However, you might be able to use reflection. The sound handler has a private field for the sound manager which has a private field for playingSounds map from which you could iterate through and check the ISound returned to find the private resource location.

 

I'm not that strong in reflection but I think that is possible.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Ok, this post was getting messy, there's no reason to show the totally incorrect things I tried, so here's what works:

 

 

SoundManager mng = ReflectionHelper.getPrivateValue(SoundHandler.class, Minecraft.getMinecraft().getSoundHandler(), "sndManager");
Map playingSounds = ReflectionHelper.getPrivateValue(SoundManager.class, mng, "playingSounds");
Iterator it = playingSounds.keySet().iterator();
while(it.hasNext())
{
   PositionedSound ps = (PositionedSound)playingSounds.get(it.next());
   ResourceLocation reloc = ReflectionHelper.getPrivateValue(PositionedSound.class, ps, "field_147664_a");
   if("music.game".equals(reloc.getResourcePath()))
   {
      Minecraft.getMinecraft().getSoundHandler().stopSound(ps);
      System.out.println("stopped music");
      break;
   }
}

Ok, this post was getting messy, there's no reason to show the totally incorrect things I tried, so here's what works:

 

 

SoundManager mng = ReflectionHelper.getPrivateValue(SoundHandler.class, Minecraft.getMinecraft().getSoundHandler(), "sndManager");
Map playingSounds = ReflectionHelper.getPrivateValue(SoundManager.class, mng, "playingSounds");
Iterator it = playingSounds.keySet().iterator();
while(it.hasNext())
{
   PositionedSound ps = (PositionedSound)playingSounds.get(it.next());
   ResourceLocation reloc = ReflectionHelper.getPrivateValue(PositionedSound.class, ps, "field_147664_a");
   if("music.game".equals(reloc.getResourcePath()))
   {
      Minecraft.getMinecraft().getSoundHandler().stopSound(ps);
      System.out.println("stopped music");
      break;
   }
}

This will crash in obf environment. When passing field name to reflection helper, pass both obfuscated and not obf names, reflection helper will not convert them into obfuscated fields...

So, how do you get the obfuscated names?

Or you can use ObfuscationReflectionHelper, that will convert them automatically, or you go to user\.gradle\caches\minecraft\net\minecraftforge\forge\[forge version]\unpacked\conf and search in files fields.csv and methods.csv. It looks like obfName,deobfname,desccription. So you can find by one name another...

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.