Skip to content

Commit 88aa476

Browse files
committed
Reworked registries
1 parent 9a4f01c commit 88aa476

File tree

5 files changed

+30
-82
lines changed

5 files changed

+30
-82
lines changed

common/src/main/kotlin/com/lambda/core/registry/AgnosticRegistries.kt renamed to common/src/main/kotlin/com/lambda/core/AgnosticRegistries.kt

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
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

2020
import net.minecraft.registry.Registry
2121
import net.minecraft.registry.RegistryKey
@@ -29,36 +29,22 @@ import net.minecraft.util.Identifier
2929
* It allows for environment-agnostic registry handling.
3030
*/
3131
object 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
}

common/src/main/kotlin/com/lambda/core/registry/RegistryWrapper.kt renamed to common/src/main/kotlin/com/lambda/core/RegistryWrapper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
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

2020
import net.minecraft.registry.entry.RegistryEntry
2121
import net.minecraft.util.Identifier
2222

2323
interface RegistryWrapper<T> {
24-
fun <T> registerForHolder(id: Identifier?, value: T): RegistryEntry<T>
24+
fun registerForHolder(id: Identifier, value: T): RegistryEntry<T>
2525
}

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package com.lambda.sound
1919

2020
import com.lambda.core.Loadable
21-
import com.lambda.core.registry.AgnosticRegistries
21+
import com.lambda.core.AgnosticRegistries
2222
import net.minecraft.registry.Registries
2323

2424
object SoundRegistry : Loadable {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ import com.lambda.Lambda
2121
import com.lambda.Lambda.LOG
2222
import com.lambda.Lambda.MOD_NAME
2323
import com.lambda.Lambda.VERSION
24-
import com.lambda.core.registry.AgnosticRegistries
24+
import com.lambda.core.AgnosticRegistries
2525
import net.fabricmc.api.ClientModInitializer
2626
import net.minecraft.registry.Registries
2727

2828
object LambdaFabric : ClientModInitializer {
2929
override fun onInitializeClient() {
30-
Lambda.initialize {
31-
Registries.REGISTRIES.forEach(AgnosticRegistries::dump)
32-
LOG.info("$MOD_NAME Fabric $VERSION was successfully initialized after $it ms\n")
30+
Lambda.initialize { time ->
31+
Registries.REGISTRIES.forEach { AgnosticRegistries.dump(it) }
32+
LOG.info("$MOD_NAME Fabric $VERSION was successfully initialized after $time ms\n")
3333
}
3434
}
3535
}

0 commit comments

Comments
 (0)