From 1d4d9787edfea95c3aa23babf1643ce901afa7ac Mon Sep 17 00:00:00 2001 From: thedarkcolour <30441001+thedarkcolour@users.noreply.github.com> Date: Sun, 18 Oct 2020 12:21:34 -0700 Subject: Close #8 --- build.gradle | 68 +++++------ .../kotlinforforge/eventbus/KotlinEventBus.kt | 37 +++--- .../thedarkcolour/kotlinforforge/test/TestMod.kt | 128 +++++++++++++++++++++ src/main/resources/META-INF/mods.toml | 20 ++-- .../1.6.2/kotlinforforge-1.6.2-obf.jar | Bin 0 -> 6220265 bytes .../1.6.2/kotlinforforge-1.6.2-obf.jar.md5 | 1 + .../1.6.2/kotlinforforge-1.6.2-obf.jar.sha1 | 1 + .../1.6.2/kotlinforforge-1.6.2-sources.jar | Bin 0 -> 38604 bytes .../1.6.2/kotlinforforge-1.6.2-sources.jar.md5 | 1 + .../1.6.2/kotlinforforge-1.6.2-sources.jar.sha1 | 1 + .../kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar | Bin 0 -> 112723 bytes .../1.6.2/kotlinforforge-1.6.2.jar.md5 | 1 + .../1.6.2/kotlinforforge-1.6.2.jar.sha1 | 1 + thedarkcolour/kotlinforforge/maven-metadata.xml | 3 +- .../kotlinforforge/maven-metadata.xml.md5 | 2 +- .../kotlinforforge/maven-metadata.xml.sha1 | 2 +- thedarkcolour/kotlinforforge/web.html | 1 + 17 files changed, 199 insertions(+), 68 deletions(-) create mode 100644 src/main/kotlin/thedarkcolour/kotlinforforge/test/TestMod.kt create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar.md5 create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar.sha1 create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar.md5 create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar.sha1 create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar.md5 create mode 100644 thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar.sha1 diff --git a/build.gradle b/build.gradle index ccff79b..68328d6 100644 --- a/build.gradle +++ b/build.gradle @@ -22,7 +22,7 @@ apply plugin: 'net.minecraftforge.gradle' apply plugin: 'kotlin' apply plugin: 'org.jetbrains.dokka' -version = "1.6.1" +version = "1.6.2" group = 'thedarkcolour.kotlinforforge' archivesBaseName = 'kotlinforforge' @@ -31,38 +31,6 @@ sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = co tasks.build.dependsOn kotlinSourcesJar tasks.build.dependsOn shadowJar -minecraft { - mappings channel: 'snapshot', version: '20200723-1.16.1' - - runs { - client { - workingDirectory project.file('run') - - property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' - - property 'forge.logging.console.level', 'debug' - } - - server { - workingDirectory project.file('run/server') - - property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' - - property 'forge.logging.console.level', 'debug' - } - - data { - workingDirectory project.file('run') - - property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP' - - property 'forge.logging.console.level', 'debug' - - args '--mod', 'examplemod', '--all', '--output', file('src/generated/resources/') - } - } -} - repositories { mavenCentral() maven { @@ -91,7 +59,25 @@ dependencies { compile group: "org.jetbrains.kotlinx", name: "kotlinx-coroutines-jdk8", version: coroutines_version // Tests - testImplementation 'junit:junit:4.11' + implementation 'junit:junit:4.11' +} + +minecraft { + mappings channel: 'snapshot', version: '20200723-1.16.1' + + runs { + client { + workingDirectory project.file('run') + + property 'forge.logging.console.level', 'debug' + } + + server { + workingDirectory project.file('run/server') + + property 'forge.logging.console.level', 'debug' + } + } } shadowJar { @@ -122,11 +108,17 @@ jar { } } +static def options(kotlinOptions) { + kotlinOptions.jvmTarget = '1.8' + kotlinOptions.freeCompilerArgs = ["-Xexplicit-api=warning"] +} + compileKotlin { - kotlinOptions { - jvmTarget = '1.8' - freeCompilerArgs = ["-Xexplicit-api=warning"] - } + options(kotlinOptions) +} + +compileTestKotlin { + options(kotlinOptions) } kotlinSourcesJar { diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/eventbus/KotlinEventBus.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/eventbus/KotlinEventBus.kt index c35c4e9..3f3fb66 100644 --- a/src/main/kotlin/thedarkcolour/kotlinforforge/eventbus/KotlinEventBus.kt +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/eventbus/KotlinEventBus.kt @@ -18,6 +18,9 @@ import java.util.function.Consumer /** @since 1.2.0 * Fixes [addListener] and [addGenericListener] for Kotlin KCallable. + * + * @param builder The BusBuilder used to configure this event bus + * @param synthetic Whether this event bus is just a wrapper for another bus */ public open class KotlinEventBus(builder: BusBuilder, synthetic: Boolean = false) : IEventBus, IEventExceptionHandler { @Suppress("LeakingThis") @@ -305,26 +308,24 @@ public open class KotlinEventBus(builder: BusBuilder, synthetic: Boolean = false */ private fun reflectKotlinSAM(consumer: Consumer<*>): Class<*>? { val clazz = consumer.javaClass + val forgeType = TypeResolver.resolveRawArgument(Consumer::class.java, consumer.javaClass) - when { - clazz.simpleName.contains("$\$Lambda$") -> { - return TypeResolver.resolveRawArgument(Consumer::class.java, consumer.javaClass) - } - clazz.simpleName.contains("\$sam$") -> { - try { - val functionField = clazz.getDeclaredField("function") - functionField.isAccessible = true - val function = functionField[consumer] - - // Function should have two type parameters (parameter type and return type) - return TypeResolver.resolveRawArguments(kotlin.jvm.functions.Function1::class.java, function.javaClass)[0] - } catch (e: NoSuchFieldException) { - // Kotlin SAM interfaces compile to classes with a "function" field - LOGGER.log(Level.FATAL, "Tried to register invalid Kotlin SAM interface: Missing 'function' field") - throw e - } + if (clazz.simpleName.contains("\$sam$")) { + try { + val functionField = clazz.getDeclaredField("function") + functionField.isAccessible = true + val function = functionField[consumer] + + // Function should have two type parameters (parameter type and return type) + return TypeResolver.resolveRawArguments(kotlin.jvm.functions.Function1::class.java, function.javaClass)[0] + } catch (e: NoSuchFieldException) { + // Kotlin SAM interfaces compile to classes with a "function" field + LOGGER.log(Level.FATAL, "Tried to register invalid Kotlin SAM interface: Missing 'function' field") + throw e } - else -> return null + } else { + // Kotlin 1.4 seems to have fixed some of its lambda problems + return forgeType } } diff --git a/src/main/kotlin/thedarkcolour/kotlinforforge/test/TestMod.kt b/src/main/kotlin/thedarkcolour/kotlinforforge/test/TestMod.kt new file mode 100644 index 0000000..01de386 --- /dev/null +++ b/src/main/kotlin/thedarkcolour/kotlinforforge/test/TestMod.kt @@ -0,0 +1,128 @@ +package thedarkcolour.kotlinforforge.test + +import net.minecraftforge.eventbus.api.BusBuilder +import net.minecraftforge.eventbus.api.Event +import net.minecraftforge.eventbus.api.EventPriority +import net.minecraftforge.eventbus.api.GenericEvent +import org.junit.Assert.assertTrue +import thedarkcolour.kotlinforforge.eventbus.KotlinEventBus +import java.util.function.Consumer + +/** + * Uncomment the `@Mod` annotation and uncomment the entry in mods.toml to run the test mod + */ +//@Mod("test_mod") +public class TestMod { + + init { + val l = arrayListOf() + + try { + testAddListenerBridge() + } catch (t: Throwable) { + l.add(t) + } + try { + testFunctionReference() + } catch (t: Throwable) { + l.add(t) + } + try { + testGenericListeners() + } catch (t: Throwable) { + l.add(t) + } + + l.forEach { throwable -> + throwable.printStackTrace() + } + + if (l.isNotEmpty()) { + throw RuntimeException("Test mod did not pass") + } + } + + // Test event class + public open class TestEvent(public var value: String) : Event() + + /** + * Tests the new bridge lambdas introduced in Kotlin 1.4. + */ + private fun testAddListenerBridge() { + val bus = KotlinEventBus(BusBuilder.builder()) + + // Make sure the type checker works properly in Kotlin 1.4 + bus.addListener { event -> + event.value = "Foo" + } + + // Check that the event posts for added listeners + val event = TestEvent("Bar") + bus.post(event) + assertTrue(event.value == "Foo") + } + + // Test function for function references + private fun exampleFunction(event: TestEvent) { + event.value = "Foo" + } + + /** + * Test that function references work properly in [KotlinEventBus.addListener]. + */ + private fun testFunctionReference() { + val bus = KotlinEventBus(BusBuilder.builder()) + val event = TestEvent("Bar") + + bus.addListener(::exampleFunction) + bus.post(event) + assertTrue(event.value == "Foo") + } + + // generic event + public class TestGenericEvent(type: Class, internal var value: T) : GenericEvent(type) + + // test the function references here as well + private fun functionReference(event: TestGenericEvent) { + event.value = "Far" + } + + // Consumer class + public class EventConsumer : Consumer> { + override fun accept(t: TestGenericEvent) { + t.value = "Jar" + } + } + + /** + * Test that [KotlinEventBus.addGenericListener] respects the generic type + * of an event fired on the [KotlinEventBus] instance. + */ + public fun testGenericListeners() { + // event bus + val bus = KotlinEventBus(BusBuilder.builder()) + + @Suppress("JoinDeclarationAndAssignment") + var fooEvent: TestGenericEvent + + // test fooEvent + fooEvent = TestGenericEvent(String::class.java, "Foo") + bus.addGenericListener(priority = EventPriority.HIGHEST) { e: TestGenericEvent -> + e.value = "Bar" + } + bus.post(fooEvent) + assertTrue(fooEvent.value == "Bar") + + // test consumer subclass + fooEvent = TestGenericEvent(String::class.java, "Foo") + bus.addGenericListener(priority = EventPriority.HIGH, EventConsumer()) + bus.post(fooEvent) + assertTrue(fooEvent.value == "Jar") + + // test barEvent for function references + fooEvent = TestGenericEvent(String::class.java, "Foo") + bus.addGenericListener(priority = EventPriority.NORMAL, ::functionReference) + bus.post(fooEvent) + assertTrue(fooEvent.value == "Far") + } +} \ No newline at end of file diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 27b9220..e0174e8 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -1,13 +1,7 @@ modLoader="kotlinforforge" # IModLanguageProvider -loaderVersion="[1.4,)" # IModLanguageProvider version +loaderVersion="[1.6,)" # IModLanguageProvider version license="GPL v3.0" -issueTrackerURL="https://github.com/thedarkcolour/KotlinForForge/issues" # Issues page - -description=''' -Kotlin for Forge. Allows mods to use the Kotlin programming language. -''' - # A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. [[dependencies.kotlinforforge]] #optional modId="forge" #mandatory @@ -29,9 +23,17 @@ Kotlin for Forge. Allows mods to use the Kotlin programming language. # --------------------------------------------------------- # # --------------------------------------------------------- # +# UNCOMMENT THIS TO RUN TEST MOD +# [[mods]] +# modId="test_mod" + [[mods]] #mandatory displayName="Kotlin for Forge" # Name of mod modId="kotlinforforge" # Modid -version="1.4.0" # Version of kotlinforforge +version="1.6.2" # Version of kotlinforforge authors="TheDarkColour" # Author -credits="Herobrine knows all." # Credits \ No newline at end of file +credits="Herobrine knows all." # Credits +description=''' +Kotlin for Forge. Allows mods to use the Kotlin programming language. +''' +issueTrackerURL="https://github.com/thedarkcolour/KotlinForForge/issues" # Issues page \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar new file mode 100644 index 0000000..cf53b93 Binary files /dev/null and b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar differ diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar.md5 b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar.md5 new file mode 100644 index 0000000..fefc52c --- /dev/null +++ b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar.md5 @@ -0,0 +1 @@ +97119625be6cbf290309672b3aa72f4c \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar.sha1 b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar.sha1 new file mode 100644 index 0000000..538a7a7 --- /dev/null +++ b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-obf.jar.sha1 @@ -0,0 +1 @@ +019df74f4dec7fccaa528d4419ceda3c203b2c42 \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar new file mode 100644 index 0000000..7c24137 Binary files /dev/null and b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar differ diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar.md5 b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar.md5 new file mode 100644 index 0000000..a4fe7c5 --- /dev/null +++ b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar.md5 @@ -0,0 +1 @@ +49a310cd958ae2f678d5a90685cfcf64 \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar.sha1 b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar.sha1 new file mode 100644 index 0000000..46e0fca --- /dev/null +++ b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2-sources.jar.sha1 @@ -0,0 +1 @@ +563054148acad349df1ab224eb41501472a9afa0 \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar new file mode 100644 index 0000000..c01b48a Binary files /dev/null and b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar differ diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar.md5 b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar.md5 new file mode 100644 index 0000000..2f32e45 --- /dev/null +++ b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar.md5 @@ -0,0 +1 @@ +f3aee52944c83f6ad081feaa73ec2449 \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar.sha1 b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar.sha1 new file mode 100644 index 0000000..1302d75 --- /dev/null +++ b/thedarkcolour/kotlinforforge/1.6.2/kotlinforforge-1.6.2.jar.sha1 @@ -0,0 +1 @@ +7c08bbfd5d0ec3c8172d2e302cbed9f3c893dc70 \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/maven-metadata.xml b/thedarkcolour/kotlinforforge/maven-metadata.xml index f63a2a6..ab5e614 100644 --- a/thedarkcolour/kotlinforforge/maven-metadata.xml +++ b/thedarkcolour/kotlinforforge/maven-metadata.xml @@ -3,7 +3,7 @@ thedarkcolour kotlinforforge - 1.6.1 + 1.6.2 1.0.0 1.0.1 @@ -18,6 +18,7 @@ 1.5.0 1.6.0 1.6.1 + 1.6.2 diff --git a/thedarkcolour/kotlinforforge/maven-metadata.xml.md5 b/thedarkcolour/kotlinforforge/maven-metadata.xml.md5 index b1bfa38..28600b0 100644 --- a/thedarkcolour/kotlinforforge/maven-metadata.xml.md5 +++ b/thedarkcolour/kotlinforforge/maven-metadata.xml.md5 @@ -1 +1 @@ -5dd75e78f41e4517a86d3efbf089de45 \ No newline at end of file +69c821851928df66bc4c3884970be77f \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/maven-metadata.xml.sha1 b/thedarkcolour/kotlinforforge/maven-metadata.xml.sha1 index d003f71..0a2f82f 100644 --- a/thedarkcolour/kotlinforforge/maven-metadata.xml.sha1 +++ b/thedarkcolour/kotlinforforge/maven-metadata.xml.sha1 @@ -1 +1 @@ -472c20190bb6d7e6e36f7c5b53960bf8129392b0 \ No newline at end of file +516f8882b5e026e17f8152820838e770faaddde9 \ No newline at end of file diff --git a/thedarkcolour/kotlinforforge/web.html b/thedarkcolour/kotlinforforge/web.html index 68989de..97688b4 100644 --- a/thedarkcolour/kotlinforforge/web.html +++ b/thedarkcolour/kotlinforforge/web.html @@ -21,6 +21,7 @@ 1.5.0 1.6.0 1.6.1 +1.6.2 maven-metadata.xml maven-metadata.xml.md5 maven-metadata.xml.sha1 -- cgit