Forgelin/src/main/kotlin/net/shadowfacts/forgelin/KotlinAdapter.kt

41 lines
1.5 KiB
Kotlin
Raw Normal View History

2016-08-06 14:28:05 +00:00
package net.shadowfacts.forgelin
import net.minecraftforge.fml.common.FMLModContainer
import net.minecraftforge.fml.common.ILanguageAdapter
import net.minecraftforge.fml.common.ModContainer
import net.minecraftforge.fml.relauncher.Side
import org.apache.logging.log4j.LogManager
import java.lang.reflect.Field
import java.lang.reflect.Method
/**
2016-11-14 15:53:45 +00:00
* Forge {@link ILanguageAdapter} for Kotlin
* Usage: Set the {@code modLanguageAdapter} field in your {@code @Mod} annotation to {@code net.shadowfacts.forgelin.KotlinAdapter}
* @author shadowfacts
2016-08-06 14:28:05 +00:00
*/
class KotlinAdapter : ILanguageAdapter {
2016-08-06 14:28:05 +00:00
2016-11-14 15:53:45 +00:00
private val log = LogManager.getLogger("KotlinAdapter")
2016-08-06 14:28:05 +00:00
override fun supportsStatics(): Boolean {
return false
}
override fun setProxy(target: Field, proxyTarget: Class<*>, proxy: Any) {
2016-11-14 15:53:45 +00:00
log.debug("Setting proxy: ${target.declaringClass.simpleName}.${target.name} -> $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)
2016-08-06 14:28:05 +00:00
}
2016-11-14 15:53:45 +00:00
override fun getNewInstance(container: FMLModContainer, objectClass: Class<*>, classLoader: ClassLoader, factoryMarkedAnnotation: Method?): Any {
log.debug("FML has asked for ${objectClass.simpleName} to be constructed")
return objectClass.kotlin.objectInstance ?: objectClass.newInstance()
2016-08-06 14:28:05 +00:00
}
override fun setInternalProxies(mod: ModContainer?, side: Side?, loader: ClassLoader?) {
2016-11-14 15:53:45 +00:00
// Nothing to do; FML's got this covered for Kotlin
2016-08-06 14:28:05 +00:00
}
}