Skip to content

Dynamic Registries

LlaGuiTo edited this page Sep 30, 2026 · 1 revision

Dynamic registries

Applies to all versions. On 1.21.1 read ResourceLocation where this page says Identifier.

A dynamic registry (com.skd.commontoolkit.dynreg.DynamicRegistry<R>) is a reload listener that behaves like a registry: it loads JSON files from datapacks, can be reloaded with /reload (unlike vanilla datapack registries), supports tags, and can sync its content to clients.

Creating one

public class ThingRegistry extends DynamicRegistry<Thing> {
    public static final ThingRegistry INSTANCE = new ThingRegistry();

    private ThingRegistry() {
        super(LOGGER, Identifier.fromNamespaceAndPath("yourmod", "things"), RegistrySerializer.simple(Thing.CODEC));
    }
}

// during mod construction / setup:
ThingRegistry.INSTANCE.registerToBus();
  • The id decides where files are read from: data/<any namespace>/<id namespace>/<id path>/*.json. For the example above, data/<ns>/yourmod/things/my_thing.json becomes the entry <ns>:my_thing.
  • Tags are read from data/<any namespace>/tags/<id namespace>/<id path>/.
  • Two registries cannot share the same id.

Serializers

Serializer Use
RegistrySerializer.simple(codec) Server-only content (not sent to clients).
RegistrySerializer.synced(codec) Content that clients also need; it is sent over the network after each reload and on login.
SubtypedSerializer Several entry types in one registry, selected by a top-level "type" key.

Reading entries

Method Description
getKeys(), getValues() All ids / all entries.
getValue(id), getOrDefault(id, def), getKey(value) Lookups.
holder(id) / holder(value) A DynamicHolder<R>: like a vanilla Holder, it stays valid across reloads and resolves to the current value.
holderCodec() / holderStreamCodec() Codecs to reference entries by id from other data or packets.
getTag(DynamicTagKey) / getOrCreateTag(...) Tag contents as a DynamicHolderSet.
addCallback(RegistryCallback) React to the reload stages (begin / end of reload).

DynamicHolderSet.codec(registry) accepts either a list of ids or a #tag in your own data files, like vanilla HolderSet.

Weighted registries

WeightedDynamicRegistry<V> adds random selection for entries that implement ILuckyWeighted (getWeight(), getQuality()):

  • getRandomItem(random), getRandomItem(random, luck, filters...)
  • getRandomFromTag(tagKey, random, luck), getRandomFromSet(holderSet, random, luck)

The effective weight of an entry is weight + luck × quality (never below 0), so luck makes high-quality entries more likely.

Datagen

DynamicRegistryProvider and DynamicTagProvider (in util.data) generate the JSON files and tags of a dynamic registry during datagen.

Clone this wiki locally