Repository navigation
Dynamic Registries
Applies to all versions. On 1.21.1 read
ResourceLocationwhere this page saysIdentifier.
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.
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
iddecides where files are read from:data/<any namespace>/<id namespace>/<id path>/*.json. For the example above,data/<ns>/yourmod/things/my_thing.jsonbecomes the entry<ns>:my_thing. - Tags are read from
data/<any namespace>/tags/<id namespace>/<id path>/. - Two registries cannot share the same id.
| 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. |
| 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.
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.
DynamicRegistryProvider and DynamicTagProvider (in util.data) generate the JSON files and tags of a dynamic registry during datagen.
Common Toolkit is a port of Placebo by Shadows_of_Fire (MIT) and includes code derived from Minecraft Forge (LGPL-2.1); it is distributed under the LGPL-2.1-or-later license.