Skip to content

Commit 93c942a

Browse files
committed
better registry controller
1 parent 54f32ad commit 93c942a

File tree

6 files changed

+80
-54
lines changed

6 files changed

+80
-54
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.lambda.core.registry
2+
3+
import com.lambda.util.Communication.warn
4+
import jdk.internal.org.jline.keymap.KeyMap.key
5+
import net.minecraft.registry.Registry
6+
import net.minecraft.registry.RegistryKey
7+
import net.minecraft.registry.entry.RegistryEntry
8+
import net.minecraft.util.Identifier
9+
10+
/**
11+
* Controller for handling temporary registry holders.
12+
* This is used to register entries into registries before they are actually loaded.
13+
* It allows for environment-agnostic registry handling.
14+
*/
15+
object AgnosticRegistries {
16+
private val registries = HashMap<
17+
RegistryKey<out Registry<*>?>,
18+
MutableList<RegistryHolder<*>>
19+
>()
20+
21+
/**
22+
* Registers a temporary registry holder.
23+
* This should be called after the controller has dumped its registries.
24+
* If not, the holder will not be dumped into its respective registry.
25+
*
26+
* @param registry The registry to register the holder for.
27+
* @param id The identifier for the registry entry.
28+
* @param value The value to register.
29+
*
30+
* @return The registry holder.
31+
*/
32+
fun <T> register(registry: Registry<T>, id: Identifier, value: T): RegistryHolder<T> {
33+
val holder = RegistryHolder(id, value)
34+
35+
registries.getOrPut(registry.key) { mutableListOf() }.add(holder)
36+
return holder
37+
}
38+
39+
/**
40+
* Loads the temporary registry holders into the actual registry.
41+
* This should be called after all registry holders have been created.
42+
*
43+
* @param registry The registry to dump into.
44+
*/
45+
fun dump(registry: Registry<*>?) = dump(registry, wrapper = null)
46+
47+
/**
48+
* Loads the temporary registry holders into the actual registry.
49+
* This should be called after all registry holders have been created.
50+
*
51+
* @param registry The registry to dump into.
52+
* @param wrapper The registry wrapper to use to determine how to register the entry.
53+
*/
54+
fun dump(registry: Registry<*>?, wrapper: RegistryWrapper<*>?) {
55+
val key = registry?.key ?: return warn("Tried to dump into a null registry.")
56+
57+
registries[key]?.forEach { it.handleRegister(wrapper ?: defaultWrapper(registry)) }
58+
registries.remove(key)
59+
}
60+
61+
/**
62+
* Default registry wrapper for vanilla registries.
63+
*/
64+
private fun defaultWrapper(registry: Registry<*>?): RegistryWrapper<*> {
65+
return object : RegistryWrapper<Any> {
66+
override fun <T> registerForHolder(id: Identifier?, value: T): RegistryEntry<T> {
67+
return Registry.registerReference(registry as Registry<T>, id, value)
68+
}
69+
}
70+
}
71+
}

common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.

common/src/main/kotlin/com/lambda/core/registry/RegistryHolder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ class RegistryHolder<T> internal constructor(
1515
with(holder) {
1616
checkNotNull(this) { "RegistryHolder not populated" }
1717

18-
return this.value()
18+
return value()
1919
}
2020
}
2121

2222
fun handleRegister(registry: RegistryWrapper<*>) {
23-
this.holder = registry.registerForHolder(id, value)
23+
holder = registry.registerForHolder(id, value)
2424
}
2525
}

common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.lambda.sound
22

33
import com.lambda.core.Loadable
4-
import com.lambda.core.registry.RegistryController
4+
import com.lambda.core.registry.AgnosticRegistries
55
import net.minecraft.registry.Registries
66

77
object SoundRegistry : Loadable {
88
override fun load(): String {
99
LambdaSound.entries.forEach {
10-
RegistryController.register(Registries.SOUND_EVENT, it.id, it.event)
10+
AgnosticRegistries.register(Registries.SOUND_EVENT, it.id, it.event)
1111
}
1212

1313
return "Loaded ${LambdaSound.entries.size} sounds"

fabric/src/main/kotlin/com/lambda/fabric/LambdaFabric.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,15 @@ import com.lambda.Lambda
44
import com.lambda.Lambda.LOG
55
import com.lambda.Lambda.MOD_NAME
66
import com.lambda.Lambda.VERSION
7-
import com.lambda.core.registry.RegistryController
8-
import com.lambda.core.registry.RegistryWrapper
7+
import com.lambda.core.registry.AgnosticRegistries
98
import net.fabricmc.api.ClientModInitializer
109
import net.minecraft.registry.Registries
11-
import net.minecraft.registry.Registry
12-
import net.minecraft.registry.entry.RegistryEntry
13-
import net.minecraft.util.Identifier
1410

1511
object LambdaFabric : ClientModInitializer {
1612
override fun onInitializeClient() {
1713
Lambda.initialize()
1814
LOG.info("$MOD_NAME Fabric $VERSION initialized.")
1915

20-
Registries.REGISTRIES.forEach { registry ->
21-
RegistryController.register(registry.key, object : RegistryWrapper<Any> {
22-
override fun <T> registerForHolder(id: Identifier?, value: T): RegistryEntry<T> {
23-
return Registry.registerReference(registry as Registry<T>, id, value)
24-
}
25-
})
26-
}
16+
Registries.REGISTRIES.forEach(AgnosticRegistries::dump)
2717
}
2818
}

forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ import com.lambda.Lambda
44
import com.lambda.Lambda.LOG
55
import com.lambda.Lambda.MOD_NAME
66
import com.lambda.Lambda.VERSION
7-
import com.lambda.core.registry.RegistryController
8-
import com.lambda.core.registry.RegistryWrapper
7+
import com.lambda.core.registry.AgnosticRegistries
98
import com.lambda.graphics.RenderMain
10-
import net.minecraft.registry.Registry
11-
import net.minecraft.registry.entry.RegistryEntry
12-
import net.minecraft.util.Identifier
139
import net.minecraftforge.api.distmarker.Dist
1410
import net.minecraftforge.api.distmarker.OnlyIn
1511
import net.minecraftforge.client.event.RenderGuiEvent
@@ -30,14 +26,9 @@ object LambdaForge {
3026
LOG.info("$MOD_NAME Forge $VERSION initialized.")
3127
}
3228

29+
// Forge forces the user to user their event in order to interact with registries.
3330
@SubscribeEvent
34-
fun onRegistrySetup(event: RegisterEvent) {
35-
RegistryController.register(event.registryKey, object : RegistryWrapper<Any> {
36-
override fun <T> registerForHolder(id: Identifier?, value: T): RegistryEntry<T> {
37-
return Registry.registerReference(event.getVanillaRegistry(), id, value)
38-
}
39-
})
40-
}
31+
fun onRegistrySetup(event: RegisterEvent) = AgnosticRegistries.dump(event.getVanillaRegistry<Any>())
4132

4233
// Most events here are hooked due to forge not caring about others
4334
// and directly patching the minecraft classes.

0 commit comments

Comments
 (0)