From a5e417cc351bd56a34592cb0597c510baa68f50d Mon Sep 17 00:00:00 2001 From: Kamigen <46357922+Edouard127@users.noreply.github.com> Date: Mon, 8 Jul 2024 15:48:48 -0400 Subject: [PATCH 1/3] Added lifecycle on forge-like --- .../com/lambda/command/CommandRegistry.kt | 2 +- .../main/kotlin/com/lambda/core/Loadable.kt | 5 -- .../kotlin/com/lambda/core/PingManager.kt | 3 +- .../kotlin/com/lambda/core/TimerManager.kt | 3 +- .../com/lambda/core/lifecycle/Loadable.kt | 59 +++++++++++++++++++ .../core/registry/RegistryController.kt | 30 ++++++++++ .../lambda/core/registry/RegistryHolder.kt | 25 ++++++++ .../lambda/core/registry/RegistryWrapper.kt | 8 +++ .../com/lambda/friend/FriendRegistry.kt | 2 +- .../graphics/renderer/gui/font/LambdaEmoji.kt | 2 +- .../graphics/renderer/gui/font/LambdaFont.kt | 4 +- .../com/lambda/gui/AbstractGuiConfigurable.kt | 4 +- .../lambda/interaction/PlayerPacketManager.kt | 2 +- .../com/lambda/interaction/RotationManager.kt | 3 +- .../interaction/material/ContainerManager.kt | 6 +- .../com/lambda/module/ModuleRegistry.kt | 2 +- .../kotlin/com/lambda/sound/SoundRegistry.kt | 8 +-- forge/build.gradle.kts | 2 + .../kotlin/com/lambda/forge/LambdaForge.kt | 40 +++++++++++++ .../com/lambda/neoforge/LambdaNeoForge.kt | 40 +++++++++++++ 20 files changed, 225 insertions(+), 25 deletions(-) delete mode 100644 common/src/main/kotlin/com/lambda/core/Loadable.kt create mode 100644 common/src/main/kotlin/com/lambda/core/lifecycle/Loadable.kt create mode 100644 common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt create mode 100644 common/src/main/kotlin/com/lambda/core/registry/RegistryHolder.kt create mode 100644 common/src/main/kotlin/com/lambda/core/registry/RegistryWrapper.kt diff --git a/common/src/main/kotlin/com/lambda/command/CommandRegistry.kt b/common/src/main/kotlin/com/lambda/command/CommandRegistry.kt index f3d3d98a0..49335c76c 100644 --- a/common/src/main/kotlin/com/lambda/command/CommandRegistry.kt +++ b/common/src/main/kotlin/com/lambda/command/CommandRegistry.kt @@ -2,7 +2,7 @@ package com.lambda.command import com.lambda.config.Configurable import com.lambda.config.configurations.LambdaConfig -import com.lambda.core.Loadable +import com.lambda.core.lifecycle.Loadable import org.reflections.Reflections import org.reflections.scanners.Scanners import org.reflections.util.ConfigurationBuilder diff --git a/common/src/main/kotlin/com/lambda/core/Loadable.kt b/common/src/main/kotlin/com/lambda/core/Loadable.kt deleted file mode 100644 index 7b5deb4d6..000000000 --- a/common/src/main/kotlin/com/lambda/core/Loadable.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.lambda.core - -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..31d05841b 100644 --- a/common/src/main/kotlin/com/lambda/core/PingManager.kt +++ b/common/src/main/kotlin/com/lambda/core/PingManager.kt @@ -1,5 +1,6 @@ package com.lambda.core +import com.lambda.core.lifecycle.Loadable import com.lambda.event.events.PacketEvent import com.lambda.event.events.TickEvent import com.lambda.event.listener.SafeListener.Companion.listener @@ -26,4 +27,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..129908ca1 100644 --- a/common/src/main/kotlin/com/lambda/core/TimerManager.kt +++ b/common/src/main/kotlin/com/lambda/core/TimerManager.kt @@ -1,5 +1,6 @@ package com.lambda.core +import com.lambda.core.lifecycle.Loadable import com.lambda.event.EventFlow.post import com.lambda.event.events.ClientEvent import com.lambda.event.events.TickEvent @@ -16,4 +17,4 @@ object TimerManager : Loadable { } } } -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/lambda/core/lifecycle/Loadable.kt b/common/src/main/kotlin/com/lambda/core/lifecycle/Loadable.kt new file mode 100644 index 000000000..77f97ad1d --- /dev/null +++ b/common/src/main/kotlin/com/lambda/core/lifecycle/Loadable.kt @@ -0,0 +1,59 @@ +package com.lambda.core.lifecycle + +import com.lambda.core.lifecycle.Lifecycle.Complete + +internal annotation class ForgeLikeOnly + +/** + * Represents the lifecycle of the mod loader process for Forge and NeoForge mods. + * Managers ran on Fabric with one of these lifecycles will be ignored and executed on the [Complete] + * lifecycle instead. + */ +@ForgeLikeOnly +sealed class Lifecycle { + /** + * Fired when the mod events are registered. Not to confuse with Lambda's event bus. + */ + interface Initialization : Loadable + + /** + * Fired right after the mod constructor is called + */ + interface Construction : Loadable + + /** + * Fired for each individual Minecraft registry. + * As long as you are still in the lifecycle scope you are allowed + * to add entries without any issues. + */ + interface Registry : Loadable + + /** + * Fired during the early initialisation of Minecraft. + */ + interface Common : Loadable + + /** + * Sided-setup on their respective side, however only `FMLClientSetupEvent` will be fired + * because Lambda is not a server-sided mod. + */ + interface SideSetup : Loadable + + /** + * Fired when mods can send messages to each other for cross-mod compatibility. + * This is the last event before the mod is considered fully loaded. + */ + interface InterModCommunication : Loadable + + /** + * Fired when the mod is fully loaded and ready to be used. + */ + interface Complete : Loadable +} + +/** + * Represents a loadable object. + */ +interface Loadable { + fun load() = this::class.simpleName?.let { "Loaded $it" } ?: "Loaded" +} 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..74d29207b --- /dev/null +++ b/common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt @@ -0,0 +1,30 @@ +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>>() + private var frozen = false + + fun register(registry: Registry, id: Identifier, value: T): RegistryHolder { + check(!frozen) { "Cannot register, RegistryController is frozen." } + 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. + * It will also freeze the controller to prevent further registrations. + */ + fun register(resourceKey: RegistryKey?>, registry: RegistryWrapper<*>) { + frozen = true + 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/friend/FriendRegistry.kt b/common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt index 553535964..5bc1e0438 100644 --- a/common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt +++ b/common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt @@ -2,7 +2,7 @@ package com.lambda.friend import com.lambda.config.Configurable import com.lambda.config.configurations.FriendConfig -import com.lambda.core.Loadable +import com.lambda.core.lifecycle.Loadable import com.mojang.authlib.GameProfile object FriendRegistry : Configurable(FriendConfig), Loadable { diff --git a/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt b/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt index 87cef41fd..19c85fb38 100644 --- a/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt +++ b/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt @@ -1,6 +1,6 @@ package com.lambda.graphics.renderer.gui.font -import com.lambda.core.Loadable +import com.lambda.core.lifecycle.Loadable import com.lambda.graphics.renderer.gui.font.glyph.EmojiGlyphs enum class LambdaEmoji(private val zipUrl: String) { 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..96c487eee 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 @@ -1,6 +1,6 @@ package com.lambda.graphics.renderer.gui.font -import com.lambda.core.Loadable +import com.lambda.core.lifecycle.Loadable import com.lambda.graphics.renderer.gui.font.glyph.FontGlyphs import com.lambda.util.LambdaResource import java.awt.Font @@ -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..5ed527bf7 100644 --- a/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt +++ b/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt @@ -2,7 +2,7 @@ package com.lambda.gui import com.lambda.config.Configurable import com.lambda.config.configurations.GuiConfig -import com.lambda.core.Loadable +import com.lambda.core.lifecycle.Loadable import com.lambda.gui.impl.AbstractClickGui import com.lambda.gui.impl.clickgui.windows.tag.CustomModuleWindow import com.lambda.gui.impl.clickgui.windows.tag.TagWindow @@ -24,4 +24,4 @@ abstract class AbstractGuiConfigurable( position = Vec2d((width + step) * index, 0.0) + step } } -} \ No newline at end of file +} diff --git a/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt b/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt index c1922f55e..9c8c166bd 100644 --- a/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt +++ b/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt @@ -1,7 +1,7 @@ package com.lambda.interaction import com.lambda.context.SafeContext -import com.lambda.core.Loadable +import com.lambda.core.lifecycle.Loadable import com.lambda.event.EventFlow.post import com.lambda.event.EventFlow.postChecked import com.lambda.event.events.PlayerPacketEvent diff --git a/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt b/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt index fe9fb046c..814ee934a 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.lifecycle.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..bb11146fe 100644 --- a/common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt +++ b/common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt @@ -1,6 +1,6 @@ package com.lambda.interaction.material -import com.lambda.core.Loadable +import com.lambda.core.lifecycle.Loadable import com.lambda.event.events.InteractionEvent import com.lambda.event.events.ScreenHandlerEvent import com.lambda.event.listener.SafeListener.Companion.listener @@ -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/module/ModuleRegistry.kt b/common/src/main/kotlin/com/lambda/module/ModuleRegistry.kt index 5e49400e9..99eaa5db2 100644 --- a/common/src/main/kotlin/com/lambda/module/ModuleRegistry.kt +++ b/common/src/main/kotlin/com/lambda/module/ModuleRegistry.kt @@ -1,6 +1,6 @@ package com.lambda.module -import com.lambda.core.Loadable +import com.lambda.core.lifecycle.Loadable import org.reflections.Reflections import org.reflections.scanners.Scanners import org.reflections.util.ConfigurationBuilder diff --git a/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt b/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt index 06a89a84a..373c6e0d4 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.lifecycle.Lifecycle +import com.lambda.core.registry.RegistryController import net.minecraft.registry.Registries -import net.minecraft.registry.Registry -object SoundRegistry : Loadable { +object SoundRegistry : Lifecycle.Registry { 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/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..30c58e34a 100644 --- a/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt +++ b/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt @@ -4,13 +4,53 @@ 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.fml.common.Mod +import net.minecraftforge.fml.common.Mod.EventBusSubscriber +import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent +import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent +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.") } + + private fun onConstructMod(event: FMLConstructModEvent) { + LOG.info("Construct mod event.") + } + + private fun onRegistrySetup(event: RegisterEvent) { + LOG.info("Register event for ${event.registryKey}.") + + RegistryController.register(event.registryKey, object : RegistryWrapper { + override fun registerForHolder(id: Identifier?, value: T): RegistryEntry { + return Registry.registerReference(event.getVanillaRegistry(), id, value) + } + }) + } + + private fun onClientSetup(event: FMLClientSetupEvent) { + LOG.info("Client setup event.") + } + + private fun onSidedSetup(event: FMLClientSetupEvent) { + LOG.info("Sided setup event.") + } + + private fun onInterModComms(event: FMLClientSetupEvent) { + LOG.info("Inter-mod comms event.") + } + + private fun onComplete(event: FMLClientSetupEvent) { + LOG.info("Complete event.") + } } diff --git a/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt b/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt index ff0993b41..60f7b22ef 100644 --- a/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt +++ b/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt @@ -4,12 +4,52 @@ 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.fml.common.Mod +import net.neoforged.fml.common.Mod.EventBusSubscriber +import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent +import net.neoforged.fml.event.lifecycle.FMLConstructModEvent +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.") } + + private fun onConstructMod(event: FMLConstructModEvent) { + LOG.info("Construct mod event.") + } + + private fun onRegistrySetup(event: RegisterEvent) { + LOG.info("Register event for ${event.registryKey}.") + + RegistryController.register(event.registryKey, object : RegistryWrapper { + override fun registerForHolder(id: Identifier?, value: T): RegistryEntry { + return Registry.registerReference(event.registry as Registry, id, value) + } + }) + } + + private fun onClientSetup(event: FMLClientSetupEvent) { + LOG.info("Client setup event.") + } + + private fun onSidedSetup(event: FMLClientSetupEvent) { + LOG.info("Sided setup event.") + } + + private fun onInterModComms(event: FMLClientSetupEvent) { + LOG.info("Inter-mod comms event.") + } + + private fun onComplete(event: FMLClientSetupEvent) { + LOG.info("Complete event.") + } } From 1f6a880af3db74dbacf0ffafed04efd327e3007b Mon Sep 17 00:00:00 2001 From: Kamigen <46357922+Edouard127@users.noreply.github.com> Date: Mon, 8 Jul 2024 19:00:55 -0400 Subject: [PATCH 2/3] Made the entire thing simpler --- .../com/lambda/command/CommandRegistry.kt | 2 +- .../main/kotlin/com/lambda/core/Loadable.kt | 8 +++ .../src/main/kotlin/com/lambda/core/Loader.kt | 2 +- .../kotlin/com/lambda/core/PingManager.kt | 1 - .../kotlin/com/lambda/core/TimerManager.kt | 1 - .../com/lambda/core/lifecycle/Loadable.kt | 59 ------------------- .../core/registry/RegistryController.kt | 4 -- .../com/lambda/friend/FriendRegistry.kt | 2 +- .../graphics/renderer/gui/font/LambdaEmoji.kt | 2 +- .../graphics/renderer/gui/font/LambdaFont.kt | 2 +- .../com/lambda/gui/AbstractGuiConfigurable.kt | 15 ++--- .../lambda/interaction/PlayerPacketManager.kt | 2 +- .../com/lambda/interaction/RotationManager.kt | 2 +- .../interaction/material/ContainerManager.kt | 2 +- .../com/lambda/module/ModuleRegistry.kt | 2 +- .../kotlin/com/lambda/sound/SoundRegistry.kt | 4 +- .../kotlin/com/lambda/fabric/LambdaFabric.kt | 14 +++++ .../kotlin/com/lambda/forge/LambdaForge.kt | 28 +-------- .../com/lambda/neoforge/LambdaNeoForge.kt | 30 ++-------- 19 files changed, 48 insertions(+), 134 deletions(-) create mode 100644 common/src/main/kotlin/com/lambda/core/Loadable.kt delete mode 100644 common/src/main/kotlin/com/lambda/core/lifecycle/Loadable.kt diff --git a/common/src/main/kotlin/com/lambda/command/CommandRegistry.kt b/common/src/main/kotlin/com/lambda/command/CommandRegistry.kt index 49335c76c..f3d3d98a0 100644 --- a/common/src/main/kotlin/com/lambda/command/CommandRegistry.kt +++ b/common/src/main/kotlin/com/lambda/command/CommandRegistry.kt @@ -2,7 +2,7 @@ package com.lambda.command import com.lambda.config.Configurable import com.lambda.config.configurations.LambdaConfig -import com.lambda.core.lifecycle.Loadable +import com.lambda.core.Loadable import org.reflections.Reflections import org.reflections.scanners.Scanners import org.reflections.util.ConfigurationBuilder diff --git a/common/src/main/kotlin/com/lambda/core/Loadable.kt b/common/src/main/kotlin/com/lambda/core/Loadable.kt new file mode 100644 index 000000000..6d4ac4c89 --- /dev/null +++ b/common/src/main/kotlin/com/lambda/core/Loadable.kt @@ -0,0 +1,8 @@ +package com.lambda.core + +/** + * Represents a loadable object. + */ +interface Loadable { + fun load() = this::class.simpleName?.let { "Loaded $it" } ?: "Loaded" +} diff --git a/common/src/main/kotlin/com/lambda/core/Loader.kt b/common/src/main/kotlin/com/lambda/core/Loader.kt index 271143d2c..fb4163daa 100644 --- a/common/src/main/kotlin/com/lambda/core/Loader.kt +++ b/common/src/main/kotlin/com/lambda/core/Loader.kt @@ -24,7 +24,7 @@ object Loader { val runtime: String get() = "${(System.currentTimeMillis() - started).toDuration(DurationUnit.MILLISECONDS)}" - private val loadables = listOf( + val loadables = listOf( ModuleRegistry, CommandRegistry, RotationManager, diff --git a/common/src/main/kotlin/com/lambda/core/PingManager.kt b/common/src/main/kotlin/com/lambda/core/PingManager.kt index 31d05841b..73eb0ff60 100644 --- a/common/src/main/kotlin/com/lambda/core/PingManager.kt +++ b/common/src/main/kotlin/com/lambda/core/PingManager.kt @@ -1,6 +1,5 @@ package com.lambda.core -import com.lambda.core.lifecycle.Loadable import com.lambda.event.events.PacketEvent import com.lambda.event.events.TickEvent import com.lambda.event.listener.SafeListener.Companion.listener diff --git a/common/src/main/kotlin/com/lambda/core/TimerManager.kt b/common/src/main/kotlin/com/lambda/core/TimerManager.kt index 129908ca1..c648921cc 100644 --- a/common/src/main/kotlin/com/lambda/core/TimerManager.kt +++ b/common/src/main/kotlin/com/lambda/core/TimerManager.kt @@ -1,6 +1,5 @@ package com.lambda.core -import com.lambda.core.lifecycle.Loadable import com.lambda.event.EventFlow.post import com.lambda.event.events.ClientEvent import com.lambda.event.events.TickEvent diff --git a/common/src/main/kotlin/com/lambda/core/lifecycle/Loadable.kt b/common/src/main/kotlin/com/lambda/core/lifecycle/Loadable.kt deleted file mode 100644 index 77f97ad1d..000000000 --- a/common/src/main/kotlin/com/lambda/core/lifecycle/Loadable.kt +++ /dev/null @@ -1,59 +0,0 @@ -package com.lambda.core.lifecycle - -import com.lambda.core.lifecycle.Lifecycle.Complete - -internal annotation class ForgeLikeOnly - -/** - * Represents the lifecycle of the mod loader process for Forge and NeoForge mods. - * Managers ran on Fabric with one of these lifecycles will be ignored and executed on the [Complete] - * lifecycle instead. - */ -@ForgeLikeOnly -sealed class Lifecycle { - /** - * Fired when the mod events are registered. Not to confuse with Lambda's event bus. - */ - interface Initialization : Loadable - - /** - * Fired right after the mod constructor is called - */ - interface Construction : Loadable - - /** - * Fired for each individual Minecraft registry. - * As long as you are still in the lifecycle scope you are allowed - * to add entries without any issues. - */ - interface Registry : Loadable - - /** - * Fired during the early initialisation of Minecraft. - */ - interface Common : Loadable - - /** - * Sided-setup on their respective side, however only `FMLClientSetupEvent` will be fired - * because Lambda is not a server-sided mod. - */ - interface SideSetup : Loadable - - /** - * Fired when mods can send messages to each other for cross-mod compatibility. - * This is the last event before the mod is considered fully loaded. - */ - interface InterModCommunication : Loadable - - /** - * Fired when the mod is fully loaded and ready to be used. - */ - interface Complete : Loadable -} - -/** - * Represents a loadable object. - */ -interface Loadable { - fun load() = this::class.simpleName?.let { "Loaded $it" } ?: "Loaded" -} diff --git a/common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt b/common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt index 74d29207b..fe22ba62a 100644 --- a/common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt +++ b/common/src/main/kotlin/com/lambda/core/registry/RegistryController.kt @@ -7,10 +7,8 @@ import net.minecraft.util.Identifier object RegistryController { private val REGISTRIES = HashMap?>, MutableList>>() - private var frozen = false fun register(registry: Registry, id: Identifier, value: T): RegistryHolder { - check(!frozen) { "Cannot register, RegistryController is frozen." } val holder = RegistryHolder(id, value) REGISTRIES.computeIfAbsent(registry.key) { mutableListOf(holder) } @@ -20,10 +18,8 @@ object RegistryController { /** * Loads the temporary registry holders into the actual registry. * This should be called after all registry holders have been created. - * It will also freeze the controller to prevent further registrations. */ fun register(resourceKey: RegistryKey?>, registry: RegistryWrapper<*>) { - frozen = true REGISTRIES[resourceKey]?.forEach { it.handleRegister(registry) } REGISTRIES.remove(resourceKey) } diff --git a/common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt b/common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt index 5bc1e0438..553535964 100644 --- a/common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt +++ b/common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt @@ -2,7 +2,7 @@ package com.lambda.friend import com.lambda.config.Configurable import com.lambda.config.configurations.FriendConfig -import com.lambda.core.lifecycle.Loadable +import com.lambda.core.Loadable import com.mojang.authlib.GameProfile object FriendRegistry : Configurable(FriendConfig), Loadable { diff --git a/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt b/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt index 19c85fb38..87cef41fd 100644 --- a/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt +++ b/common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt @@ -1,6 +1,6 @@ package com.lambda.graphics.renderer.gui.font -import com.lambda.core.lifecycle.Loadable +import com.lambda.core.Loadable import com.lambda.graphics.renderer.gui.font.glyph.EmojiGlyphs enum class LambdaEmoji(private val zipUrl: String) { 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 96c487eee..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 @@ -1,6 +1,6 @@ package com.lambda.graphics.renderer.gui.font -import com.lambda.core.lifecycle.Loadable +import com.lambda.core.Loadable import com.lambda.graphics.renderer.gui.font.glyph.FontGlyphs import com.lambda.util.LambdaResource import java.awt.Font diff --git a/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt b/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt index 5ed527bf7..1b5d1e96d 100644 --- a/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt +++ b/common/src/main/kotlin/com/lambda/gui/AbstractGuiConfigurable.kt @@ -2,7 +2,7 @@ package com.lambda.gui import com.lambda.config.Configurable import com.lambda.config.configurations.GuiConfig -import com.lambda.core.lifecycle.Loadable +import com.lambda.core.Loadable import com.lambda.gui.impl.AbstractClickGui import com.lambda.gui.impl.clickgui.windows.tag.CustomModuleWindow import com.lambda.gui.impl.clickgui.windows.tag.TagWindow @@ -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 + } } - } } diff --git a/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt b/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt index 9c8c166bd..c1922f55e 100644 --- a/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt +++ b/common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt @@ -1,7 +1,7 @@ package com.lambda.interaction import com.lambda.context.SafeContext -import com.lambda.core.lifecycle.Loadable +import com.lambda.core.Loadable import com.lambda.event.EventFlow.post import com.lambda.event.EventFlow.postChecked import com.lambda.event.events.PlayerPacketEvent diff --git a/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt b/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt index 814ee934a..60fe0b57d 100644 --- a/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt +++ b/common/src/main/kotlin/com/lambda/interaction/RotationManager.kt @@ -3,7 +3,7 @@ package com.lambda.interaction import com.lambda.Lambda.mc import com.lambda.config.groups.RotationSettings import com.lambda.context.SafeContext -import com.lambda.core.lifecycle.Loadable +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 bb11146fe..bb09b9bf9 100644 --- a/common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt +++ b/common/src/main/kotlin/com/lambda/interaction/material/ContainerManager.kt @@ -1,6 +1,6 @@ package com.lambda.interaction.material -import com.lambda.core.lifecycle.Loadable +import com.lambda.core.Loadable import com.lambda.event.events.InteractionEvent import com.lambda.event.events.ScreenHandlerEvent import com.lambda.event.listener.SafeListener.Companion.listener diff --git a/common/src/main/kotlin/com/lambda/module/ModuleRegistry.kt b/common/src/main/kotlin/com/lambda/module/ModuleRegistry.kt index 99eaa5db2..5e49400e9 100644 --- a/common/src/main/kotlin/com/lambda/module/ModuleRegistry.kt +++ b/common/src/main/kotlin/com/lambda/module/ModuleRegistry.kt @@ -1,6 +1,6 @@ package com.lambda.module -import com.lambda.core.lifecycle.Loadable +import com.lambda.core.Loadable import org.reflections.Reflections import org.reflections.scanners.Scanners import org.reflections.util.ConfigurationBuilder diff --git a/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt b/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt index 373c6e0d4..ac791f868 100644 --- a/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt +++ b/common/src/main/kotlin/com/lambda/sound/SoundRegistry.kt @@ -1,10 +1,10 @@ package com.lambda.sound -import com.lambda.core.lifecycle.Lifecycle +import com.lambda.core.Loadable import com.lambda.core.registry.RegistryController import net.minecraft.registry.Registries -object SoundRegistry : Lifecycle.Registry { +object SoundRegistry : Loadable { override fun load(): String { LambdaSound.entries.forEach { RegistryController.register(Registries.SOUND_EVENT, it.id, it.event) 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/src/main/kotlin/com/lambda/forge/LambdaForge.kt b/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt index 30c58e34a..d8e7474a5 100644 --- a/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt +++ b/forge/src/main/kotlin/com/lambda/forge/LambdaForge.kt @@ -9,10 +9,9 @@ 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.fml.event.lifecycle.FMLClientSetupEvent -import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent import net.minecraftforge.registries.RegisterEvent @@ -24,33 +23,12 @@ object LambdaForge { LOG.info("$MOD_NAME Forge $VERSION initialized.") } - private fun onConstructMod(event: FMLConstructModEvent) { - LOG.info("Construct mod event.") - } - - private fun onRegistrySetup(event: RegisterEvent) { - LOG.info("Register event for ${event.registryKey}.") - + @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) } }) } - - private fun onClientSetup(event: FMLClientSetupEvent) { - LOG.info("Client setup event.") - } - - private fun onSidedSetup(event: FMLClientSetupEvent) { - LOG.info("Sided setup event.") - } - - private fun onInterModComms(event: FMLClientSetupEvent) { - LOG.info("Inter-mod comms event.") - } - - private fun onComplete(event: FMLClientSetupEvent) { - LOG.info("Complete event.") - } } diff --git a/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt b/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt index 60f7b22ef..203676028 100644 --- a/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt +++ b/neoforge/src/main/kotlin/com/lambda/neoforge/LambdaNeoForge.kt @@ -9,10 +9,9 @@ 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.fml.event.lifecycle.FMLClientSetupEvent -import net.neoforged.fml.event.lifecycle.FMLConstructModEvent import net.neoforged.neoforge.registries.RegisterEvent @Mod(Lambda.MOD_ID) @@ -20,36 +19,15 @@ import net.neoforged.neoforge.registries.RegisterEvent object LambdaNeoForge { init { Lambda.initialize() - LOG.info("$MOD_NAME NeoForge $VERSION initialized.") + LOG.info("$MOD_NAME Forge $VERSION initialized.") } - private fun onConstructMod(event: FMLConstructModEvent) { - LOG.info("Construct mod event.") - } - - private fun onRegistrySetup(event: RegisterEvent) { - LOG.info("Register event for ${event.registryKey}.") - + @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) } }) } - - private fun onClientSetup(event: FMLClientSetupEvent) { - LOG.info("Client setup event.") - } - - private fun onSidedSetup(event: FMLClientSetupEvent) { - LOG.info("Sided setup event.") - } - - private fun onInterModComms(event: FMLClientSetupEvent) { - LOG.info("Inter-mod comms event.") - } - - private fun onComplete(event: FMLClientSetupEvent) { - LOG.info("Complete event.") - } } From ab40d5ddc50ea9650ca9fc3f39b30b3a4840f5a5 Mon Sep 17 00:00:00 2001 From: Kamigen <46357922+Edouard127@users.noreply.github.com> Date: Mon, 8 Jul 2024 19:07:27 -0400 Subject: [PATCH 3/3] Update Loader.kt --- common/src/main/kotlin/com/lambda/core/Loader.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/kotlin/com/lambda/core/Loader.kt b/common/src/main/kotlin/com/lambda/core/Loader.kt index fb4163daa..271143d2c 100644 --- a/common/src/main/kotlin/com/lambda/core/Loader.kt +++ b/common/src/main/kotlin/com/lambda/core/Loader.kt @@ -24,7 +24,7 @@ object Loader { val runtime: String get() = "${(System.currentTimeMillis() - started).toDuration(DurationUnit.MILLISECONDS)}" - val loadables = listOf( + private val loadables = listOf( ModuleRegistry, CommandRegistry, RotationManager,