Skip to content

Commit 910d559

Browse files
committed
ModuleAPI.kt refactor.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 13d2060 commit 910d559

File tree

1 file changed

+42
-3
lines changed
  • src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/module

1 file changed

+42
-3
lines changed

src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/module/ModuleAPI.kt

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
package com.mairwunnx.projectessentials.core.api.v1.module
22

3+
import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI
4+
import com.mairwunnx.projectessentials.core.api.v1.events.internal.ModuleCoreEventType
5+
import com.mairwunnx.projectessentials.core.api.v1.events.internal.ModuleEventData
6+
import com.mairwunnx.projectessentials.core.api.v1.extensions.empty
7+
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
8+
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderType
9+
import net.minecraftforge.fml.ModList
310
import net.minecraftforge.fml.common.Mod
11+
import org.apache.logging.log4j.LogManager
412

513
/**
614
* Class for interacting with other modules.
715
* @since 2.0.0-SNAPSHOT.1.
816
*/
917
@Suppress("unused", "MemberVisibilityCanBePrivate")
1018
object ModuleAPI {
19+
private val logger = LogManager.getLogger()
20+
private var modules = listOf<IModule>()
21+
1122
/**
1223
* @return all installed and checked modules.
1324
* @since 2.0.0-SNAPSHOT.1.
1425
*/
15-
fun getAllModules() = ModuleProcessor.getModules()
26+
fun getModules() = modules
1627

1728
/**
1829
* @return module mod id what declared in `@Mod` annotation.
@@ -31,7 +42,7 @@ object ModuleAPI {
3142
* @since 2.0.0-SNAPSHOT.1.
3243
*/
3344
fun getModuleByName(name: String) =
34-
getAllModules().find { it.name.toLowerCase() == name.toLowerCase() }?.let {
45+
getModules().asSequence().find { it.name.toLowerCase() == name.toLowerCase() }?.let {
3546
return@let it
3647
} ?: throw ModuleNotFoundException(
3748
"Module with name $name not found and not processed."
@@ -42,7 +53,35 @@ object ModuleAPI {
4253
* @return true if module existing or installed.
4354
* @since 2.0.0-SNAPSHOT.1.
4455
*/
45-
fun isModuleExist(module: String) = ModuleProcessor.getModules().find {
56+
fun isModuleExist(module: String) = getModules().asSequence().find {
4657
it.name.toLowerCase() == module.toLowerCase()
4758
}.let { return@let it != null }
59+
60+
internal fun initializeOrdered() {
61+
ProviderAPI.getProvidersByType(ProviderType.Module).forEach {
62+
val clazz = if (it.isAnnotationPresent(Mod::class.java)) {
63+
ModList.get().getModObjectById<IModule>(
64+
it.getAnnotation(Mod::class.java)?.value ?: String.empty
65+
).get()
66+
} else it.newInstance() as IModule
67+
ModuleEventAPI.fire(ModuleCoreEventType.OnModuleClassProcessing, ModuleEventData(clazz))
68+
processIndexes(clazz.loadIndex)
69+
logger.debug(
70+
"Project Essentials module found: ${it.simpleName}, name: ${clazz.name}, version: ${clazz.version}"
71+
)
72+
modules = modules + clazz
73+
ModuleEventAPI.fire(ModuleCoreEventType.OnModuleClassProcessed, ModuleEventData(clazz))
74+
}.run { initialize() }
75+
}
76+
77+
private fun initialize() =
78+
modules.asSequence().sortedWith(compareBy { by -> by.loadIndex }).forEach { module ->
79+
logger.info("Starting initializing module ${module.name}").also { module.init() }
80+
}
81+
82+
private fun processIndexes(index: Int) {
83+
modules.asSequence().find { it.loadIndex == index }?.let {
84+
throw ModuleIndexDuplicateException("Module with same load index $index already processed.")
85+
}
86+
}
4887
}

0 commit comments

Comments
 (0)