diff --git a/gradle.properties b/gradle.properties index 39dbc7f..9635aaa 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -mod_version = 1.1.0 +mod_version = 1.2.0 group = net.shadowfacts archivesBaseName = Forgelin @@ -6,4 +6,4 @@ mc_version = 1.10.2 mcp_mappings = snapshot_20160802 forge_version = 12.18.1.2045 -kotlin_version = 1.0.5 +kotlin_version = 1.0.6 diff --git a/src/main/kotlin/net/shadowfacts/forgelin/KotlinAdapter.kt b/src/main/kotlin/net/shadowfacts/forgelin/KotlinAdapter.kt index 4bdf24c..8b4239e 100644 --- a/src/main/kotlin/net/shadowfacts/forgelin/KotlinAdapter.kt +++ b/src/main/kotlin/net/shadowfacts/forgelin/KotlinAdapter.kt @@ -23,45 +23,19 @@ class KotlinAdapter : ILanguageAdapter { override fun setProxy(target: Field, proxyTarget: Class<*>, proxy: Any) { log.debug("Setting proxy: ${target.declaringClass.simpleName}.${target.name} -> $proxy") - if (proxyTarget.fields.any { x -> x.name == "INSTANCE" }) { - // Singleton - try { - log.debug("Setting proxy on INSTANCE; singleton target") - val obj = proxyTarget.getField("INSTANCE").get(null) - target.set(obj, proxy) - } catch (e: Exception) { - throw KotlinAdapterException(e) - } - } else { - target.set(proxyTarget, proxy) - } + + // objectInstance is not null if it's a Kotlin object, so set the value on the object + // if it is null, set the value on the static field + target.set(proxyTarget.kotlin.objectInstance, proxy) } override fun getNewInstance(container: FMLModContainer, objectClass: Class<*>, classLoader: ClassLoader, factoryMarkedAnnotation: Method?): Any { log.debug("FML has asked for ${objectClass.simpleName} to be constructed") - try { - // Try looking for an object type - val f = objectClass.getField("INSTANCE") - val obj = f.get(null) ?: throw NullPointerException() - log.debug("Found an object INSTANCE reference in ${objectClass.simpleName}, using that. ${obj}") - return obj - } catch (e: Exception) { - // Try looking for a class type - log.debug("Failed to get object reference, trying class construction") - try { - val obj = objectClass.newInstance() ?: throw NullPointerException() - log.debug("Constructed an object from a class type ($objectClass), using that. $obj") - return obj - } catch (e: Exception) { - throw KotlinAdapterException(e) - } - } + return objectClass.kotlin.objectInstance ?: objectClass.newInstance() } override fun setInternalProxies(mod: ModContainer?, side: Side?, loader: ClassLoader?) { // Nothing to do; FML's got this covered for Kotlin } - private class KotlinAdapterException(e: Exception) : RuntimeException("Kotlin adapter error - do not report to Forge!", e) - } \ No newline at end of file diff --git a/src/test/kotlin/Test.kt b/src/test/kotlin/Test.kt index dc388cb..52c9324 100644 --- a/src/test/kotlin/Test.kt +++ b/src/test/kotlin/Test.kt @@ -1,5 +1,6 @@ import net.minecraftforge.fml.common.FMLLog import net.minecraftforge.fml.common.Mod +import net.minecraftforge.fml.common.SidedProxy import net.minecraftforge.fml.common.event.FMLPreInitializationEvent /** @@ -8,9 +9,27 @@ import net.minecraftforge.fml.common.event.FMLPreInitializationEvent @Mod(modid = "test", modLanguageAdapter = "net.shadowfacts.forgelin.KotlinAdapter") object Test { + @SidedProxy(clientSide = "ClientProxy", serverSide = "CommonProxy") + lateinit var proxy: CommonProxy + private set + @Mod.EventHandler fun preInit(event: FMLPreInitializationEvent) { FMLLog.bigWarning("Hello from Kotlin!") + proxy.someMethod() } +} + +open class CommonProxy { + open fun someMethod() { + FMLLog.bigWarning("CommonProxy.someMethod") + } +} + +class ClientProxy: CommonProxy() { + override fun someMethod() { + super.someMethod() + FMLLog.bigWarning("ClientProxy.someMethod") + } } \ No newline at end of file