Class DynamicRegistries

java.lang.Object
net.fabricmc.fabric.api.event.registry.DynamicRegistries

public final class DynamicRegistries extends Object
Contains methods for registering and accessing dynamic registries.

Basic usage

Custom dynamic registries can be registered with register(RegistryKey, Codec). These registries will not be synced to the client.

The list of all dynamic registries, whether from vanilla or mods, can be accessed using getDynamicRegistries().

Tags for the entries of a custom registry must be placed in /tags/<registry namespace>/<registry path>/. For example, the tags for the example registry below would be placed in /tags/my_mod/my_data/.

Synchronization

Dynamic registries are not synchronized to the client by default. To register a synced dynamic registry, you can replace the register(net.minecraft.registry.RegistryKey<? extends net.minecraft.registry.Registry<T>>, com.mojang.serialization.Codec<T>) call with a call to registerSynced(RegistryKey, Codec, SyncOption...).

If you want to use a different codec for syncing, e.g. to skip unnecessary data, you can use the overload with two codecs: registerSynced(RegistryKey, Codec, Codec, SyncOption...).

Synced dynamic registries can also be prevented from syncing if they have no entries. This is useful for compatibility with clients that might not have your dynamic registry. This behavior can be enabled by passing the DynamicRegistries.SyncOption.SKIP_WHEN_EMPTY flag to registerSynced.

Examples

public static final RegistryKey<Registry<MyData>> MY_DATA_KEY = RegistryKey.ofRegistry(Identifier.of("my_mod", "my_data"));

// Option 1: Register a non-synced registry
DynamicRegistries.register(MY_DATA_KEY, MyData.CODEC);

// Option 2a: Register a synced registry
DynamicRegistries.registerSynced(MY_DATA_KEY, MyData.CODEC);

// Option 2b: Register a synced registry with a different network codec
DynamicRegistries.registerSynced(MY_DATA_KEY, MyData.CODEC, MyData.NETWORK_CODEC);
  • Method Details

    • getDynamicRegistries

      public static @Unmodifiable List<RegistryLoader.Entry<?>> getDynamicRegistries()
      Returns an unmodifiable list of all dynamic registries, including modded ones.

      The list will not reflect any changes caused by later registrations.

      Returns:
      an unmodifiable list of all dynamic registries
    • register

      public static <T> void register(RegistryKey<? extends Registry<T>> key, com.mojang.serialization.Codec<T> codec)
      Registers a non-synced dynamic registry.

      The entries of the registry will be loaded from data packs at the file path data/<entry namespace>/<registry namespace>/<registry path>/<entry path>.json.

      Type Parameters:
      T - the entry type of the registry
      Parameters:
      key - the unique key of the registry
      codec - the codec used to load registry entries from data packs
    • registerSynced

      public static <T> void registerSynced(RegistryKey<? extends Registry<T>> key, com.mojang.serialization.Codec<T> codec, DynamicRegistries.SyncOption... options)
      Registers a synced dynamic registry.

      The entries of the registry will be loaded from data packs at the file path data/<entry namespace>/<registry namespace>/<registry path>/<entry path>.json.

      The registry will be synced from the server to players' clients using the same codec that is used to load the registry.

      If the object contained in the registry is complex and contains a lot of data that is not relevant on the client, another codec for networking can be specified with registerSynced(RegistryKey, Codec, Codec, SyncOption...).

      Type Parameters:
      T - the entry type of the registry
      Parameters:
      key - the unique key of the registry
      codec - the codec used to load registry entries from data packs and the network
      options - options to configure syncing
    • registerSynced

      public static <T> void registerSynced(RegistryKey<? extends Registry<T>> key, com.mojang.serialization.Codec<T> dataCodec, com.mojang.serialization.Codec<T> networkCodec, DynamicRegistries.SyncOption... options)
      Registers a synced dynamic registry.

      The entries of the registry will be loaded from data packs at the file path data/<entry namespace>/<registry namespace>/<registry path>/<entry path>.json

      The registry will be synced from the server to players' clients using the given network codec.

      Type Parameters:
      T - the entry type of the registry
      Parameters:
      key - the unique key of the registry
      dataCodec - the codec used to load registry entries from data packs
      networkCodec - the codec used to load registry entries from the network
      options - options to configure syncing