diff --git a/common/src/main/kotlin/com/lambda/core/Loadable.kt b/common/src/main/kotlin/com/lambda/core/Loadable.kt index 7b5deb4d6..6d4ac4c89 100644 --- a/common/src/main/kotlin/com/lambda/core/Loadable.kt +++ b/common/src/main/kotlin/com/lambda/core/Loadable.kt @@ -1,5 +1,8 @@ package com.lambda.core +/** + * Represents a loadable object. + */ interface Loadable { fun load() = this::class.simpleName?.let { "Loaded $it" } ?: "Loaded" -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/lambda/core/PingManager.kt b/common/src/main/kotlin/com/lambda/core/PingManager.kt index 27a3919e0..73eb0ff60 100644 --- a/common/src/main/kotlin/com/lambda/core/PingManager.kt +++ b/common/src/main/kotlin/com/lambda/core/PingManager.kt @@ -26,4 +26,4 @@ object PingManager : Loadable { pings.add(Util.getMeasuringTimeMs() - event.packet.startTime) } } -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/lambda/core/TimerManager.kt b/common/src/main/kotlin/com/lambda/core/TimerManager.kt index 65196829b..c648921cc 100644 --- a/common/src/main/kotlin/com/lambda/core/TimerManager.kt +++ b/common/src/main/kotlin/com/lambda/core/TimerManager.kt @@ -16,4 +16,4 @@ object TimerManager : Loadable { } } } -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt b/common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt new file mode 100644 index 000000000..fe22ba62a --- /dev/null +++ b/common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt @@ -0,0 +1,26 @@ +package com.lambda.core.registry + +import net.minecraft.registry.Registry +import net.minecraft.registry.RegistryKey +import net.minecraft.util.Identifier + + +object RegistryController { + private val REGISTRIES = HashMap?>, MutableList>>() + + fun register(registry: Registry, id: Identifier, value: T): RegistryHolder { + val holder = RegistryHolder(id, value) + + REGISTRIES.computeIfAbsent(registry.key) { mutableListOf(holder) } + return holder + } + + /** + * Loads the temporary registry holders into the actual registry. + * This should be called after all registry holders have been created. + */ + fun register(resourceKey: RegistryKey?>, registry: RegistryWrapper<*>) { + REGISTRIES[resourceKey]?.forEach { it.handleRegister(registry) } + REGISTRIES.remove(resourceKey) + } +} diff --git a/common/src/main/kotlin/com/lambda/core/registry/RegistryHolder.kt b/common/src/main/kotlin/com/lambda/core/registry/RegistryHolder.kt new file mode 100644 index 000000000..e448857d9 --- /dev/null +++ b/common/src/main/kotlin/com/lambda/core/registry/RegistryHolder.kt @@ -0,0 +1,25 @@ +package com.lambda.core.registry + +import net.minecraft.registry.entry.RegistryEntry +import net.minecraft.util.Identifier +import java.util.function.Supplier + + +class RegistryHolder internal constructor( + val id: Identifier, + var value: T, +) : Supplier { + private var holder: RegistryEntry? = null + + override fun get(): T { + with(holder) { + checkNotNull(this) { "RegistryHolder not populated" } + + return this.value() + } + } + + fun handleRegister(registry: RegistryWrapper<*>) { + this.holder = registry.registerForHolder(id, value) + } +} diff --git a/common/src/main/kotlin/com/lambda/core/registry/RegistryWrapper.kt b/common/src/main/kotlin/com/lambda/core/registry/RegistryWrapper.kt new file mode 100644 index 000000000..17cecd0df --- /dev/null +++ b/common/src/main/kotlin/com/lambda/core/registry/RegistryWrapper.kt @@ -0,0 +1,8 @@ +package com.lambda.core.registry + +import net.minecraft.registry.entry.RegistryEntry +import net.minecraft.util.Identifier + +interface RegistryWrapper { + fun registerForHolder(id: Identifier?, value: T): RegistryEntry +} diff --git a/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaFont.kt b/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaFont.kt index 7651d7704..f77f841bd 100644 --- a/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaFont.kt +++ b/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaFont.kt @@ -26,4 +26,4 @@ enum class LambdaFont(private val fontName: String) { return "Loaded ${entries.size} fonts" } } -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt b/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt index 878b57ee9..1b5d1e96d 100644 --- a/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt +++ b/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt @@ -17,11 +17,12 @@ abstract class AbstractGuiConfigurable( var mainWindows by setting("windows", defaultWindows) open var customWindows = mutableListOf() - private val defaultWindows get() = - tags.mapIndexed { index, tag -> - TagWindow(tag, ownerGui).apply { - val step = 5.0 - position = Vec2d((width + step) * index, 0.0) + step + private val defaultWindows + get() = + tags.mapIndexed { index, tag -> + TagWindow(tag, ownerGui).apply { + val step = 5.0 + position = Vec2d((width + step) * index, 0.0) + step + } } - } -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt b/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt index fe9fb046c..60fe0b57d 100644 --- a/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt +++ b/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt @@ -1,10 +1,9 @@ package com.lambda.interaction import com.lambda.Lambda.mc -import com.lambda.config.groups.IRotationConfig import com.lambda.config.groups.RotationSettings -import com.lambda.core.Loadable import com.lambda.context.SafeContext +import com.lambda.core.Loadable import com.lambda.event.EventFlow.post import com.lambda.event.events.* import com.lambda.event.listener.SafeListener.Companion.listener diff --git a/common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt b/common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt index ad4d35bbf..bb09b9bf9 100644 --- a/common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt +++ b/common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt @@ -19,7 +19,7 @@ import net.minecraft.item.ItemStack import net.minecraft.screen.GenericContainerScreenHandler import net.minecraft.screen.ScreenHandler import net.minecraft.screen.ScreenHandlerType -import java.util.TreeSet +import java.util.* // ToDo: Make this a Configurable to save container caches. Should use a cached region based storage system. object ContainerManager : Loadable { @@ -121,4 +121,4 @@ object ContainerManager : Loadable { // fun SafeContext.nextDisposable() = player.combined.firstOrNull { it.item in TaskFlow.disposables } class NoContainerFound(selection: StackSelection): Exception("No container found matching $selection") -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt b/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt index 06a89a84a..ac791f868 100644 --- a/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt +++ b/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt @@ -1,13 +1,13 @@ package com.lambda.sound import com.lambda.core.Loadable +import com.lambda.core.registry.RegistryController import net.minecraft.registry.Registries -import net.minecraft.registry.Registry object SoundRegistry : Loadable { override fun load(): String { LambdaSound.entries.forEach { - Registry.register(Registries.SOUND_EVENT, it.id, it.event) + RegistryController.register(Registries.SOUND_EVENT, it.id, it.event) } return "Loaded ${LambdaSound.entries.size} sounds" diff --git a/fabric/src/main/kotlin/com/lambda/fabric/LambdaFabric.kt b/fabric/src/main/kotlin/com/lambda/fabric/LambdaFabric.kt index 3dd74603a..50b9b5dea 100644 --- a/fabric/src/main/kotlin/com/lambda/fabric/LambdaFabric.kt +++ b/fabric/src/main/kotlin/com/lambda/fabric/LambdaFabric.kt @@ -4,11 +4,25 @@ import com.lambda.Lambda import com.lambda.Lambda.LOG import com.lambda.Lambda.MOD_NAME import com.lambda.Lambda.VERSION +import com.lambda.core.registry.RegistryController +import com.lambda.core.registry.RegistryWrapper import net.fabricmc.api.ClientModInitializer +import net.minecraft.registry.Registries +import net.minecraft.registry.Registry +import net.minecraft.registry.entry.RegistryEntry +import net.minecraft.util.Identifier object LambdaFabric : ClientModInitializer { override fun onInitializeClient() { Lambda.initialize() LOG.info("$MOD_NAME Fabric $VERSION initialized.") + + Registries.REGISTRIES.forEach { registry -> + RegistryController.register(registry.key, object : RegistryWrapper { + override fun registerForHolder(id: Identifier?, value: T): RegistryEntry { + return Registry.registerReference(registry as Registry, id, value) + } + }) + } } } diff --git a/forge/build.gradle.kts b/forge/build.gradle.kts index 3d3a838d8..370900cbf 100644 --- a/forge/build.gradle.kts +++ b/forge/build.gradle.kts @@ -59,11 +59,13 @@ val shadowBundle: Configuration by configurations.creating { fun DependencyHandlerScope.setupConfigurations() { includeLib.dependencies.forEach { + implementation(it) forgeRuntimeLibrary(it) include(it) } includeMod.dependencies.forEach { + modImplementation(it) forgeRuntimeLibrary(it) include(it) } diff --git a/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt b/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt index 4ba6b9ec0..d8e7474a5 100644 --- a/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt +++ b/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt @@ -4,13 +4,31 @@ import com.lambda.Lambda import com.lambda.Lambda.LOG import com.lambda.Lambda.MOD_NAME import com.lambda.Lambda.VERSION +import com.lambda.core.registry.RegistryController +import com.lambda.core.registry.RegistryWrapper +import net.minecraft.registry.Registry +import net.minecraft.registry.entry.RegistryEntry +import net.minecraft.util.Identifier +import net.minecraftforge.eventbus.api.SubscribeEvent import net.minecraftforge.fml.common.Mod +import net.minecraftforge.fml.common.Mod.EventBusSubscriber +import net.minecraftforge.registries.RegisterEvent @Mod(Lambda.MOD_ID) +@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD) object LambdaForge { init { Lambda.initialize() LOG.info("$MOD_NAME Forge $VERSION initialized.") } + + @SubscribeEvent + fun onRegistrySetup(event: RegisterEvent) { + RegistryController.register(event.registryKey, object : RegistryWrapper { + override fun registerForHolder(id: Identifier?, value: T): RegistryEntry { + return Registry.registerReference(event.getVanillaRegistry(), id, value) + } + }) + } } diff --git a/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt b/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt index ff0993b41..203676028 100644 --- a/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt +++ b/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt @@ -4,12 +4,30 @@ import com.lambda.Lambda import com.lambda.Lambda.LOG import com.lambda.Lambda.MOD_NAME import com.lambda.Lambda.VERSION +import com.lambda.core.registry.RegistryController +import com.lambda.core.registry.RegistryWrapper +import net.minecraft.registry.Registry +import net.minecraft.registry.entry.RegistryEntry +import net.minecraft.util.Identifier +import net.neoforged.bus.api.SubscribeEvent import net.neoforged.fml.common.Mod +import net.neoforged.fml.common.Mod.EventBusSubscriber +import net.neoforged.neoforge.registries.RegisterEvent @Mod(Lambda.MOD_ID) +@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD) object LambdaNeoForge { init { Lambda.initialize() - LOG.info("$MOD_NAME NeoForge $VERSION initialized.") + LOG.info("$MOD_NAME Forge $VERSION initialized.") + } + + @SubscribeEvent + fun onRegistrySetup(event: RegisterEvent) { + RegistryController.register(event.registryKey, object : RegistryWrapper { + override fun registerForHolder(id: Identifier?, value: T): RegistryEntry { + return Registry.registerReference(event.registry as Registry, id, value) + } + }) } }