mc-kotlin · v1.20.1 · 2026-08-06 · sha256 7befa8370f6a0b3d
mc-kotlin v1.20.1A
Immutable. This exact content is served forever at /api/v1/blob/7befa8370f6a0b3d.
---
name: mc-kotlin
description: Fabric Kotlin 语言支持。fabric-language-kotlin、kotlin("jvm")、@PublishedApi。触发词:Kotlin、fabric-language-kotlin、build.gradle.kts
platform: fabric
version: "1.20.1"
dependencies: []
mappings: yarn
---
# Kotlin 语言支持(Fabric 1.20.1)
## 概述
Fabric 官方支持 Kotlin,通过 `fabric-language-kotlin` 和 Gradle Kotlin DSL 实现。
## 添加 Kotlin 支持
### 1. build.gradle.kts
```kotlin
plugins {
kotlin("jvm") version "1.9.20"
id("fabric-loom") version "1.4-SNAPSHOT"
id("maven-publish")
}
val minecraft_version: String by project
val yarn_mappings: String by project
val loader_version: String by project
val fabric_api_version: String by project
loom {
accessWidenerPath.set(file("src/main/resources/examplemod.accesswidener"))
kotlin {
minecraftVersion.set(minecraft_version)
yarnMappings.set(v2(yarn_mappings))
}
}
repositories {
mavenCentral()
maven(url = "https://maven.fabricmc.net/")
}
dependencies {
minecraft("com.mojang:minecraft:${minecraft_version}")
mappings(v2(yarn_mappings))
modImplementation("net.fabricmc:fabric-loader:${loader_version}")
modImplementation("net.fabricmc:fabric-language-kotlin:1.10.0+kotlin.1.9.20")
modApi("net.fabric.sdk:fabric-api:${fabric_api_version}")
}
tasks.processResources {
inputs.property("version", project.version)
filesMatching("fabric.mod.json") {
expand("version" to project.version)
}
}
```
### 2. fabric.mod.json 中的 Kotlin 依赖
```json
{
"depends": {
"fabric-language-kotlin": ">=1.10.0"
}
}
```
## Kotlin 代码示例
```kotlin
// ExampleMod.kt
@AutoStorageAware
class ExampleMod : FabricMod {
override val modId = "examplemod"
override val modName = "Example Mod"
override val version = "1.0.0"
companion object {
val LOG = Logger.getLogger(modId)
}
override fun onInitialize() {
LOG.info("Hello Fabric with Kotlin!")
// Registry.register 用法相同
}
}
```
## @PublishedApi 注解
用于暴露私有成员的公共 API:
```kotlin
// @PublishedApi 用于在 lambda 中安全访问私有字段
class MyItem(settings: Settings) : Item(settings) {
companion object {
val MY_ITEM: RegistrySupplier<Item> = Registry.register(
Registries.ITEM,
Identifier(MOD_ID, "my_item"),
MyItem(Settings())
)
}
// @PublishedApi 用于在 lambda 中调用
private companion object {
private const val MOD_ID = "examplemod"
}
}
```
## 常见错误
- ❌忘记 `fabric-language-kotlin` 依赖 — Kotlin 代码无法运行
- ❌ Kotlin 版本与 fabric-language-kotlin 不匹配 — 兼容版本参考官方文档
- ❌ 在 `onInitialize()` 中使用 Kotlin 协程 — Minecraft 服务端不是多线程,协程需谨慎
## 扩展点
| 配合 Skill | 协作说明 |
|-----------|---------|
| `mc-registry` | Kotlin 语法糖使注册更简洁 |
| `mc-item` | Kotlin data class 用于物品数据 |
| `mc-block` | Kotlin 扩展函数简化方块逻辑 |