Skip to content

Custom sounds for mod developers

stashymane edited this page Oct 30, 2021 · 7 revisions

Defining sounds explicitly in sounds.json

The same steps apply for mods as for resource packs, except you have to place the sounds.json under your assets folder. Make sure to use the extrasounds folder as well, just like in ExtraSounds' resources folder.

Defining autogenerated sounds

Starting from 2.0.0, apart from the usual sounds.json, you are also able to autogenerate sounds for items that share the same class or certain properties, all up to your discretion. As an example, you can check the default generator for Vanilla blocks and items.

Prerequisites

ExtraSounds autogenerates the JSON file every single time resources are reloaded, and you need to make sure your autogenerator can be run at any point after item or block registration.
In addition to that, the autogenerator will only receive item identifiers that are of the same namespace, and only one generator can be registered for any given namespace. If you override the minecraft namespace, the default generator will not run anymore.

Creating a generator

There are two ways to create a generator - you can either create an entire class that extends SoundGenerator, or just create a static field of it. For fields, the SoundGenerator class has a helper function SoundGenerator.of(), which builds a generator for you. This is the recommended way to do so, especially if the class will be modified in the future, as backwards compatibility will be retained, at least to a certain extent.

public static SoundGenerator fieldName = SoundGenerator.of(namespace, id -> { /* convert */});

Sound generators essentially receive an item instance, which you have to convert to a SoundEntry. Creating it manually is pretty verbose, so for more simple sounds (single events or sounds that already have categories) there are some helper functions in the Sounds class provided by ES. You may also use the registered categories so you don't have to manually create identifiers for categories that are provided by default.

Registering your generator

To actually have your generator register, you have to add an extrasounds entrypoint in your fabric.mod.json, be it a class or a field. That will automatically be picked up and registered by the mod.

"extrasounds": [
    "com.package.mod.class::fieldName"
]

Of course if you extend the SoundGenerator class instead of creating a field you shouldn't include the ::fieldName part.
NOTE: IntelliJ will warn that it cannot resolve the field name if you have the Minecraft development extension, you may safely ignore this.

Clone this wiki locally