Ready to replace those zombie sounds with your own sounds? You've come to the right place.
Playing a pre-exisiting sound is fairly simple. Make sure you are on the logical server and call world.playSound
like so:
if (!world.isClient) { world.playSound( null, // Player - if non-null, will play sound for every nearby player *except* the specified player blockPos, // The position of where the sound will come from SoundEvents.BLOCK_ANVIL_LAND, // The sound that will play, in this case, the sound the anvil plays when it lands. SoundCategory.BLOCKS, // This determines which of the volume sliders affect this sound 1f, //Volume multiplier, 1 is normal, 0.5 is half volume, etc 1f // Pitch multiplier, 1 is normal, 0.5 is half pitch, etc ); }
You can do this when the block is right clicked for example, by overriding onUse
:
public class ExampleBlock extends Block { [...] @Override public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity placedBy, Hand hand, BlockHitResult blockHitResult) { if (!world.isClient) { world.playSound(null, blockPos, SoundEvents.BLOCK_ANVIL_LAND, SoundCategory.BLOCKS, 1f, 1f); } return false; } }
There's some extra steps involved in playing a sound that's not already in the game. We'll add a “punch” sound, that is available under CC0 here for the example.
To play a sound, you first need a sound file. Minecraft uses the .ogg
file format for those.
If your sound file is in a different format (like the one in the provided link), you can use an online converter to convert from your format to .ogg
.
Make sure your sound has only one channel, otherwise the attenuation effect (gradual loss of volume with distance) won't be applied to it.
Now put your .ogg file in the resources/assets/modid/sounds
folder. In the example case the file will be resources/assets/tutorial/sounds/my_sound.ogg
.
Under resources/assets/modid
add a new file named sounds.json
, if you do not have one yet.
Then add a new object entry with the name of your sound, and the sound's identifier in “sounds”
, for example:
{ "my_sound": { "sounds": [ "tutorial:my_sound" ] } }
You can also add a subtitle to your sound. The subtitle is a translation key, which should go in your language file.
{ "my_sound": { "subtitle": "subtitles.tutorial.my_sound", "sounds": [ "tutorial:my_sound" ] } }
See the Minecraft Wiki for more details about sounds.json
.
Simply create a new instance of SoundEvent
with the identifier modid:sound_name
, for example:
public class ExampleMod { [...] public static final Identifier MY_SOUND_ID = Identifier.of("tutorial:my_sound"); public static SoundEvent MY_SOUND_EVENT = SoundEvent.of(MY_SOUND_ID); }
If you're using version 1.19.2 or below, please replace SoundEvent.of
with new SoundEvent
, and
if you're using versions below 1.21, replace Identifier.of
with new Identifier
。
Register your sound event under the SOUND_EVENT
registry:
@Override public void onInitialize(){ [...] Registry.register(Registries.SOUND_EVENT, ExampleMod.MY_SOUND_ID, MY_SOUND_EVENT); }
If you're using version 1.19.2 or below, please replace Registries
with Registry
。
Use the sound event just like we explained at the start (onUse
is just an example, use it anywhere you have access to World
instance):
public class ExampleBlock extends Block { @Override public ActionResult onUse(BlockState blockState, World world, BlockPos blockPos, PlayerEntity placedBy, Hand hand, BlockHitResult blockHitResult) { if (!world.isClient) { world.playSound( null, // Player - if non-null, will play sound for every nearby player *except* the specified player blockPos, // The position of where the sound will come from ExampleMod.MY_SOUND_EVENT, // The sound that will play SoundCategory.BLOCKS, // This determines which of the volume sliders affect this sound 1f, //Volume multiplier, 1 is normal, 0.5 is half volume, etc 1f // Pitch multiplier, 1 is normal, 0.5 is half pitch, etc ); } return false; } }
You should now hear a punch sound when you right click your block!
Don't hear anything? Try: