Skip to content

Commit 820b9f8

Browse files
committed
Processing localization messaging added.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent af4ab3b commit 820b9f8

File tree

1 file changed

+36
-26
lines changed
  • src/main/kotlin/com/mairwunnx/projectessentials/core

1 file changed

+36
-26
lines changed
Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.mairwunnx.projectessentials.core
22

3+
import com.mairwunnx.projectessentials.core.api.v1.IMCLocalizationMessage
34
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandProcessor
45
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationProcessor
56
import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI.subscribeOn
67
import com.mairwunnx.projectessentials.core.api.v1.events.forge.FMLCommonSetupEventData
78
import com.mairwunnx.projectessentials.core.api.v1.events.forge.ForgeEventType
89
import com.mairwunnx.projectessentials.core.api.v1.events.forge.InterModEnqueueEventData
910
import com.mairwunnx.projectessentials.core.api.v1.events.forge.InterModProcessEventData
10-
import com.mairwunnx.projectessentials.core.api.v1.localization.Localization
1111
import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationAPI
12-
import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationProcessor
12+
import com.mairwunnx.projectessentials.core.api.v1.localizationMarker
1313
import com.mairwunnx.projectessentials.core.api.v1.module.ModuleProcessor
1414
import com.mairwunnx.projectessentials.core.api.v1.processor.ProcessorAPI
1515
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
@@ -20,22 +20,16 @@ import com.mairwunnx.projectessentials.core.impl.configurations.GeneralConfigura
2020
import com.mairwunnx.projectessentials.core.impl.configurations.NativeAliasesConfiguration
2121
import com.mairwunnx.projectessentials.core.impl.events.EventBridge
2222
import net.minecraftforge.common.MinecraftForge.EVENT_BUS
23+
import net.minecraftforge.fml.InterModComms
24+
import net.minecraftforge.fml.ModList
2325
import net.minecraftforge.fml.common.Mod
26+
import org.apache.logging.log4j.LogManager
2427

2528
@Suppress("unused")
2629
@Mod("project_essentials_core")
2730
internal class EntryPoint {
28-
/*
29-
Sorry for hardcoded classes in this list.
30-
I tried getting this classes with library https://github.com/matfax/klassindex
31-
but it incorrectly works with multi-module projects,
32-
for example if core will using klassindex library, then
33-
other modules will incorrectly finding annotated classes,
34-
because same class path of generated object with class references.
31+
private val logger = LogManager.getLogger()
3532

36-
If you know how resolve it, please, open issue or pull request
37-
and we can discuss it.
38-
*/
3933
private val providers = listOf(
4034
GeneralConfiguration::class.java,
4135
NativeAliasesConfiguration::class.java,
@@ -47,47 +41,63 @@ internal class EntryPoint {
4741
private val processors = listOf(
4842
ConfigurationProcessor,
4943
ModuleProcessor,
50-
LocalizationProcessor,
5144
CommandProcessor
5245
)
5346

5447
init {
5548
EventBridge.initialize()
56-
providers.forEach(ProviderAPI::addProvider)
57-
subscribeEvents()
5849
EVENT_BUS.register(this)
50+
subscribeEvents()
51+
providers.forEach(ProviderAPI::addProvider)
5952
}
6053

6154
private fun subscribeEvents() {
6255
subscribeOn<FMLCommonSetupEventData>(
6356
ForgeEventType.SetupEvent
6457
) {
65-
applyLocalization()
6658
processors.forEach(ProcessorAPI::register)
6759
}
6860

6961
subscribeOn<InterModEnqueueEventData>(
7062
ForgeEventType.EnqueueIMCEvent
7163
) {
64+
sendLocalizationRequest()
7265
ProcessorAPI.processProcessors()
7366
}
7467

7568
subscribeOn<InterModProcessEventData>(
7669
ForgeEventType.ProcessIMCEvent
77-
) {
70+
) { event ->
71+
processLocalizationRequest(event)
7872
ProcessorAPI.postProcessProcessors()
7973
}
8074
}
8175

82-
private fun applyLocalization() {
83-
LocalizationAPI.apply(
84-
Localization(
85-
mutableListOf(
86-
"/assets/projectessentialscore/lang/en_us.json",
87-
"/assets/projectessentialscore/lang/ru_ru.json",
88-
"/assets/projectessentialscore/lang/zh_cn.json"
89-
), "core", EntryPoint::class.java
76+
private fun sendLocalizationRequest() {
77+
InterModComms.sendTo(
78+
"project_essentials_core",
79+
IMCLocalizationMessage
80+
) {
81+
fun() = mutableListOf(
82+
"/assets/projectessentialscore/lang/en_us.json",
83+
"/assets/projectessentialscore/lang/ru_ru.json",
84+
"/assets/projectessentialscore/lang/zh_cn.json"
9085
)
91-
)
86+
}
87+
}
88+
89+
private fun processLocalizationRequest(event: InterModProcessEventData) {
90+
event.event.getIMCStream { method ->
91+
method == IMCLocalizationMessage
92+
}.also { stream ->
93+
stream.forEach { message ->
94+
val clazz = ModList.get().getModContainerById(message.modId).get().mod.javaClass
95+
message.getMessageSupplier<() -> List<String>>().get().also {
96+
logger.debug(
97+
localizationMarker, "Localization got from ${message.senderModId}"
98+
).run { LocalizationAPI.apply(clazz, it) }
99+
}
100+
}
101+
}
92102
}
93103
}

0 commit comments

Comments
 (0)