Skip to content

Registry

Ashley Wright edited this page Jun 1, 2021 · 8 revisions

WitcherySimpleRegistry<K, V>

This is at the core of a lot of Witchery content. It provides a basic way to register and interact with content

Types

You have the basic type, WitcherySimpleRegistry<K, V>, the constructor of it takes the initial size, which defaults to 16. This doesn't limit the size of the registry but simply gives the expected base size. There is also WitcheryIdentityRegistry<K, V>, which extends the other type; it provides a basic integer ID interface that can be used for networking, NOT for data storing.

WitcherySimpleRegistry overview

Implements Iterable<Map.Entry<K, V>>

Methods:

operator boolean contains(key: K?) Checks if the following key has been added to the registry

operator V? get(key: K?) Gets the value associated with the key. null if key is null or is not added.

K? getKey(value: V?) Gets the key for the value if registered. null if value is null or is not added.

K? getRandom(random: java.lang.Random) A random key from the registry. null if registry has no elements.

operator void set(key: K, value: V) Adds a new element to the registry. Throws IllegalArgumentException if key already exists

WitcheryIdentityRegistry overview

Extends WitcheryRegistry<K, V>, meaning all of what applies to that would apply to this, plus the additional:

int getIdByKey(key: K?) Gets the current ID for provided key, or -1 if key is null or is not added.

int getId(value: V?) Gets the current ID for provided value, or -1 if key is null or is not added.

K? getKey(id: int) Opposite of getIdByKey(key: K?), functions the same way you'd expect. -1 or any ID that doesn't exist returns null.

V? getValue(id: int) Opposite of getId(key: V?), functions the same way you'd expect. -1 or any ID that doesn't exist returns null.

Examples

Kotlin

val demon = DemonType()
println(demon) //DemonType@5FE490

//Normal registry
val registry = WitcherySimpleRegistry<ResourceLocation, DemonType>(4)

registry[ResourceLocation("demonmod", "demon1")] = demon

println(registry.getKey(demon)) //demonmod:demon1
println(registry[ResourceLocation("demonmod", "demon1")]) //DemonType@5FE490

//ID registry
val idRegistry = WitcheryIdentityRegistry<ResourceLocation, DemonType>(4)

idRegistry[ResourceLocation("demonmod", "demon1")] = demon

println(idRegistry.getId(demon)) //0
println(idRegistry.getValue(0)) //DemonType@5FE490

Java

DemonType demon = new DemonType();
System.out.println(demon); //DemonType@5FE490

//Normal registry
WitcherySimpleRegistry<ResourceLocation, DemonType> registry = new WitcherySimpleRegistry<>(4);

registry.set(new ResourceLocation("demonmod", "demon1"), demon);

System.out.println(registry.getKey(demon)); //demonmod:demon1
System.out.println(registry.get(new ResourceLocation("demonmod", "demon1"))); //DemonType@5FE490

//ID registry
WitcheryIdentityRegistry<ResourceLocation, DemonType> idRegistry = new WitcheryIdentityRegistry<>(4);

idRegistry.set(new ResourceLocation("demonmod", "demon1"), demon);

System.out.println(idRegistry.getId(demon)); //0
System.out.println(idRegistry.getValue(0)); //DemonType@5FE490

Scala

val demon = new DemonType()
println(demon) //DemonType@5FE490

//Normal registry
val registry = new WitcherySimpleRegistry[ResourceLocation, DemonType](4)

registry.set(new ResourceLocation("demonmod", "demon1"), demon)

println(registry.getKey(demon)) //demonmod:demon1
println(registry.get(new ResourceLocation("demonmod", "demon1"))) //DemonType@5FE490

//ID registry
val idRegistry = new WitcheryIdentityRegistry[ResourceLocation, DemonType](4)

idRegistry.set(new ResourceLocation("demonmod", "demon1"), demon)

println(idRegistry.getId(demon)) //0
println(idRegistry.getValue(0)) //DemonType@5FE490

Deferred Registering

To allow for registry replacement and disabling, deferred registering rather than accessing registries directly is preferred.

This is achieved using the WitcheryContentRegistrar<K, T> class. Which can be used like such;
Kotlin

val registry = WitcherySimpleRegistry<String, DemonType>(1)
val registrar = WitcheryContentRegistrar(registry.wrap()) //You need a RegistryWrapper, you can access an existing one using RegistryWrappers or the `wrap` extension function on a WitcherySimpleRegistry.
val type by registrar.add("demon", ::DemonType) //You pass a key and a supplier to the value, we use the `by` keyword to use the returned value as a delegate
registrar.initialize() //Allow the values to be registered in the future
type?.apply {} //The property can now be used as a normal value, except that it'll return null if the backing value is not registered.

Java

WitcherySimpleRegistry<String, DemonType> registry = new WitcherySimpleRegistry<>(1);
WitcheryContentRegistrar<String, DemonType> registrar = new WitcheryContentRegistrar<>(RegistryWrappers.wrap(registry)); //You need a RegistryWrapper, you can access an existing one using RegistryWrappers or the `RegistryWrappers#wrap` method.
WitcheryRegistryObject<String, DemonType> type = registrar.add("demon", DemonType::new); //You pass a key and a supplier to the value, then the returned value can be used to access the object
registrar.initialize(); //Allow the values to be registered in the future
type.get(); //Nullable if not registered
type.getUnchecked(); //Crashes if not registered

(The Scala example has been ommited in this case as it can be inferred.)

The WitcheryContentRegistrar constructor also allows passing a pair of a config name and a key to name convertor, this will be used for the config if passed.