1515 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1616 */
1717
18- package com.lambda.core.registry
18+ package com.lambda.core
1919
2020import net.minecraft.registry.Registry
2121import net.minecraft.registry.RegistryKey
@@ -29,36 +29,22 @@ import net.minecraft.util.Identifier
2929 * It allows for environment-agnostic registry handling.
3030 */
3131object AgnosticRegistries {
32- private val registries = HashMap <
32+ val registries = HashMap <
3333 RegistryKey <out Registry <* >? > ,
34- MutableList <RegistryHolder < * >>
34+ MutableList <Pair < Identifier , * >>
3535 > ()
3636
3737 /* *
3838 * Registers a temporary registry holder.
39- * This should be called after the controller has dumped its registries.
40- * If not, the holder will not be dumped into its respective registry .
39+ *
40+ * This should be called before the given registry has been loaded .
4141 *
4242 * @param registry The registry to register the holder for.
4343 * @param id The identifier for the registry entry.
4444 * @param value The value to register.
45- *
46- * @return The registry holder.
47- */
48- fun <T > register (registry : Registry <T >, id : Identifier , value : T ): RegistryHolder <T > {
49- val holder = RegistryHolder (id, value)
50-
51- registries.getOrPut(registry.key) { mutableListOf () }.add(holder)
52- return holder
53- }
54-
55- /* *
56- * Loads the temporary registry holders into the actual registry.
57- * This should be called after all registry holders have been created.
58- *
59- * @param registry The registry to dump into.
6045 */
61- fun dump (registry : Registry <* >? ) = dump(registry, wrapper = null )
46+ fun <T > register (registry : Registry <T >, id : Identifier , value : T ) =
47+ registries.getOrPut(registry.key) { mutableListOf () }.add(id to value)
6248
6349 /* *
6450 * Loads the temporary registry holders into the actual registry.
@@ -69,27 +55,31 @@ object AgnosticRegistries {
6955 *
7056 * @return Whether there were temporary registries or not or null if the registry is null.
7157 */
72- fun dump (registry : Registry <* >? , wrapper : RegistryWrapper <* >? ): Boolean? {
73- if (registry == null || registry !is SimpleRegistry ) return null
58+ fun <T : Any > dump (registry : Registry <T >, wrapper : RegistryWrapper <T > = defaultWrapper(registry)): Boolean? {
7459 val key = registry.key
7560
76- registries[key]?.forEach { it.handleRegister(wrapper ? : defaultWrapper(registry)) }
61+ registries[key]?.forEach { (id, value) ->
62+ @Suppress(" Unchecked_cast" )
63+ val v = value as ? T ? : return @forEach
64+ wrapper.registerForHolder(id, v)
65+ }
66+
7767 return registries.remove(key) != null
7868 }
7969
8070 /* *
8171 * Default registry wrapper for vanilla registries.
8272 */
83- @Suppress(" UNCHECKED_CAST" )
84- private fun defaultWrapper (registry : SimpleRegistry <* >): RegistryWrapper <* > {
85- return object : RegistryWrapper <Any > {
86- override fun <T > registerForHolder (id : Identifier ? , value : T ): RegistryEntry <T > {
87- registry.frozen = false // fuck off
88- val entry = Registry .registerReference(registry as Registry <T >, id, value)
89- registry.frozen = true
73+ fun <T : Any > defaultWrapper (registry : Registry <T >) = object : RegistryWrapper <T > {
74+ override fun registerForHolder (id : Identifier , value : T ): RegistryEntry <T > {
75+ val simple = registry as ? SimpleRegistry
76+ val frozen = simple?.frozen ? : true // will never be null
77+
78+ simple?.let { it.frozen = false } // fuck off
79+ val entry = Registry .registerReference(registry, id, value)
80+ simple?.let { it.frozen = frozen }
9081
91- return entry
92- }
82+ return entry
9383 }
9484 }
9585}
0 commit comments