User Tools

Site Tools


tutorial:kotlin

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tutorial:kotlin [2023/06/13 05:05] – created dayo05tutorial: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 knowledge about Java language and JVM itselfAlso, THIS IS NOT A KOTLIN LANGUAGE TUTORIAL.**+**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 [[https://kotlinlang.org/docs/getting-started.html|the official Kotlin site]].**
  
-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.+[[https://kotlinlang.org/|Kotlin]] is 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 meansthat you can use Kotlin for development of Minecraft fabric mods!
  
-===== Advantage for using Kotlin ===== +===== Advantage of using Kotlin ===== 
-  * Easy to using lambda+  * Far less boilerplate. 
-  * ''val'', ''var'' keyword for implicit type resolve.+  * 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 ''Int'' vs nullable ''Int?''
 +  * Extension functionsi.e. functions that seem to be "added" to existing types (it's a syntax sugar but a useful one) 
 +  * Operator overloading 
 +  * Good separation between mutable (''var'') and immutable (''val'') data which promotes good code practices 
 +  * Support for better functional programming patterns 
 +  * Anonymous objects (without class declarations) and data classes (simple value holders) 
 +  * Type variance and covariance, no wildcard generic types 
 +  * String templates (like ''"length of string is ${s.length}"''
 +  * A ton of utility functions 
 +  * ...and a lot more 
  
-I assume you're using latest version of [[https://www.jetbrains.com/idea/|IntellIJ]]. +For a more comprehensive comparison of Kotlin vs Java [[https://kotlinlang.org/docs/comparison-to-java.html#some-java-issues-addressed-in-kotlin|visit the official site]].
-===== Understanding Limitation ===== +
-  * You cannot use Kotlin file inside MixinBut, 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 [[tutorial:start|here]]+
  
-===== Step 2: Configure Kotlin with IntellIJ's feature ===== +The following steps assume you're using the latest version of [[https://www.jetbrains.com/idea/|IntellIJ IDEA]].
-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 [[https://plugins.jetbrains.com/plugin/8327-minecraft-development|Minecraft Development plugin]] which doesn't supports Kotlin template. This will apply gradle plugin for Kotlin, add dependencies for Kotlin language.+
  
-Finallysync gradle project.+===== Limitations ===== 
 +  * You cannot use kotlin files for mixins. 
 +  * I REPEATYOU **CANNOT** USE KOTLIN FILES FOR MIXINS...  
 +  * ...But, you can redirect a code to invoke a function marked with ''@JvmStatic'' annotation, to a method in an ''object'' or ''companion object'' from a Java mixin.
  
-You can write/compile with Kotlin this point.+===== Step 1: Configure/Initialize mod ===== 
 +Create your mod as you usually do — see [[tutorial:start|creating mod]]. If you already have a mod configured, go to next step.
  
-===== Step 3Modify gradle file ===== +===== Step 2Configure Kotlin using IntellIJ's Action ===== 
-==== Method of manage Kotlin core library dependency ==== +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 itema quick-action menu (''ShiftShift'' and search for "Configure Kotlin...") or a shortcut (''Ctrl''+''Alt''+''Shift''+''K'' or ''Command''+''Option''+''Shift''+''K'')
-There are [[https://github.com/FabricMC/fabric-language-kotlin|API which provided by fabric project]] to support kotlin which can integrate easier. With this dependencycore and some extra packages for kotlin has automatically added to projectand you don't need to spend energy caused by shadowing entire Kotlin classes.+
  
-To use this, you can convert +This is the easiest method of configuring it, especially when used with the [[https://plugins.jetbrains.com/plugin/8327-minecraft-development|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 [[https://github.com/FabricMC/fabric-language-kotlin|an official ''fabric-language-kotlin'' dependency]] to add more extensive kotlin support for your mod. With this dependencysome 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
 <code groovy> <code groovy>
-implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")+dependencies { 
 +   //...  
 +   implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") 
 +}
 </code> </code>
-to +with
 <code groovy> <code groovy>
-modImplementation("net.fabricmc:fabric-language-kotlin:1.9.5+kotlin.1.8.22")+dependencies { 
 +   //... 
 +   modImplementation("net.fabricmc:fabric-language-kotlin:1.10.8+kotlin.1.9.0") 
 +}
 </code> </code>
-Do not forget to add dependency to ''fabric.mod.json''. 
  
-Alsoyou 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)+(Use appropriate versionwhich can be found on [[https://github.com/FabricMC/fabric-language-kotlin|the official github]])
  
-==== Increasing version of jvmToolchain(Only for MC 1.17+) ==== +Do not forget to modify your ''fabric.mod.json'' as stated in their [[https://github.com/FabricMC/fabric-language-kotlin|README.md]]: 
-Because Kotlin has exceptionsometimes you should mix Java. **BUT Minecraft 1.17+ uses java version greater then 11, you should increase the version of jvmToolcahin.**+ 
 +<code json> 
 +
 +    "schemaVersion":  1,  
 +    "entrypoints":
 +        "main":
 +            { 
 +                "adapter": "kotlin", 
 +                "value": "package.ClassName" 
 +            } 
 +        ] 
 +    }, 
 +    "depends":
 +        "fabric-language-kotlin": ">=1.10.8+kotlin.1.9.0" 
 +    } 
 +
 +</code> 
 + 
 +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://github.com/FabricMC/fabric-language-kotlin|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):
  
-You can simply increasing the number of  
 <code groovy> <code groovy>
 jvmToolchain(11) jvmToolchain(11)
 </code> </code>
-greater then version of java.(I suggest using 17 in MC 1.18+)+ 
 +You should be able to use ''11'' for versions below 1.17, ''16'' for 1.17 and ''17'' since 1.18.
tutorial/kotlin.1686632722.txt.gz · Last modified: 2023/06/13 05:05 by dayo05