Skip to content

Commit 00afff5

Browse files
committed
Synced with 1.14.4.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent 187aa88 commit 00afff5

27 files changed

+351
-787
lines changed

src/main/java/com/mairwunnx/projectessentials/core/impl/events/EventBridge.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI;
44
import com.mairwunnx.projectessentials.core.api.v1.events.forge.*;
5-
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
6-
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
7-
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
8-
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
5+
import net.minecraftforge.fml.event.lifecycle.*;
96
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
107

118
public class EventBridge {
@@ -14,6 +11,7 @@ public static void initialize() {
1411
FMLJavaModLoadingContext.get().getModEventBus().addListener(EventBridge::enqueueIMC);
1512
FMLJavaModLoadingContext.get().getModEventBus().addListener(EventBridge::processIMC);
1613
FMLJavaModLoadingContext.get().getModEventBus().addListener(EventBridge::doClientStuff);
14+
FMLJavaModLoadingContext.get().getModEventBus().addListener(EventBridge::complete);
1715
}
1816

1917
public static void setup(final FMLCommonSetupEvent event) {
@@ -39,4 +37,10 @@ public static void processIMC(final InterModProcessEvent event) {
3937
ForgeEventType.ProcessIMCEvent, new InterModProcessEventData(event)
4038
);
4139
}
40+
41+
public static void complete(final FMLLoadCompleteEvent event) {
42+
ModuleEventAPI.INSTANCE.fire(
43+
ForgeEventType.LoadComplete, new LoadCompleteEventData(event)
44+
);
45+
}
4246
}
Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,120 @@
1+
@file:Suppress("UNUSED_PARAMETER")
2+
13
package com.mairwunnx.projectessentials.core
24

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
58
import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI.subscribeOn
6-
import com.mairwunnx.projectessentials.core.api.v1.events.forge.FMLCommonSetupEventData
79
import com.mairwunnx.projectessentials.core.api.v1.events.forge.ForgeEventType
810
import com.mairwunnx.projectessentials.core.api.v1.events.forge.InterModEnqueueEventData
911
import com.mairwunnx.projectessentials.core.api.v1.events.forge.InterModProcessEventData
10-
import com.mairwunnx.projectessentials.core.api.v1.localization.Localization
1112
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
1515
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
16+
import com.mairwunnx.projectessentials.core.api.v1.providersMarker
1617
import com.mairwunnx.projectessentials.core.impl.ModuleObject
1718
import com.mairwunnx.projectessentials.core.impl.commands.BackLocationCommand
1819
import com.mairwunnx.projectessentials.core.impl.commands.ConfigureEssentialsCommand
1920
import com.mairwunnx.projectessentials.core.impl.configurations.GeneralConfiguration
2021
import com.mairwunnx.projectessentials.core.impl.configurations.NativeAliasesConfiguration
2122
import com.mairwunnx.projectessentials.core.impl.events.EventBridge
2223
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
2327
import net.minecraftforge.fml.common.Mod
28+
import net.minecraftforge.fml.event.server.FMLServerAboutToStartEvent
29+
import org.apache.logging.log4j.LogManager
2430

2531
@Suppress("unused")
2632
@Mod("project_essentials_core")
2733
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()
5335

5436
init {
5537
EventBridge.initialize()
56-
providers.forEach(ProviderAPI::addProvider)
57-
subscribeEvents()
5838
EVENT_BUS.register(this)
39+
subscribeEvents()
5940
}
6041

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+
}
6847

48+
private fun subscribeEvents() {
6949
subscribeOn<InterModEnqueueEventData>(
7050
ForgeEventType.EnqueueIMCEvent
7151
) {
72-
ProcessorAPI.processProcessors()
52+
sendLocalizationRequest()
53+
sendProvidersRequest()
7354
}
7455

7556
subscribeOn<InterModProcessEventData>(
7657
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
7768
) {
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+
)
7974
}
8075
}
8176

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
9088
)
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+
}
92119
}
93120
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,6 @@ const val SETTING_DISABLE_PORTAL_SPAWNING = "disable-portal-spawning"
5454
const val SETTING_WEATHER_COMMAND_DEFAULT_DURATION = "weather-command-default-duration"
5555
const val SETTING_DEOP_COMMAND_REMOVE_OP_PERM = "deop-command-remove-op-perm"
5656
const val SETTING_LIST_MAX_ELEMENTS_IN_PAGE = "list-max-elements-in-page"
57+
58+
const val IMCLocalizationMessage = "localization-request"
59+
const val IMCProvidersMessage = "providers-request"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ package com.mairwunnx.projectessentials.core.api.v1
33
import org.apache.logging.log4j.MarkerManager
44

55
val localizationMarker = MarkerManager.getMarker("L10N")
6+
val providersMarker = MarkerManager.getMarker("PROVIDERS")

src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/commands/CommandAPI.kt

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
package com.mairwunnx.projectessentials.core.api.v1.commands
44

5+
import com.mairwunnx.projectessentials.core.api.v1.events.ModuleEventAPI
6+
import com.mairwunnx.projectessentials.core.api.v1.events.internal.CommandEventData
7+
import com.mairwunnx.projectessentials.core.api.v1.events.internal.ModuleCoreEventType
8+
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderAPI
9+
import com.mairwunnx.projectessentials.core.api.v1.providers.ProviderType
510
import com.mojang.brigadier.CommandDispatcher
611
import com.mojang.brigadier.arguments.BoolArgumentType
712
import com.mojang.brigadier.arguments.IntegerArgumentType
@@ -11,12 +16,16 @@ import net.minecraft.command.CommandSource
1116
import net.minecraft.command.arguments.EntityArgument
1217
import net.minecraft.entity.Entity
1318
import net.minecraft.entity.player.ServerPlayerEntity
19+
import org.apache.logging.log4j.LogManager
1420

1521
/**
1622
* Class for interacting with command api.
1723
* @since 2.0.0-SNAPSHOT.1.
1824
*/
1925
object CommandAPI {
26+
private val logger = LogManager.getLogger()
27+
private var commands = listOf<ICommand>()
28+
2029
private lateinit var dispatcher: CommandDispatcher<CommandSource>
2130
private var dispatcherAssigned = false
2231

@@ -48,7 +57,7 @@ object CommandAPI {
4857
* @return all installed and checked commands.
4958
* @since 2.0.0-SNAPSHOT.1.
5059
*/
51-
fun getAllCommands() = CommandProcessor.getCommands()
60+
fun getCommands() = commands
5261

5362
/**
5463
* Remove already registered command with
@@ -248,4 +257,27 @@ object CommandAPI {
248257
context: CommandContext<CommandSource>,
249258
argumentName: String
250259
): MutableCollection<ServerPlayerEntity> = EntityArgument.getPlayers(context, argumentName)
260+
261+
internal fun registerAll() {
262+
ProviderAPI.getProvidersByType(ProviderType.Command).forEach {
263+
val clazz = it.getDeclaredField("INSTANCE").get(null) as ICommand
264+
ModuleEventAPI.fire(
265+
ModuleCoreEventType.OnCommandClassProcessing, CommandEventData(clazz)
266+
)
267+
logger.debug(
268+
"Command taken! ${clazz.javaClass.simpleName}, name: ${clazz.name}, aliases: ${clazz.aliases}"
269+
)
270+
commands = commands + clazz
271+
register(clazz)
272+
ModuleEventAPI.fire(
273+
ModuleCoreEventType.OnCommandClassProcessed, CommandEventData(clazz)
274+
)
275+
}
276+
}
277+
278+
private fun register(command: ICommand) =
279+
logger.info("Starting registering command ${command.name}").also {
280+
command.initialize()
281+
command.register(getDispatcher())
282+
}
251283
}

src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/commands/CommandProcessor.kt

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

src/main/kotlin/com/mairwunnx/projectessentials/core/api/v1/commands/arguments/StringArrayArgument.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@file:Suppress("DEPRECATION")
2+
13
package com.mairwunnx.projectessentials.core.api.v1.commands.arguments
24

35
import com.mojang.brigadier.StringReader

0 commit comments

Comments
 (0)