User Tools

Site Tools


tutorial:kotlin

This is an old revision of the document!


Using Kotlin with Fabric

WARNING: this document assumes you have highly knowledge about Java language and JVM itself. Also, THIS IS NOT A KOTLIN LANGUAGE TUTORIAL.

Kotlin is very powerful language which highly integrated with Java language and able to executed by JVM. With this, you can also use Kotlin language for development of Minecraft fabric mod.

Advantage for using Kotlin

  • Easy to using lambda.
  • val, var keyword for implicit type resolve.

I assume you're using latest version of IntellIJ.

Understanding Limitation

  • You cannot use Kotlin file inside Mixin. But, you can redirect with invoking method which marked by @JvmStatic annotation or method in object or companion object.

Step 1: Configure/Initialize mod as same as Java does

See here

Step 2: Configure Kotlin with IntellIJ's feature

You can use (Tools → Kotlin → Configure Kotlin in Project) or shortcut (Ctrl + Alt + Shift + K or Command + Option + Shift + K). This is the easiest method to configure especially using with Minecraft Development plugin which doesn't supports Kotlin template. This will apply gradle plugin for Kotlin, add dependencies for Kotlin language.

Finally, sync gradle project.

You can write/compile with Kotlin this point.

Step 3: Modify gradle file

Method of manage Kotlin core library dependency

There are API which provided by fabric project to support kotlin which can integrate easier. With this dependency, core and some extra packages for kotlin has automatically added to project, and you don't need to spend energy caused by shadowing entire Kotlin classes.

To use this, you can convert

implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

to

modImplementation("net.fabricmc:fabric-language-kotlin:1.9.5+kotlin.1.8.22")

Do not forget to add dependency to fabric.mod.json.

Also, you can change the signature of the initializer handler to Kotlin specified method like method inside object classes.(As described inside the README.md in github)

Increasing version of jvmToolchain(Only for MC 1.17+)

Because Kotlin has exception, sometimes you should mix Java. BUT Minecraft 1.17+ uses java version greater then 11, you should increase the version of jvmToolcahin.

You can simply increasing the number of

jvmToolchain(11)

greater then version of java.(I suggest using 17 in MC 1.18+)

tutorial/kotlin.1686632722.txt.gz · Last modified: 2023/06/13 05:05 by dayo05