Skip to content

Commit a5e417c

Browse files
committed
Added lifecycle on forge-like
1 parent 33177b7 commit a5e417c

File tree

20 files changed

+225
-25
lines changed

20 files changed

+225
-25
lines changed

common/src/main/kotlin/com/lambda/command/CommandRegistry.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.lambda.command
22

33
import com.lambda.config.Configurable
44
import com.lambda.config.configurations.LambdaConfig
5-
import com.lambda.core.Loadable
5+
import com.lambda.core.lifecycle.Loadable
66
import org.reflections.Reflections
77
import org.reflections.scanners.Scanners
88
import org.reflections.util.ConfigurationBuilder

common/src/main/kotlin/com/lambda/core/Loadable.kt

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

common/src/main/kotlin/com/lambda/core/PingManager.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.core
22

3+
import com.lambda.core.lifecycle.Loadable
34
import com.lambda.event.events.PacketEvent
45
import com.lambda.event.events.TickEvent
56
import com.lambda.event.listener.SafeListener.Companion.listener
@@ -26,4 +27,4 @@ object PingManager : Loadable {
2627
pings.add(Util.getMeasuringTimeMs() - event.packet.startTime)
2728
}
2829
}
29-
}
30+
}

common/src/main/kotlin/com/lambda/core/TimerManager.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lambda.core
22

3+
import com.lambda.core.lifecycle.Loadable
34
import com.lambda.event.EventFlow.post
45
import com.lambda.event.events.ClientEvent
56
import com.lambda.event.events.TickEvent
@@ -16,4 +17,4 @@ object TimerManager : Loadable {
1617
}
1718
}
1819
}
19-
}
20+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.lambda.core.lifecycle
2+
3+
import com.lambda.core.lifecycle.Lifecycle.Complete
4+
5+
internal annotation class ForgeLikeOnly
6+
7+
/**
8+
* Represents the lifecycle of the mod loader process for Forge and NeoForge mods.
9+
* Managers ran on Fabric with one of these lifecycles will be ignored and executed on the [Complete]
10+
* lifecycle instead.
11+
*/
12+
@ForgeLikeOnly
13+
sealed class Lifecycle {
14+
/**
15+
* Fired when the mod events are registered. Not to confuse with Lambda's event bus.
16+
*/
17+
interface Initialization : Loadable
18+
19+
/**
20+
* Fired right after the mod constructor is called
21+
*/
22+
interface Construction : Loadable
23+
24+
/**
25+
* Fired for each individual Minecraft registry.
26+
* As long as you are still in the lifecycle scope you are allowed
27+
* to add entries without any issues.
28+
*/
29+
interface Registry : Loadable
30+
31+
/**
32+
* Fired during the early initialisation of Minecraft.
33+
*/
34+
interface Common : Loadable
35+
36+
/**
37+
* Sided-setup on their respective side, however only `FMLClientSetupEvent` will be fired
38+
* because Lambda is not a server-sided mod.
39+
*/
40+
interface SideSetup : Loadable
41+
42+
/**
43+
* Fired when mods can send messages to each other for cross-mod compatibility.
44+
* This is the last event before the mod is considered fully loaded.
45+
*/
46+
interface InterModCommunication : Loadable
47+
48+
/**
49+
* Fired when the mod is fully loaded and ready to be used.
50+
*/
51+
interface Complete : Loadable
52+
}
53+
54+
/**
55+
* Represents a loadable object.
56+
*/
57+
interface Loadable {
58+
fun load() = this::class.simpleName?.let { "Loaded $it" } ?: "Loaded"
59+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.lambda.core.registry
2+
3+
import net.minecraft.registry.Registry
4+
import net.minecraft.registry.RegistryKey
5+
import net.minecraft.util.Identifier
6+
7+
8+
object RegistryController {
9+
private val REGISTRIES = HashMap<RegistryKey<out Registry<*>?>, MutableList<RegistryHolder<*>>>()
10+
private var frozen = false
11+
12+
fun <T> register(registry: Registry<T>, id: Identifier, value: T): RegistryHolder<T> {
13+
check(!frozen) { "Cannot register, RegistryController is frozen." }
14+
val holder = RegistryHolder(id, value)
15+
16+
REGISTRIES.computeIfAbsent(registry.key) { mutableListOf(holder) }
17+
return holder
18+
}
19+
20+
/**
21+
* Loads the temporary registry holders into the actual registry.
22+
* This should be called after all registry holders have been created.
23+
* It will also freeze the controller to prevent further registrations.
24+
*/
25+
fun register(resourceKey: RegistryKey<out Registry<*>?>, registry: RegistryWrapper<*>) {
26+
frozen = true
27+
REGISTRIES[resourceKey]?.forEach { it.handleRegister(registry) }
28+
REGISTRIES.remove(resourceKey)
29+
}
30+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.lambda.core.registry
2+
3+
import net.minecraft.registry.entry.RegistryEntry
4+
import net.minecraft.util.Identifier
5+
import java.util.function.Supplier
6+
7+
8+
class RegistryHolder<T> internal constructor(
9+
val id: Identifier,
10+
var value: T,
11+
) : Supplier<T> {
12+
private var holder: RegistryEntry<T>? = null
13+
14+
override fun get(): T {
15+
with(holder) {
16+
checkNotNull(this) { "RegistryHolder not populated" }
17+
18+
return this.value()
19+
}
20+
}
21+
22+
fun handleRegister(registry: RegistryWrapper<*>) {
23+
this.holder = registry.registerForHolder(id, value)
24+
}
25+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.lambda.core.registry
2+
3+
import net.minecraft.registry.entry.RegistryEntry
4+
import net.minecraft.util.Identifier
5+
6+
interface RegistryWrapper<T> {
7+
fun <T> registerForHolder(id: Identifier?, value: T): RegistryEntry<T>
8+
}

common/src/main/kotlin/com/lambda/friend/FriendRegistry.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.lambda.friend
22

33
import com.lambda.config.Configurable
44
import com.lambda.config.configurations.FriendConfig
5-
import com.lambda.core.Loadable
5+
import com.lambda.core.lifecycle.Loadable
66
import com.mojang.authlib.GameProfile
77

88
object FriendRegistry : Configurable(FriendConfig), Loadable {

common/src/main/kotlin/com/lambda/graphics/renderer/gui/font/LambdaEmoji.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.lambda.graphics.renderer.gui.font
22

3-
import com.lambda.core.Loadable
3+
import com.lambda.core.lifecycle.Loadable
44
import com.lambda.graphics.renderer.gui.font.glyph.EmojiGlyphs
55

66
enum class LambdaEmoji(private val zipUrl: String) {

0 commit comments

Comments
 (0)