tutorial:kotlin
Differences
This shows you the differences between two versions of the page.
tutorial:kotlin [2023/06/13 05:05] – created dayo05 | tutorial:kotlin [2023/08/11 12:44] (current) – grammar, spelling, wording tooster | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Using Kotlin with Fabric ====== | ====== Using Kotlin with Fabric ====== | ||
- | **WARNING: this document assumes you have highly | + | **WARNING: this document assumes you have prior knowledge |
- | Kotlin is very powerful language which highly | + | [[https:// |
- | ===== Advantage | + | ===== Advantage |
- | * Easy to using lambda. | + | * Far less boilerplate. |
- | * '' | + | * Easy to use lambdas. |
+ | * A lot of syntax sugar that makes coding easier. | ||
+ | * Automatic type inference (compiler can determine a type of variable based on it's usage) | ||
+ | * Null-safety out of the box using nullable and not-nullable types (e.g. not-nullable | ||
+ | * Extension functions, i.e. functions that seem to be " | ||
+ | * Operator overloading | ||
+ | * Good separation between mutable ('' | ||
+ | * Support | ||
+ | * Anonymous objects (without class declarations) and data classes (simple value holders) | ||
+ | * Type variance and covariance, no wildcard generic types | ||
+ | * String templates (like ''" | ||
+ | * A ton of utility functions | ||
+ | * ...and a lot more | ||
- | I assume you're using latest version | + | For a more comprehensive comparison |
- | ===== Understanding Limitation ===== | + | |
- | * You cannot use Kotlin file inside Mixin. But, you can redirect with invoking method which marked by '' | + | |
- | ===== Step 1: Configure/ | + | |
- | See [[tutorial: | + | |
- | ===== Step 2: Configure Kotlin with IntellIJ's feature ===== | + | The following steps assume you're using the latest version of [[https://www.jetbrains.com/ |
- | 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 | + | |
- | Finally, sync gradle project. | + | ===== Limitations ===== |
+ | * You cannot use kotlin files for mixins. | ||
+ | * I REPEAT, YOU **CANNOT** USE KOTLIN FILES FOR MIXINS... | ||
+ | * ...But, you can redirect a code to invoke a function marked with '' | ||
- | You can write/compile with Kotlin this point. | + | ===== Step 1: Configure/Initialize mod ===== |
+ | Create your mod as you usually do — see [[tutorial: | ||
- | ===== Step 3: Modify gradle file ===== | + | ===== Step 2: Configure Kotlin using IntellIJ' |
- | ==== Method of manage | + | This step essentially boils down to using a " |
- | There are [[https:// | + | |
- | To use this, you can convert | + | This is the easiest method of configuring it, especially when used with the [[https:// |
+ | |||
+ | Finally, sync gradle project (two arrows icon tn the Gradle toolbar or with '' | ||
+ | |||
+ | You should be able to use Kotlin at this point. | ||
+ | |||
+ | ===== Step 3 (recommended): | ||
+ | |||
+ | ==== Add the official kotlin fabric library to your `build.gradle` file ==== | ||
+ | The previous step added a dependency on kotlin stdlib. You can use [[https:// | ||
+ | |||
+ | To do this, replace | ||
<code groovy> | <code groovy> | ||
- | implementation(" | + | dependencies { |
+ | //... | ||
+ | implementation(" | ||
+ | } | ||
</ | </ | ||
- | to | + | with |
<code groovy> | <code groovy> | ||
- | modImplementation(" | + | dependencies { |
+ | // | ||
+ | modImplementation(" | ||
+ | } | ||
</ | </ | ||
- | Do not forget to add dependency to '' | ||
- | Also, you can change the signature of the initializer handler to Kotlin specified method like method inside object classes.(As described inside | + | (Use appropriate version, which can be found on [[https:// |
- | ==== Increasing version of jvmToolchain(Only for MC 1.17+) | + | Do not forget to modify your '' |
- | Because | + | |
+ | <code json> | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ] | ||
+ | }, | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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 [[https:// | ||
+ | |||
+ | ==== Increasing version of jvmToolchain ==== | ||
+ | Some errors may appear when compiling | ||
- | You can simply increasing the number of | ||
<code groovy> | <code groovy> | ||
jvmToolchain(11) | jvmToolchain(11) | ||
</ | </ | ||
- | greater then version of java.(I suggest using 17 in MC 1.18+) | + | |
+ | You should be able to use '' |
tutorial/kotlin.1686632722.txt.gz · Last modified: 2023/06/13 05:05 by dayo05