Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bb25e55
提交注册条目时保存元数据并拒绝重复 ID
QiuYe-QAQ Nov 20, 2026
0e20777
按输出目录统一 DataGen 调度和语言文件生成
QiuYe-QAQ Nov 20, 2026
b416db1
接入类型化本地化声明和条目元数据视图
QiuYe-QAQ Nov 20, 2026
c1b732b
UpData
QiuYe-QAQ Nov 20, 2026
d7e348b
通过编译期索引收集 Trans 和 TransVariants 声明
QiuYe-QAQ Nov 20, 2026
48f3822
将本地化注解处理器迁入 Gradle 子模块
QiuYe-QAQ Nov 20, 2026
47d9f57
补充 1.21.1 本地化接入和迁移说明
QiuYe-QAQ Nov 20, 2026
9be432c
UpData
QiuYe-QAQ Nov 20, 2026
802bf34
迁移本地化解析到 Kotlin 并减少候选处理开销
QiuYe-QAQ Nov 20, 2026
e807c61
将本地化声明与 Java 注解处理器迁移到 Kotlin
QiuYe-QAQ Nov 20, 2026
7f26dbe
为 Kotlin 本地化声明接入 KSP
QiuYe-QAQ Nov 20, 2026
5e9acb7
迁移注册条目与 NeoForge 生命周期到 Kotlin
QiuYe-QAQ Nov 20, 2026
9e41c46
将 DataGen 调度与 Provider 类型迁移到 Kotlin
QiuYe-QAQ Nov 20, 2026
32c1a25
迁移标签、语言和服务端 DataGen Provider 到 Kotlin
QiuYe-QAQ Nov 20, 2026
91d0a6e
将模型、配方与战利品生成器迁移到 Kotlin
QiuYe-QAQ Nov 20, 2026
e95e4e5
迁移注册追踪和注册辅助类型到 Kotlin
QiuYe-QAQ Nov 20, 2026
f4738fa
将 Registrate 注册与 DataGen 入口迁移到 Kotlin
QiuYe-QAQ Nov 20, 2026
d7c240f
迁移注册 Builder 的基础契约与声明逻辑
QiuYe-QAQ Nov 20, 2026
37e0d28
迁移具体注册 Builder 到 Kotlin
QiuYe-QAQ Nov 20, 2026
59ff797
让测试模组共用 Registrate 并覆盖 Java 与 Kotlin 注册
QiuYe-QAQ Nov 20, 2026
df9d68d
将注册回调 SAM 和空值注解迁移到 Kotlin
QiuYe-QAQ Nov 20, 2026
6867edb
简化 Kotlin 注册 Builder 与回调写法
QiuYe-QAQ Nov 20, 2026
26dc106
迁移 Provider 更新工具并移除 Lombok 构建配置
QiuYe-QAQ Nov 20, 2026
4a68cdf
补充 Java 与 Kotlin 共用注册入口的迁移说明
QiuYe-QAQ Nov 20, 2026
520f83f
保留流体供应器重载的类型注册行为
QiuYe-QAQ Nov 20, 2026
36dc0cd
UpData
QiuYe-QAQ Nov 21, 2026
f337662
统一 Kotlin Builder 格式
QiuYe-QAQ Nov 21, 2026
4ab3e28
spotless
QiuYe-QAQ Nov 21, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ classes/
.settings
*.launch
.architectury-transformer/debug.log
**/resources/.cache/
**/resources/

*.patch
*.DS_Store
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ This repository is the `com.modularmc.registrate` fork maintained for the Modula
First, create a `Registrate` object which will be used across your entire project.

```java
public static final Registrate REGISTRATE = Registrate.create(MOD_ID);
public static final Registrate REGISTRATE = Registrate.create(MOD_ID).sourceLocale(MinecraftLocale.EN_US);
```

不一定要使用常量字段,可以在注册设置完成后传递或丢弃。
Expand All @@ -77,7 +77,8 @@ Using a constant field is not necessary, it can be passed around and thrown away
If declared static in your `@Mod` class, you must create the `Registrate` object lazily so it is not created too early during loading. This can be done easily like so:

```java
public static final NonNullSupplier<Registrate> REGISTRATE = NonNullSupplier.lazy(() -> Registrate.create(MOD_ID));
public static final NonNullSupplier<Registrate> REGISTRATE =
NonNullSupplier.lazy(() -> Registrate.create(MOD_ID).sourceLocale(MinecraftLocale.EN_US));
```

接下来,开始添加对象。
Expand Down Expand Up @@ -119,6 +120,8 @@ public static final BlockEntry<MyStairsBlock> MY_STAIRS = REGISTRATE.block("my_s
这个自定义版本会创建 BlockItem(含默认模型和语言条目)、将方块添加到标签、配置楼梯属性方块状态,并添加自定义本地化。
This customized version will create a BlockItem (with its own default model and lang entry), add the block to a tag, configure the blockstate for stair properties, and add a custom localization.

源语言、译文和注解处理器的接入方式见 [本地化与迁移](docs/localization.md)。

<!-- 有关不同 API 和方法的概述,请查阅 [Javadocs](https://ci.tterrag.com/job/Registrate/job/1.21/javadoc/)。 -->
有关不同 API 和方法的概述,请查阅 Javadocs(待定)。更高级的用法请阅读 [维基](https://github.com/ModularMCLib/RegistrateLib/wiki)(编写中)。
<!-- To get an overview of the different APIs and methods, check out the [Javadocs](https://ci.tterrag.com/job/Registrate/job/1.21/javadoc/). -->
Expand Down
20 changes: 13 additions & 7 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import org.jetbrains.kotlin.gradle.dsl.JvmDefaultMode

plugins {
id 'java-library'
id 'org.jetbrains.kotlin.jvm'
id 'eclipse'
id 'idea'
id 'maven-publish'
alias libs.plugins.modDevGradle
//alias libs.plugins.shadow
alias libs.plugins.spotless
alias libs.plugins.lombok
alias libs.plugins.kotlin.jvm
alias libs.plugins.ksp
}

group = maven_group
Expand All @@ -20,6 +22,13 @@ java {
toolchain.languageVersion = JavaLanguageVersion.of(21)
}

kotlin {
jvmToolchain(21)
compilerOptions {
jvmDefault = JvmDefaultMode.NO_COMPATIBILITY
}
}

sourceSets {
// Include resources generated by data generators.
main.resources {
Expand Down Expand Up @@ -78,16 +87,13 @@ apply from: "$rootDir/gradle/scripts/resources.gradle"
apply from: "$rootDir/gradle/scripts/publishing.gradle"
apply from: "$rootDir/gradle/scripts/spotless.gradle"


tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
options.compilerArgs << '-Xmaxerrs' << '2000'
options.compilerArgs << "-Xlint:-removal"
}

tasks.named('test') {
// The test source set is exercised by NeoForge's GameTest server.
failOnNoDiscoveredTests = false
}

lombok {
version = "1.18.38"
}
9 changes: 9 additions & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ dependencies {
compileOnly(libs.jetbrains.annotations)
compileOnly(libs.jspecify)

jarJar(api(libs.kotlin.stdlib.get())) {
exclude group: 'org.jetbrains', module: 'annotations'
}
additionalRuntimeClasspath(libs.kotlin.stdlib.get()) {
exclude group: 'org.jetbrains', module: 'annotations'
}

testImplementation(libs.testframework.get())
testAnnotationProcessor(project(':localization-processor'))
kspTest(project(':localization-processor'))

// Standard runtime mods //
localRuntime(libs.testframework)
Expand Down
4 changes: 2 additions & 2 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class MyMod {

// 简单方式(注意:需确保不在构造之前访问)
// Simple approach (ensure it's not accessed before construction)
public static final Registrate REGISTRATE = Registrate.create(MOD_ID);
public static final Registrate REGISTRATE = Registrate.create(MOD_ID).sourceLocale(MinecraftLocale.EN_US);

public MyMod(IEventBus modEventBus) {
// 在此处注册事件监听器
Expand All @@ -55,7 +55,7 @@ If you need lazy initialization to avoid creating too early during class loading

```java
public static final NonNullSupplier<Registrate> REGISTRATE =
NonNullSupplier.lazy(() -> Registrate.create(MOD_ID));
NonNullSupplier.lazy(() -> Registrate.create(MOD_ID).sourceLocale(MinecraftLocale.EN_US));
```

## 注册你的第一个对象 / Registering Your First Object
Expand Down
3 changes: 2 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RegistrateLib is a powerful fluent registration API wrapper for creating and reg

```java
// 创建 Registrate 实例 / Create a Registrate instance
public static final Registrate REGISTRATE = Registrate.create(MOD_ID);
public static final Registrate REGISTRATE = Registrate.create(MOD_ID).sourceLocale(MinecraftLocale.EN_US);

// 注册一个简单的方块 / Register a simple block
public static final BlockEntry<Block> MY_BLOCK = REGISTRATE.block("my_block", Block::new)
Expand All @@ -38,6 +38,7 @@ public static final ItemEntry<Item> MY_ITEM = REGISTRATE.item("my_item", Item::n
3. [注册条目](entries.md) — 注册返回的强类型句柄 / Strongly-typed registry entry handles
4. [数据提供者](providers.md) — 数据生成系统详解 / Data generation system in detail
5. [高级用法](advanced.md) — 自定义注册表、生命周期钩子等 / Custom registries, lifecycle hooks, and more
6. [本地化与迁移](localization.md) / Localization and migration

## 架构概述 / Architecture Overview

Expand Down
68 changes: 68 additions & 0 deletions docs/localization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 1.21.1 本地化与迁移

## 配置源语言

为每个生成语言数据的模组设置 `MinecraftLocale`。把原有 `.lang(String)` 和 `addRawLang` 文本实际使用的语言设为源语言。`defaultLang()` 从注册路径生成英文名称,适用于 `EN_US`。其他源语言的对象可用 `.lang(String)` 或 `.localization(...)` 声明源文本。

```java
import com.modularmc.registrate.Registrate;
import com.modularmc.registrate.localization.MinecraftLocale;

Registrate registrate = Registrate.create(MOD_ID).sourceLocale(MinecraftLocale.EN_US);
```

## 声明文本

物品、方块、实体和流体的 Builder 从注册对象取 Minecraft 原生翻译 Key。通用 Builder 可用 `.localization(keyFunction, text -> ...)` 指定取 Key 的方式。

```java
registrate.item("copper_ingot", Item::new)
.localization(text -> text
.source("Copper Ingot")
.domain(Domain.MATERIALS)
.type(TextType.MATERIAL)
.translation(MinecraftLocale.ZH_CN, "铜锭"))
.register();

var title = registrate.addLocalization(new TranslationKey(MOD_ID, "gui.mymod.voltage"),
text -> text.source("Voltage: %s V").translation(MinecraftLocale.ZH_CN, "电压:%1$s V"));
```

`addLocalization` 返回可用于 GUI 的翻译组件。进度标题等跨侧文本可在模组构造期间声明,在服务端 DataGen 的进度回调中引用组件。`addLang`、`addRawLang` 和 `ProviderType.LANG` 回调都使用同一个源 Locale。

在 `registrate.localizationCatalog()` 中登记 `Domain`、`GlossaryTerm` 和 `Unit`。Builder 的 `.profile(profile)` 可复用领域、文本类型和术语。条目的显式参数覆盖 Profile 的相应字段。

## 接入注解处理器

用 `./gradlew :localization-processor:jar` 构建与库版本对应的处理器。Java 源码使用 `annotationProcessor`,Kotlin 源码使用 KSP 插件和 `ksp`。混合项目可同时使用两项配置,主库作为模组依赖。

```groovy
dependencies {
def processor = files("libs/registratelib-1.21.1-${registratelib_version}-localization-processor.jar")
annotationProcessor(processor)
ksp(processor)
}
```

`@Trans` 和 `@TransVariants` 可以声明在类型或字段上。`modId` 选择声明归属的模组和源 Locale。省略 `namespace` 时,资源命名空间取 `modId`。

```java
@TransVariants(modId = "mymod", keyPrefix = "gui.mymod.power", value = {
@Variant(id = "low", source = "Low power"),
@Variant(id = "high", source = "High power")
})
final class Labels {
@Trans(modId = "mymod", key = "gui.mymod.container", source = "Test Container",
translations = @Translation(locale = MinecraftLocale.ZH_CN, text = "测试容器"))
static final String CONTAINER = "gui.mymod.container";
}
```

Kotlin 中在带有后备字段的属性上使用 `@field:Trans` 或 `@field:TransVariants`。两个处理器按 Mod ID 写入各自的版本化声明索引,DataGen 从所属模组读取并合并。变体的 Key 由前缀和 ID 拼接,例如 `gui.mymod.power.low`。同一命名空间、Key 和 Locale 下,同级异值声明会报告双方来源。`%s` 的隐式索引按出现顺序分配,译文可用 `%1$s` 等显式索引重排。

## 注册兼容性

- `RegistryEntry.metadata()` 提供条目的本地化声明和 DataGen 回调视图。读取本地化声明所引用的原生 Key 时,目标 Holder 需要已绑定。
- 已有样例沿用原注册 ID 和原生翻译 Key。BlockItem 复用方块名称时,复用同一个 Key。
- 混合源码项目可在 Java 的 `@Mod` 入口创建 `Registrate` 并把它传给 Kotlin 注册类。测试模组用同一实例注册 `testmod:magic_item_model`,GameTest 从 Java 入口的实例查找该方块,并核对 BlockItem 的 `block.testmod.magic_item_model`。
- 同一个 Registrate 实例内,同一注册表的重复 ID 会在提交时失败。不同注册表可以使用相同的名称。
5 changes: 3 additions & 2 deletions docs/providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ REGISTRATE.block("my_block", MyBlock::new)

## RegistrateLangProvider —— 语言提供者 / Lang Provider

自动生成 `en_us` 和 `en_ud`(上下颠倒)语言文件。
Automatically generates `en_us` and `en_ud` (upside-down) lang files.
按模组的源 Locale 和显式译文生成语言文件。由 `en_us` 条目派生 `en_ud`(上下颠倒)文本。 [本地化与迁移](localization.md)

Generates language files from the mod's source locale and explicit translations. Derives upside-down text from `en_us` entries.

```java
// 通过 Registrate 添加语言条目 / Add lang entries via Registrate
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ org.gradle.caching = true
# Mod Info
mod_id = registratelib
mod_name = RegistrateLib
mod_version = 2.0.1
mod_version = 2.0.0
mod_description =
mod_license = Mozilla Public License Version 2.0
mod_url = https://github.com/ModularMCLib/Registrate-Lib/
Expand Down
12 changes: 8 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ neoForge = "21.1.265"
loader = "4"
parchment = "2024.11.17" # https://parchmentmc.org/docs/getting-started
shadow = "9.6.1"
spotless = "8.10.4"
modDevGradle = "2.0.147"
lombok = "9.8.2"
spotless = "8.11.0"
modDevGradle = "2.0.148"
kotlin = "2.4.21"
ksp = "2.3.14"
jetbrains-annotations = "26.1.0"
jspecify = "1.0.1"
renderNurse = "0.0.26"
Expand All @@ -16,6 +17,8 @@ mixin = "0.8.7"
minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" }
neoForge = { module = "net.neoforged:neoforge", version.ref = "neoForge" }
testframework = { module = "net.neoforged:testframework", version.ref = "neoForge" }
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
ksp-api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
renderNurse = { module = "net.neoforged:render-nurse", version.ref = "renderNurse" }
mixin = { module = "org.spongepowered:mixin", version.ref = "mixin" }
jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "jetbrains-annotations" }
Expand All @@ -25,4 +28,5 @@ jspecify = { module = "org.jspecify:jspecify", version.ref = "jsp
modDevGradle = { id = "net.neoforged.moddev", version.ref = "modDevGradle" }
shadow = { id = "com.gradleup.shadow", version.ref = "shadow" }
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
lombok = { id = "io.freefair.lombok", version.ref = "lombok" }
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
12 changes: 4 additions & 8 deletions gradle/scripts/jars.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ jar {
exclude "**/SimplifiedToTraditional.properties"
}

tasks.named('assemble') {
dependsOn(':localization-processor:jar')
}

tasks.register('slimJar', Jar) {
dependsOn("classes")
archiveClassifier = "slim"
Expand All @@ -13,14 +17,6 @@ java {
withSourcesJar()
}

// Use delombok'd files for sources
sourcesJar {
exclude {
sourceSets.main.allSource.contains it.file
}
from delombok
}

base {
archivesName = "${project.name}-${libs.versions.minecraft.get()}"
}
Expand Down
7 changes: 4 additions & 3 deletions gradle/scripts/moddevgradle.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ neoForge {

gameDirectory.set(file('run/data'))
// Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources.
programArguments.addAll('--mod', devTestModId)
programArguments.addAll('--all')
programArguments.addAll('--output', file('src/test/generated/resources/').getAbsolutePath())
programArguments.addAll('--mod', "${devTestModId},localetest", '--flat')
programArguments.add(providers.gradleProperty('testDataSide').orElse('all').map { "--${it}".toString() })
programArguments.add('--output')
programArguments.add(providers.gradleProperty('testDataOutput').orElse('src/test/generated/resources').map { file(it).absolutePath })
programArguments.addAll('--existing', file('src/test/resources/').getAbsolutePath())
}

Expand Down
3 changes: 2 additions & 1 deletion gradle/scripts/repositories.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
repositories {
mavenLocal()
mavenCentral()
// force gradle to download FastUtil & LWJGL from the central maven so we can get source artifacts
// Resolve libraries hosted on Maven Central without querying mod repositories.
exclusiveContent { // Force
forRepository { mavenCentral() }
filter {
includeGroup("it.unimi.dsi")
includeGroup("org.lwjgl")
includeGroup("com.google.guava")
includeGroup("com.google.devtools.ksp")
includeGroupAndSubgroups("io.netty")
includeGroupAndSubgroups("org.jetbrains")
}
Expand Down
12 changes: 10 additions & 2 deletions gradle/scripts/spotless.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spotless {
endWithNewline()
}
java {
target 'src/main/java/**/*.java', 'src/test/java/**/*.java'
target 'src/*/java/**/*.java', 'localization-processor/src/main/java/**/*.java'

def orderFile = file("$rootDir/gradle/spotless/spotless.importorder")
def formatFile = file("$rootDir/gradle/spotless/spotless.eclipseformat.xml")
Expand All @@ -23,12 +23,20 @@ spotless {
eclipse('4.31').configFile(formatFile)
}
kotlin {
target'src/main/java/**/*.kt', 'src/test/java/**/*.kt'
target 'src/*/java/**/*.kt', 'localization-processor/src/*/java/**/*.kt'

def orderFile = file("$rootDir/gradle/spotless/spotless.ktlint")
ktlint().setEditorConfigPath(orderFile)
.editorConfigOverride([
'ktlint_standard_no-unused-imports': 'enabled',
])

toggleOffOn()
replaceRegex(
'empty Kotlin function bodies',
/(?m)(fun[^\n{=]*\([^)]*\)(?:\s*:\s*[^\n{=]+)?\s*)\{\r?\n[ \t]*\}/,
'$1{}'
)
trimTrailingWhitespace()
leadingSpacesToTabs()
endWithNewline()
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.0-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-9.9.1-bin.zip
networkTimeout=10000
retries=0
retryBackOffMs=500
Expand Down
Loading
Loading