Fabric language module for Kotlin. Adds support for using a Kotlin object as the main mod class and bundles the Kotlin libraries and runtime for you.
Add it as a dependency (build.gradle and build.gradle.kts):
dependencies {
// [...]
modImplementation(group = "net.fabricmc", name = "fabric-language-kotlin", version = "1.7.1+kotlin.1.6.10")
}Use the kotlin adapter for your mod by setting the adapter property in the fabric.mod.json file.
Remember to the add a dependency entry to your fabric.mod.json file:
{
"schemaVersion": 1,
"entrypoints": {
"main": [
{
"adapter": "kotlin",
"value": "package.ClassName"
}
]
},
"depends": {
"fabric-language-kotlin": ">=1.7.1+kotlin.1.6.10"
}
}For more info reference documentation:fabric_mod_json.
Do not forget to set the schemaVersion to 1 or it will fall back to schema 0 and will not attempt to load entrypoints.
As a class
Click to view code
{
"adapter": "kotlin",
"value": "mymod.MyMod"
}package mymod
class MyMod : ModInitializer {
override fun onInitialize() {
TODO()
}
}As an object
Click to view code
{
"adapter": "kotlin",
"value": "mymod.MyMod"
}package mymod
object MyMod : ModInitializer {
override fun onInitialize() {
TODO()
}
}As a companion object
Click to view code
{
"adapter": "kotlin",
"value": "mymod.MyMod$Companion"
}package mymod
class MyMod {
companion object : ModInitializer {
override fun onInitialize() {
TODO()
}
}
}Functions do not get returned but executed, so they have to only contain initialization code, not return a initializer type.
In an object
Click to view code
{
"adapter": "kotlin",
"value": "mymod.MyMod::init"
}package mymod
object MyMod {
fun init() {
TODO()
}
}In a companion object
Click to view code
{
"adapter": "kotlin",
"value": "mymod.MyMod$Companion::init"
}package mymod
class MyMod {
companion object {
fun init() {
TODO()
}
}
}As a top level function
Click to view code
The classname gets constructed by taking the filename and appending Kt.
{
"adapter": "kotlin",
"value": "mymod.MyModKt::init"
}File: src/main/kotlin/mymod/MyMod.kt
package mymod
fun init() {
TODO()
}In an object
Click to view code
{
"adapter": "kotlin",
"value": "mymod.MyMod::initializer"
}package mymod
object MyMod {
val initializer = ModInitializer {
TODO()
}
}In a companion object
Click to view code
{
"adapter": "kotlin",
"value": "mymod.MyMod$Companion::initializer"
}package mymod
class MyMod {
companion object {
val initializer = ModInitializer {
TODO()
}
}
}Companion objects can be used by appending $Companion to the class.
Take care of processResource there, it might try to expand it, in that case escape it.
See examples in sample-mod/fabric.mod.json.
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10
org.jetbrains.kotlin:kotlin-reflect:1.6.10
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2
org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.5.2
org.jetbrains.kotlinx:kotlinx-serialization-core-jvm:1.3.1
org.jetbrains.kotlinx:kotlinx-serialization-json-jvm:1.3.1
org.jetbrains.kotlinx:kotlinx-serialization-cbor-jvm:1.3.1
https://maven.fabricmc.net/net/fabricmc/fabric-language-kotlin/
- Update the readme in
templates/README.template.md. - Run
./gradlew processMDTemplates.