1+ @file:Suppress(" MemberVisibilityCanBePrivate" )
2+
13package com.mairwunnx.projectessentials.core.api.v1.providers
24
35import com.mairwunnx.projectessentials.core.api.v1.commands.ICommand
46import com.mairwunnx.projectessentials.core.api.v1.configuration.IConfiguration
57import com.mairwunnx.projectessentials.core.api.v1.module.IModule
8+ import com.mairwunnx.projectessentials.core.api.v1.providersMarker
69import org.apache.logging.log4j.LogManager
7- import org.apache.logging.log4j.MarkerManager
810
911/* *
1012 * Provider API class. If you build new module
1113 * for project essentials then you must use this class.
1214 *
13- * You can register providers (configurations, commands and module).
14- *
15- * You can register manually (just addProvider(<provider class>)) and you can
16- * use ClassIndex library, code example (kotlin language) (it scans all
17- * configuration classes with annotations):
18- *
19- * ```kotlin
20- * ClassIndex.getAnnotated(Configuration::class.java).forEach {
21- * ProviderAPI.addProvider(it)
22- * }
23- * ```
24- *
25- * BTW! `addProvider(...)` you must call only in init block of your mod.
26- * (or if you use java as language then in constructor)
27- *
28- * ["ClassIndex library"](https://github.com/atteo/classindex)
29- *
3015 * @since 2.0.0-SNAPSHOT.1.
3116 */
3217object ProviderAPI {
3318 private val logger = LogManager .getLogger()
34- private val marker = MarkerManager .Log4jMarker (" PROVIDER" )
3519 private val providers = mutableMapOf<ProviderType , MutableList <Class <* >>>()
3620
3721 /* *
3822 * Adds target provider. (provider type will determine automatically)
3923 *
40- * **Call only in module initialize block or constructor!!**
4124 * @param clazz provider class.
4225 * @since 2.0.0-SNAPSHOT.1.
4326 */
44- @Synchronized
4527 fun addProvider (clazz : Class <* >) {
28+ fun out (type : String ) = logger.debug(
29+ providersMarker, " Provider class found: Type: `$type `, Class: `${clazz.simpleName} `"
30+ )
31+
4632 when {
4733 IConfiguration ::class .java.isAssignableFrom(clazz) -> {
48- addProvider(ProviderType .CONFIGURATION , clazz).run {
49- logger.debug(
50- marker,
51- " Provider class founded: Type: `Configuration`, Class: `${clazz.simpleName} `"
52- )
53- }
34+ addProvider(ProviderType .Configuration , clazz).run { out (" Configuration" ) }
5435 }
5536 IModule ::class .java.isAssignableFrom(clazz) -> {
56- addProvider(ProviderType .MODULE , clazz).run {
57- logger.debug(
58- marker,
59- " Provider class founded: Type: `Module`, Class: `${clazz.simpleName} `"
60- )
61- }
37+ addProvider(ProviderType .Module , clazz).run { out (" Module" ) }
6238 }
6339 ICommand ::class .java.isAssignableFrom(clazz) -> {
64- addProvider(ProviderType .COMMAND , clazz).run {
65- logger.debug(
66- marker,
67- " Provider class founded: Type: `Command`, Class: `${clazz.simpleName} `"
68- )
69- }
40+ addProvider(ProviderType .Command , clazz).run { out (" Command" ) }
7041 }
7142 else -> logger.warn(
72- marker ,
43+ providersMarker ,
7344 " Incorrect provider class found! (skipped to load): Class: `${clazz.simpleName} `"
7445 )
7546 }
@@ -78,7 +49,6 @@ object ProviderAPI {
7849 /* *
7950 * Adds target provider with specified type.
8051 *
81- * **Call only in module initialize block or constructor!!**
8252 * @param type provider type.
8353 * @param clazz provider class.
8454 * @since 2.0.0-SNAPSHOT.1.
@@ -91,12 +61,12 @@ object ProviderAPI {
9161 * @return all added providers.
9262 * @since 2.0.0-SNAPSHOT.1.
9363 */
94- fun getAllProviders () = providers
64+ fun getProviders () = providers
9565
9666 /* *
9767 * @param type provider type to return.
9868 * @return all specified for type providers.
9969 * @since 2.0.0-SNAPSHOT.1.
10070 */
101- fun getProvidersByType (type : ProviderType ) = getAllProviders ()[type] ? : mutableListOf ()
71+ fun getProvidersByType (type : ProviderType ) = getProviders ()[type] ? : mutableListOf ()
10272}
0 commit comments