Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.8.9] Playing sounds loopable with stop condition


Laseph
 Share

Recommended Posts

Hello, I'm having difficulty finding good and consistent information on how to add sounds to my mod. I would like to have a loopable sound that stops when a smelter is done smelting. But i don't really know what the correct way of doing it is. I know you can play sounds by accessing the sound handler but i don't how to do the smelting sound with the stop condition. I would also want to know how to handle sounds correctly in general.

Link to comment
Share on other sites

There are several strategies. The easiest is for your smelter to "play" an occasional sound effect after deciding (!remote) that it is smelting. This works if the sound is subtle enough to overlap or have gaps.

 

What's actually happening is that the server-side playSoundEffect method sends a packet to all world accesses (clients within earshot), and each client reacts to its network packet by actually rendering the effect through a predefined "sound" (renderer). This predefined renderer has predefined parameters. Among them is no repeat.

 

Yes, the naming of sound methods and classes adds confusion to the already mind-bending concept of client-server interoperation. PlaySoundEffect is a server-side decision. PlaySound is a client side rendering method. Do not confuse them. Moreover, in articles about sound, the word "sound" can refer to either a sound renderer or a sound resource, depending on context, and some writers confuse the two just to make modders' lives more interesting.

 

If you really really want a repeating (continuous) sound that you can stop on command, then you must do two things: Instantiate your own client-side renderer, and handle an event to substitute yours for the predefined one when a client is about to play it.

 

For the renderer, extend PositionedSound and probably implement ITickableSound. For the event, subscribe to PlaySoundEvent (@SideOnly(Side.CLIENT)). Mine looks like this:

 

@SideOnly(Side.CLIENT)
public class InventionsClientEventSubs {

  public InventionsClientEventSubs() {
  }

  @SubscribeEvent
  public void onPlaySound(PlaySoundEvent e) {
    ResourceLocation r = e.sound.getSoundLocation ();

    if (r.equals (BlockBlower.WHIRRLOC)) {
      WorldClient wc = Minecraft.getMinecraft ().theWorld;
      PositionedSound ps = (PositionedSound) e.sound;
      e.result = new ContinuousPositionedSoundFan (wc, ps); // Substitute with my continuous sound
    }
  }
}

 

And because it's client-only, I register the event handler to its bus in my client proxy. Note: ContinuousPositionedSoundFan is my own extension of PositionedSound, so I have written a constructor to copy what it needs from another ps. YMMV.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

Link to comment
Share on other sites

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

 Share



×
×
  • Create New...

Important Information

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