Playing sounds

Sound Assets

The files for your sounds can be in .ogg or .wav format and must be placed in the

“resources/assets/modid/sound/” directory.

Given this example, the directory to properly use the “test.mysound” SoundType would be:

“/assets/test/sound/mysound.ogg” within the resources directory.

Adding and registering your sounds

First, use Halplbe’s SoundHelper to add a sound to the game using the beforeGameStarts() method. Then, register a SoundType in with the SoundType.register(String) method to ensure proper compatibility with servers.

Import net.minecraft.core.sound.SoundTypes;
Import turniplabs.halplibe.helper.SoundHelper;
Import turniplabs.halplibe.util.GameStartEntrypoint;

public class GameMain implements GameStartEntrypoint {
	@Override
	public void beforeGameStarts() {
	      SoundTypes.register( "test.mysound" );
	      SoundHelper.addSound( "test", "mysound.ogg" );
      }
}

Playing Sounds

There are a two ways to play sounds.

First

world.playSoundEffect(...); // Plays sound at a given coordinate.
// Parameters: (Entity player, SoundCategory category, double x, double y, double z, String soundPath, float volume, float pitch)

Example

world.playSoundEffect(**null**, SoundCategory.WORLD_SOUND, 100, 100, 100, "test.mysound", 1.0f, 1.0f)
// Plays “test.mysound” at 100x 100y 100z at full volume and original pitch for all players.;

Second

world.playSoundAtEntity(...); // Plays sound at a given entity.

“namespace.sound_name”, where namespace is typically the mod ID.

Example

world.playSoundAtEntity(entityplayer, entityplayer, "test.mysound", 1.0f, 1.0f);
// Plays “test.mysound” at “entityplayer” at full volume and original pitch for all players except “entityplayer”.

Trick

Add a random double to the pitch argument of the playSound function to vary the pitch.

double randomPitch = Math.random();

Credits

Thanks to ‘deeter._’ for contributing to this book with their BTA sound documentation. Much appreciated!. Document