Table of Contents

Using Kotlin with Fabric

WARNING: this document assumes you have prior knowledge of Java (and a bit of JVM and Gradle). THIS IS NOT A KOTLIN LANGUAGE TUTORIAL, for that see the official Kotlin site.

Kotlin is a very powerful language which highly integrates with Java and can be executed on a JVM (it's compiled down to Java but you can write interop java and kotlin code in one project). This means, that you can use Kotlin for development of Minecraft fabric mods!

Advantage of using Kotlin

For a more comprehensive comparison of Kotlin vs Java visit the official site.

The following steps assume you're using the latest version of IntellIJ IDEA.

Limitations

Step 1: Configure/Initialize mod

Create your mod as you usually do — see creating mod. If you already have a mod configured, go to next step.

Step 2: Configure Kotlin using IntellIJ's Action

This step essentially boils down to using a “Configure Kotlin in Project” Action which you can access via (Tools → Kotlin → Configure Kotlin in Project) menu item, a quick-action menu (Shift, Shift and search for “Configure Kotlin…”) or a shortcut (Ctrl+Alt+Shift+K or Command+Option+Shift+K).

This is the easiest method of configuring it, especially when used with the Minecraft Development plugin which doesn't support the Kotlin template. This will apply the appropriate Kotlin plugin for gradle and add dependencies for Kotlin language.

Finally, sync gradle project (two arrows icon tn the Gradle toolbar or with Ctrl+Shift+O when a gradle file is opened).

You should be able to use Kotlin at this point.

Step 3 (recommended): Modify gradle file to add fabric kotlin modding library

Add the official kotlin fabric library to your `build.gradle` file

The previous step added a dependency on kotlin stdlib. You can use an official ''fabric-language-kotlin'' dependency to add more extensive kotlin support for your mod. With this dependency, some core and some extra packages for kotlin will automatically be added to the project, and you shall be spared of wasting your time with manually shadowing entire Kotlin classes.

To do this, replace

dependencies {
   //... 
   implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}

with

dependencies {
   //...
   modImplementation("net.fabricmc:fabric-language-kotlin:1.10.8+kotlin.1.9.0")
}

(Use appropriate version, which can be found on the official github)

Do not forget to modify your fabric.mod.json as stated in their README.md:

{
    "schemaVersion":  1, 
    "entrypoints": {
        "main": [
            {
                "adapter": "kotlin",
                "value": "package.ClassName"
            }
        ]
    },
    "depends": {
        "fabric-language-kotlin": ">=1.10.8+kotlin.1.9.0"
    }
}

You can also modify mod entrypoints to allow for top-level functions, fields, methods or function references to work. Check the documentation of entrypoints in their README.md.

Increasing version of jvmToolchain

Some errors may appear when compiling Kotlin, so it's a good idea to increase the version of jvmToolcahin depending on your mc version (kotlin compiles down to java 8 by default):

jvmToolchain(11)

You should be able to use 11 for versions below 1.17, 16 for 1.17 and 17 since 1.18.