前言
Kotlin 是由 JetBrains 推出的一門基於 JVM 平臺的程式語言,引入了許多不同於 Java 的先進概念以及語法糖,極大地提高了開發人員的程式設計效率,廣受各路 Java 開發者推崇。但由於 NeoForge 官方並未就使用 Kotlin 開發模組提供支援,使得精通 Kotlin 的開發者未能使用所擅長的語言編寫模組而被迫改用 Java 。幸運的是藉助 thedarkcolour 開發的 KotlinForForge 前置模組,完全使用 Kotlin 語言開發 NeoForge 模組成為了可能。下面簡要地就藉助該前置模組開發 Kotlin 語言模組的步驟進行說明。
NeoForge MDK 的搭建
前往 NeoForge 的 示例 MDK 倉庫 下載示例 NeoForge 模組原始碼。切換到所下載的原始碼目錄中,先執行 ./gradlew runClient
嘗試啟動遊戲客戶端程式, Gradle 會自動下載完成所需的依賴檔案,成功啟動遊戲後關閉。接下來根據自己的 IDE 情況執行 ./gradlew eclipse
或 ./gradlew idea
進行 IDE 相關配置,完成後用 IDE 開啟專案。至此 MDK 搭建完畢。
配置專案 Kotlin 相關依賴
接下來需要對專案進行設定以引入 Kotlin 與前置模組 KotlinForForge 的支援。開啟 build.gradle
檔案,對 plugins
、 repositories
與 dependencies
程式碼塊進行修改:
plugins {
id 'java-library'
id 'eclipse'
id 'idea'
id 'maven-publish'
id 'net.neoforged.gradle.userdev' version '7.0.80'
// Adds the Kotlin Gradle plugin
id 'org.jetbrains.kotlin.jvm' version '1.9.22'
// OPTIONAL Kotlin Serialization plugin
id 'org.jetbrains.kotlin.plugin.serialization' version '1.9.22'
}
repositories {
mavenLocal()
// Add KFF Maven repository
maven {
name = 'Kotlin for Forge'
url = 'https://thedarkcolour.github.io/KotlinForForge/'
}
}
dependencies {
// Specify the version of Minecraft to use.
// Depending on the plugin applied there are several options. We will assume you applied the userdev plugin as shown above.
// The group for userdev is net.neoforged, the module name is neoforge, and the version is the same as the neoforge version.
// You can however also use the vanilla plugin (net.neoforged.gradle.vanilla) to use a version of Minecraft without the neoforge loader.
// And its provides the option to then use net.minecraft as the group, and one of; client, server or joined as the module name, plus the game version as version.
// For all intends and purposes: You can treat this dependency as if it is a normal library you would use.
implementation "net.neoforged:neoforge:${neo_version}"
// Example mod dependency with JEI
// The JEI API is declared for compile time use, while the full JEI artifact is used at runtime
// compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}"
// compileOnly "mezz.jei:jei-${mc_version}-forge-api:${jei_version}"
// runtimeOnly "mezz.jei:jei-${mc_version}-forge:${jei_version}"
// Example mod dependency using a mod jar from ./libs with a flat dir repository
// This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar
// The group id is ignored when searching -- in this case, it is "blank"
// implementation "blank:coolmod-${mc_version}:${coolmod_version}"
// Example mod dependency using a file as dependency
// implementation files("libs/coolmod-${mc_version}-${coolmod_version}.jar")
// Example project dependency using a sister or child project:
// implementation project(":myproject")
// For more info:
// http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
// http://www.gradle.org/docs/current/userguide/dependency_management.html
// Adds KFF as dependency and Kotlin libs (use the variant matching your mod loader)
// NEOFORGE
implementation 'thedarkcolour:kotlinforforge-neoforge:4.10.0'
}
接下來修改 gradle.properties
檔案:
# Sets default memory used for gradle commands. Can be overridden by user or command line properties.
#org.gradle.jvmargs=
org.gradle.daemon=false
org.gradle.debug=false
#read more on this at https://github.com/neoforged/NeoGradle/blob/NG_7.0/README.md#apply-parchment-mappings
# you can also find the latest versions at: https://parchmentmc.org/docs/getting-started
neogradle.subsystems.parchment.minecraftVersion=1.20.3
neogradle.subsystems.parchment.mappingsVersion=2023.12.31
# Environment Properties
# You can find the latest versions here: https://projects.neoforged.net/neoforged/neoforge
# The Minecraft version must agree with the Neo version to get a valid artifact
minecraft_version=1.20.4
# The Minecraft version range can use any release version of Minecraft as bounds.
# Snapshots, pre-releases, and release candidates are not guaranteed to sort properly
# as they do not follow standard versioning conventions.
minecraft_version_range=[1.20.4,1.21)
# The Neo version must agree with the Minecraft version to get a valid artifact
neo_version=20.4.215
# The Neo version range can use any version of Neo as bounds
neo_version_range=[20.4,)
# The loader version range can only use the major version of FML as bounds
loader_version_range=[4.10,)
## Mod Properties
# The unique mod identifier for the mod. Must be lowercase in English locale. Must fit the regex [a-z][a-z0-9_]{1,63}
# Must match the String constant located in the main mod class annotated with @Mod.
mod_id=kotlindemo
# The human-readable display name for the mod.
mod_name=Kotlin NeoForge Demo Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=All Rights Reserved
# The mod version. See https://semver.org/
mod_version=1.0.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
mod_group_id=top.srcres.mods.kotlindemo
# The authors of the mod. This is a simple text string that is used for display purposes in the mod list.
mod_authors=src_resources
# The description of the mod. This is a simple multiline text string that is used for display purposes in the mod list.
mod_description=Example mod description.\nNewline characters can be used and will be replaced properly.
然後開啟 src
目錄內的 mods.toml
檔案,修改 modLoader
為 kotlinforforge
:
modLoader="kotlinforforge" #mandatory
接下來刪除 java
目錄,新建 kotlin
目錄,即可在其中使用 Kotlin 語言編寫程式碼了。筆者的示例程式碼如下:
package top.srcres.mods.kotlindemo
import com.mojang.logging.LogUtils
import net.neoforged.fml.common.Mod
@Mod(KotlinDemo.MODID)
object KotlinDemo {
const val MODID = "kotlindemo"
val logger = LogUtils.getLogger();
init {
logger.info("$MODID is initialized.")
}
}
全部工作完成後,回到專案根目錄,執行 ./gradlew runClient
啟動遊戲。可以看到我們的示例模組已被正常載入:
專案原始碼
本文示例專案原始碼已上傳 GitHub 供讀者參考,使用 MIT 協議開源。