diff options
author | thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> | 2020-03-15 15:29:12 -0700 |
---|---|---|
committer | thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> | 2020-03-15 15:29:12 -0700 |
commit | 9deb20a755528d1370cab429e6849cfc607780db (patch) | |
tree | a37fb494f0b35a2444fca66654db8d1f0ca60395 /src | |
parent | e881f3b56b904c16a488579380d5dceae828c1b2 (diff) | |
download | KotlinForForge-9deb20a755528d1370cab429e6849cfc607780db.tar.gz KotlinForForge-9deb20a755528d1370cab429e6849cfc607780db.tar.bz2 KotlinForForge-9deb20a755528d1370cab429e6849cfc607780db.zip |
Update 1.1.0
Diffstat (limited to 'src')
7 files changed, 51 insertions, 45 deletions
diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt index eae9ef5..fa43f57 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/AutoKotlinEventBusSubscriber.kt @@ -16,7 +16,7 @@ import java.util.stream.Collectors /** * Handles [net.minecraftforge.fml.common.Mod.EventBusSubscriber] annotations for object declarations. */ -object AutoKotlinEventBusSubscriber { +public object AutoKotlinEventBusSubscriber { private val EVENT_BUS_SUBSCRIBER: Type = Type.getType(Mod.EventBusSubscriber::class.java) /** @@ -34,7 +34,7 @@ object AutoKotlinEventBusSubscriber { * } * } */ - fun inject(mod: ModContainer, scanData: ModFileScanData, classLoader: ClassLoader) { + public fun inject(mod: ModContainer, scanData: ModFileScanData, classLoader: ClassLoader) { logger.debug(Logging.LOADING, "Attempting to inject @EventBusSubscriber kotlin objects in to the event bus for ${mod.modId}") val data: ArrayList<ModFileScanData.AnnotationData> = scanData.annotations.stream() .filter { annotationData -> diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinForForge.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinForForge.kt index aef44e5..e5ec289 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinForForge.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinForForge.kt @@ -6,4 +6,4 @@ import net.minecraftforge.fml.common.Mod * Set 'modLoader' in mods.toml to "kotlinforforge" and loaderVersion to "[1,)". */ @Mod("kotlinforforge") -object KotlinForForge
\ No newline at end of file +public object KotlinForForge
\ No newline at end of file diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinLanguageProvider.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinLanguageProvider.kt index a98fda7..61705ef 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinLanguageProvider.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinLanguageProvider.kt @@ -10,7 +10,7 @@ import java.util.function.Consumer import java.util.function.Supplier import java.util.stream.Collectors -class KotlinLanguageProvider : FMLJavaModLanguageProvider() { +public class KotlinLanguageProvider : FMLJavaModLanguageProvider() { override fun getFileVisitor(): Consumer<ModFileScanData> { return Consumer { scanResult -> val target = scanResult.annotations.stream() @@ -26,7 +26,7 @@ class KotlinLanguageProvider : FMLJavaModLanguageProvider() { override fun name(): String = "kotlinforforge" - class KotlinModTarget constructor(private val className: String, val modId: String) : IModLanguageProvider.IModLanguageLoader { + public class KotlinModTarget constructor(private val className: String, val modId: String) : IModLanguageProvider.IModLanguageLoader { override fun <T> loadMod(info: IModInfo, modClassLoader: ClassLoader, modFileScanResults: ModFileScanData): T { val ktContainer = Class.forName("thedarkcolour.kotlinforforge.KotlinModContainer", true, Thread.currentThread().contextClassLoader) logger.debug(Logging.LOADING, "Loading KotlinModContainer from classloader ${Thread.currentThread().contextClassLoader} - got ${ktContainer.classLoader}}") diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModContainer.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModContainer.kt index 7c8101d..0ea150f 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModContainer.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModContainer.kt @@ -8,30 +8,24 @@ import net.minecraftforge.eventbus.api.IEventListener import net.minecraftforge.fml.* import net.minecraftforge.forgespi.language.IModInfo import net.minecraftforge.forgespi.language.ModFileScanData -import org.apache.logging.log4j.LogManager import java.util.* import java.util.function.Consumer import java.util.function.Supplier /** * Functions as [net.minecraftforge.fml.javafmlmod.FMLModContainer] for Kotlin - * - * */ -class KotlinModContainer(private val info: IModInfo, private val className: String, private val loader: ClassLoader, private val scanData: ModFileScanData) : ModContainer(info) { - //private val modClass: Class<*> - /** Use a separate logger because KotlinForForge.logger isn't initialized yet */ - private val logger = LogManager.getLogger() +public class KotlinModContainer(private val info: IModInfo, private val className: String, private val classLoader: ClassLoader, private val scanData: ModFileScanData) : ModContainer(info) { private lateinit var modInstance: Any - val eventBus: IEventBus + public val eventBus: IEventBus init { - logger.debug(Logging.LOADING, "Creating KotlinModContainer instance for {} with classLoader {} & {}", className, loader, javaClass.classLoader) + logger.debug(Logging.LOADING, "Creating KotlinModContainer instance for {} with classLoader {} & {}", className, classLoader, javaClass.classLoader) triggerMap[ModLoadingStage.CONSTRUCT] = dummy().andThen(::constructMod).andThen(::afterEvent) triggerMap[ModLoadingStage.CREATE_REGISTRIES] = dummy().andThen(::fireEvent).andThen(::afterEvent) triggerMap[ModLoadingStage.LOAD_REGISTRIES] = dummy().andThen(::fireEvent).andThen(::afterEvent) triggerMap[ModLoadingStage.COMMON_SETUP] = dummy().andThen(::fireEvent).andThen(::afterEvent) - triggerMap[ModLoadingStage.SIDED_SETUP] = dummy().andThen(::fireEvent).andThen (::afterEvent) + triggerMap[ModLoadingStage.SIDED_SETUP] = dummy().andThen(::fireEvent).andThen(::afterEvent) triggerMap[ModLoadingStage.ENQUEUE_IMC] = dummy().andThen(::fireEvent).andThen(::afterEvent) triggerMap[ModLoadingStage.PROCESS_IMC] = dummy().andThen(::fireEvent).andThen(::afterEvent) triggerMap[ModLoadingStage.COMPLETE] = dummy().andThen(::fireEvent).andThen(::afterEvent) @@ -50,15 +44,14 @@ class KotlinModContainer(private val info: IModInfo, private val className: Stri private fun fireEvent(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) { val event = lifecycleEvent.getOrBuildEvent(this) - logger.debug(Logging.LOADING, "Firing event for modid ${getModId()} : $event") + logger.debug(Logging.LOADING, "Firing event for modid $modId : $event") try { eventBus.post(event) - logger.debug(Logging.LOADING, "Fired event for modid ${getModId()} : $event") - - } catch (e: Throwable) { + logger.debug(Logging.LOADING, "Fired event for modid $modId : $event") + } catch (throwable: Throwable) { logger.error(Logging.LOADING,"An error occurred while dispatching event ${lifecycleEvent.fromStage()} to $modId") - throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "fml.modloading.errorduringevent", e) + throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "fml.modloading.errorduringevent", throwable) } } @@ -71,20 +64,20 @@ class KotlinModContainer(private val info: IModInfo, private val className: Stri private fun constructMod(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) { val modClass: Class<*> try { - modClass = Class.forName(className, true, loader) + modClass = Class.forName(className, true, classLoader) logger.debug(Logging.LOADING, "Loaded kotlin modclass ${modClass.name} with ${modClass.classLoader}") - } catch (e: Throwable) { - logger.error(Logging.LOADING, "Failed to load kotlin class $className", e) - throw ModLoadingException(info, ModLoadingStage.CONSTRUCT, "fml.modloading.failedtoloadmodclass", e) + } catch (throwable: Throwable) { + logger.error(Logging.LOADING, "Failed to load kotlin class $className", throwable) + throw ModLoadingException(info, ModLoadingStage.CONSTRUCT, "fml.modloading.failedtoloadmodclass", throwable) } try { logger.debug(Logging.LOADING, "Loading mod instance ${getModId()} of type ${modClass.name}") modInstance = modClass.kotlin.objectInstance ?: modClass.newInstance() logger.debug(Logging.LOADING, "Loaded mod instance ${getModId()} of type ${modClass.name}") - } catch (e: Throwable) { - logger.error(Logging.LOADING, "Failed to create mod instance. ModID: ${getModId()}, class ${modClass.name}", e) - throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "fml.modloading.failedtoloadmod", e, modClass) + } catch (throwable: Throwable) { + logger.error(Logging.LOADING, "Failed to create mod instance. ModID: ${getModId()}, class ${modClass.name}", throwable) + throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "fml.modloading.failedtoloadmod", throwable, modClass) } try { @@ -92,14 +85,21 @@ class KotlinModContainer(private val info: IModInfo, private val className: Stri // Inject into object EventBusSubscribers AutoKotlinEventBusSubscriber.inject(this, scanData, modClass.classLoader) logger.debug(Logging.LOADING, "Completed Automatic Kotlin event subscribers for ${getModId()}") - } catch (e: Throwable) { - logger.error(Logging.LOADING, "Failed to register Automatic Kotlin subscribers. ModID: ${getModId()}, class ${modClass.name}", e) - throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "fml.modloading.failedtoloadmod", e, modClass) + } catch (throwable: Throwable) { + logger.error(Logging.LOADING, "Failed to register Automatic Kotlin subscribers. ModID: ${getModId()}, class ${modClass.name}", throwable) + throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "fml.modloading.failedtoloadmod", throwable, modClass) } + } + override fun matches(mod: Any?): Boolean { + return mod == modInstance } - override fun getMod(): Any = modInstance + override fun getMod(): Any { + return modInstance + } - override fun matches(mod: Any?): Boolean = mod == modInstance + override fun acceptEvent(e: Event) { + eventBus.post(e) + } }
\ No newline at end of file diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModLoadingContext.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModLoadingContext.kt index 1c50aae..af42d16 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModLoadingContext.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/KotlinModLoadingContext.kt @@ -6,13 +6,13 @@ import net.minecraftforge.fml.ModLoadingContext /** * Functions as [net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext] for Kotlin */ -class KotlinModLoadingContext constructor(private val container: KotlinModContainer) { - fun getEventBus(): IEventBus { +public class KotlinModLoadingContext constructor(private val container: KotlinModContainer) { + public fun getEventBus(): IEventBus { return container.eventBus } - companion object { - fun get(): KotlinModLoadingContext { + public companion object { + public fun get(): KotlinModLoadingContext { return ModLoadingContext.get().extension() } } diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/Logger.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/Logger.kt index 8500863..8276903 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/Logger.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/Logger.kt @@ -2,4 +2,10 @@ package thedarkcolour.kotlinforforge import org.apache.logging.log4j.LogManager +/** + * Logger field for KotlinForForge. + * + * Kept here instead of [KotlinForForge] because logger is used + * before [KotlinModContainer] should initialize. + */ internal val logger = LogManager.getLogger()
\ No newline at end of file diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/forge/Forge.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/forge/Forge.kt index 73cab7e..d712c75 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/forge/Forge.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/forge/Forge.kt @@ -18,7 +18,7 @@ import thedarkcolour.kotlinforforge.KotlinModLoadingContext * @see net.minecraftforge.event.entity.living.LivingEvent * @see net.minecraftforge.event.world.BlockEvent */ -val FORGE_BUS: IEventBus +public val FORGE_BUS: IEventBus inline get() = MinecraftForge.EVENT_BUS /** @since 1.0.0 @@ -30,7 +30,7 @@ val FORGE_BUS: IEventBus * @see net.minecraftforge.event.AttachCapabilitiesEvent * @see net.minecraftforge.event.RegistryEvent */ -val MOD_BUS: IEventBus +public val MOD_BUS: IEventBus inline get() = KotlinModLoadingContext.get().getEventBus() /** @since 1.0.0 @@ -38,22 +38,22 @@ val MOD_BUS: IEventBus * * Used in place of [net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext] */ -val MOD_CONTEXT: KotlinModLoadingContext +public val MOD_CONTEXT: KotlinModLoadingContext inline get() = KotlinModLoadingContext.get() -val LOADING_CONTEXT: ModLoadingContext +public val LOADING_CONTEXT: ModLoadingContext inline get() = ModLoadingContext.get() /** @since 1.0.0 * The current [Dist] of this environment. */ -val DIST: Dist = FMLEnvironment.dist +public val DIST: Dist = FMLEnvironment.dist /** @since 1.0.0 * An alternative to [net.minecraftforge.fml.DistExecutor.callWhenOn] * that inlines the callable. */ -inline fun <T> callWhenOn(dist: Dist, toRun: () -> T): T? { +public inline fun <T> callWhenOn(dist: Dist, toRun: () -> T): T? { return if (DIST == dist) { try { toRun() @@ -69,7 +69,7 @@ inline fun <T> callWhenOn(dist: Dist, toRun: () -> T): T? { * An alternative to [net.minecraftforge.fml.DistExecutor.runWhenOn] * that uses Kotlin functions instead of Java functional interfaces. */ -inline fun runWhenOn(dist: Dist, toRun: () -> Unit) { +public inline fun runWhenOn(dist: Dist, toRun: () -> Unit) { if (DIST == dist) { toRun() } @@ -79,7 +79,7 @@ inline fun runWhenOn(dist: Dist, toRun: () -> Unit) { * An alternative to [net.minecraftforge.fml.DistExecutor.runForDist] * that inlines the method call. */ -inline fun <T> runForDist(clientTarget: () -> T, serverTarget: () -> T): T { +public inline fun <T> runForDist(clientTarget: () -> T, serverTarget: () -> T): T { return when (DIST) { Dist.CLIENT -> clientTarget() Dist.DEDICATED_SERVER -> serverTarget() @@ -89,7 +89,7 @@ inline fun <T> runForDist(clientTarget: () -> T, serverTarget: () -> T): T { /** @since 1.0.0 * Registers a config. */ -fun registerConfig(type: ModConfig.Type, spec: ForgeConfigSpec, fileName: String? = null) { +public fun registerConfig(type: ModConfig.Type, spec: ForgeConfigSpec, fileName: String? = null) { if (fileName == null) { LOADING_CONTEXT.registerConfig(type, spec) } else { |