|
| 1 | +@file:Suppress("UNUSED_PARAMETER") |
| 2 | + |
1 | 3 | package com.mairwunnx.projectessentials.core |
2 | 4 |
|
3 | | -import com.mairwunnx.projectessentials.core.api.v1.commands.CommandProcessor |
4 | | -import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationProcessor |
| 5 | +import com.mairwunnx.projectessentials.core.api.v1.IMCLocalizationMessage |
| 6 | +import com.mairwunnx.projectessentials.core.api.v1.IMCProvidersMessage |
| 7 | +import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI |
5 | 8 | import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI.subscribeOn |
6 | | -import com.mairwunnx.projectessentials.core.api.v1.events.forge.FMLCommonSetupEventData |
7 | 9 | import com.mairwunnx.projectessentials.core.api.v1.events.forge.ForgeEventType |
8 | 10 | import com.mairwunnx.projectessentials.core.api.v1.events.forge.InterModEnqueueEventData |
9 | 11 | import com.mairwunnx.projectessentials.core.api.v1.events.forge.InterModProcessEventData |
10 | | -import com.mairwunnx.projectessentials.core.api.v1.localization.Localization |
11 | 12 | import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationAPI |
12 | | -import com.mairwunnx.projectessentials.core.api.v1.localization.LocalizationProcessor |
13 | | -import com.mairwunnx.projectessentials.core.api.v1.module.ModuleProcessor |
14 | | -import com.mairwunnx.projectessentials.core.api.v1.processor.ProcessorAPI |
| 13 | +import com.mairwunnx.projectessentials.core.api.v1.localizationMarker |
| 14 | +import com.mairwunnx.projectessentials.core.api.v1.module.ModuleAPI |
15 | 15 | import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI |
| 16 | +import com.mairwunnx.projectessentials.core.api.v1.providersMarker |
16 | 17 | import com.mairwunnx.projectessentials.core.impl.ModuleObject |
17 | 18 | import com.mairwunnx.projectessentials.core.impl.commands.BackLocationCommand |
18 | 19 | import com.mairwunnx.projectessentials.core.impl.commands.ConfigureEssentialsCommand |
19 | 20 | import com.mairwunnx.projectessentials.core.impl.configurations.GeneralConfiguration |
20 | 21 | import com.mairwunnx.projectessentials.core.impl.configurations.NativeAliasesConfiguration |
21 | 22 | import com.mairwunnx.projectessentials.core.impl.events.EventBridge |
22 | 23 | import net.minecraftforge.common.MinecraftForge.EVENT_BUS |
| 24 | +import net.minecraftforge.eventbus.api.SubscribeEvent |
| 25 | +import net.minecraftforge.fml.InterModComms |
| 26 | +import net.minecraftforge.fml.ModList |
23 | 27 | import net.minecraftforge.fml.common.Mod |
| 28 | +import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent |
| 29 | +import org.apache.logging.log4j.LogManager |
24 | 30 |
|
25 | 31 | @Suppress("unused") |
26 | 32 | @Mod("project_essentials_core") |
27 | 33 | 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. |
35 | | -
|
36 | | - If you know how resolve it, please, open issue or pull request |
37 | | - and we can discuss it. |
38 | | - */ |
39 | | - private val providers = listOf( |
40 | | - GeneralConfiguration::class.java, |
41 | | - NativeAliasesConfiguration::class.java, |
42 | | - ModuleObject::class.java, |
43 | | - BackLocationCommand::class.java, |
44 | | - ConfigureEssentialsCommand::class.java |
45 | | - ) |
46 | | - |
47 | | - private val processors = listOf( |
48 | | - ConfigurationProcessor, |
49 | | - ModuleProcessor, |
50 | | - LocalizationProcessor, |
51 | | - CommandProcessor |
52 | | - ) |
| 34 | + private val logger = LogManager.getLogger() |
53 | 35 |
|
54 | 36 | init { |
55 | 37 | EventBridge.initialize() |
56 | | - providers.forEach(ProviderAPI::addProvider) |
57 | | - subscribeEvents() |
58 | 38 | EVENT_BUS.register(this) |
| 39 | + subscribeEvents() |
59 | 40 | } |
60 | 41 |
|
61 | | - private fun subscribeEvents() { |
62 | | - subscribeOn<FMLCommonSetupEventData>( |
63 | | - ForgeEventType.SetupEvent |
64 | | - ) { |
65 | | - applyLocalization() |
66 | | - processors.forEach(ProcessorAPI::register) |
67 | | - } |
| 42 | + @SubscribeEvent |
| 43 | + fun onServerPreStart(event: FMLServerAboutToStartEvent) { |
| 44 | + ConfigurationAPI.loadAll() |
| 45 | + ModuleAPI.initializeOrdered() |
| 46 | + } |
68 | 47 |
|
| 48 | + private fun subscribeEvents() { |
69 | 49 | subscribeOn<InterModEnqueueEventData>( |
70 | 50 | ForgeEventType.EnqueueIMCEvent |
71 | 51 | ) { |
72 | | - ProcessorAPI.processProcessors() |
| 52 | + sendLocalizationRequest() |
| 53 | + sendProvidersRequest() |
73 | 54 | } |
74 | 55 |
|
75 | 56 | subscribeOn<InterModProcessEventData>( |
76 | 57 | ForgeEventType.ProcessIMCEvent |
| 58 | + ) { event -> |
| 59 | + processLocalizationRequest(event) |
| 60 | + processProvidersRequest(event) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private fun sendLocalizationRequest() { |
| 65 | + InterModComms.sendTo( |
| 66 | + "project_essentials_core", |
| 67 | + IMCLocalizationMessage |
77 | 68 | ) { |
78 | | - ProcessorAPI.postProcessProcessors() |
| 69 | + fun() = mutableListOf( |
| 70 | + "/assets/projectessentialscore/lang/en_us.json", |
| 71 | + "/assets/projectessentialscore/lang/ru_ru.json", |
| 72 | + "/assets/projectessentialscore/lang/zh_cn.json" |
| 73 | + ) |
79 | 74 | } |
80 | 75 | } |
81 | 76 |
|
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 |
| 77 | + private fun sendProvidersRequest() { |
| 78 | + InterModComms.sendTo( |
| 79 | + "project_essentials_core", |
| 80 | + IMCProvidersMessage |
| 81 | + ) { |
| 82 | + fun() = listOf( |
| 83 | + GeneralConfiguration::class.java, |
| 84 | + NativeAliasesConfiguration::class.java, |
| 85 | + ModuleObject::class.java, |
| 86 | + BackLocationCommand::class.java, |
| 87 | + ConfigureEssentialsCommand::class.java |
90 | 88 | ) |
91 | | - ) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private fun processLocalizationRequest(event: InterModProcessEventData) { |
| 93 | + event.event.getIMCStream { method -> |
| 94 | + method == IMCLocalizationMessage |
| 95 | + }.also { stream -> |
| 96 | + stream.forEach { message -> |
| 97 | + val clazz = ModList.get().getModContainerById(message.modId).get().mod.javaClass |
| 98 | + message.getMessageSupplier<() -> List<String>>().get().also { |
| 99 | + logger.debug( |
| 100 | + localizationMarker, "Localization got from ${message.senderModId}" |
| 101 | + ).run { LocalizationAPI.apply(clazz, it) } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private fun processProvidersRequest(event: InterModProcessEventData) { |
| 108 | + event.event.getIMCStream { method -> |
| 109 | + method == IMCProvidersMessage |
| 110 | + }.also { stream -> |
| 111 | + stream.forEach { message -> |
| 112 | + message.getMessageSupplier<() -> List<Class<out Any>>>().get().also { |
| 113 | + logger.debug( |
| 114 | + providersMarker, "Providers got from ${message.senderModId}" |
| 115 | + ).run { it().forEach(ProviderAPI::addProvider) } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
92 | 119 | } |
93 | 120 | } |
0 commit comments