注册

注册

一个为你带来带注解注册表的库!

基础库

Registed

实现

首先,通过 gradle 实现 Registed:

使用 Groovy
build.gradle
repositories {
	maven { url "https://api.modrinth.com/maven" }
}

dependencies {
	modApi "maven.modrinth:registed:$project.registed_version"
}
gradle.properties
registed_version=?

将 ? 替换为 Registed 最新的 tag name。

使用 Kotlin
build.gradle.kts
repositories {
	maven("https://api.modrinth.com/maven")
}

dependencies {
	modApi("maven.modrinth:registed:${property("registedVersion")}")
}
gradle.properties
registedVersion=?

将 ? 替换为 Registed 最新的 tag name。

接下来,在 fabric.mod.json 中将 Registed 引用为依赖项:

fabric.mod.json
{
  // Other contents

  "depends": {
    "registed": "*"
  },

  // Other contents
}

Registed 不应被嵌套在你的项目中。你应始终将 Registed 视为一个独立的模组。

用法

要注册一个对象,请执行:

full.path.to.ClassName.java
@RegistryId("modid")
public class ClassName {
	@ho.artisan.registed.annotation.registries.Item
	public static final Item ITEM = ...;
}

你必须使用 @RegistryID^@RegistryID_classpath 注解你的注册类,以告知 Registed 使用哪个命名空间。

之后,记得在 fabric.mod.json 中引用该类:

fabric.mod.json
{
  // Other contents

  "entrypoints": {
    "registed": [
      "full.path.to.ClassName"
    ]
  },

  // Other contents
}

Registed 支持 Registries^Registries_classpath 类中所有 Registry<?> 类型。你应将目标注册表类型名称从 UPPER_SNAKE_CASE 转换为 CamelCase 以找到所需的注解,例如,Registries.LOOT_NBT_PROVIDER_TYPE 对应的注解为 @LootNBTProviderType。

所有用于注册对象的可用注解都位于包 ho.artisan.registed.annotation.registries 下。

这些注解只能应用于字段,并且你应始终确保注册对象为 static 和 final 字段。

默认情况下,注册对象将使用其字段名以 snake_case 形式作为其在游戏内的标识符路径。例如,字段 MY_EXAMPLE_ITEM 的标识符路径为 my_example_item。我们建议使用 UPPER_SNAKE_CASE 命名字段,因为字段应为 static 和 final,且只有在 snake case 下 Registed 才会保留这些下划线。

要自定义注册对象在游戏内的标识符路径,你可以为目标字段应用注解 @RegistryID^@RegistryID_classpath:

@RegistryID("my_item")
@ho.artisan.registed.annotation.registries.Item
public static final Item ITEM = ...;

@RegistryID[@^RegistryID_classpath] 提供的标识符路径将被原样使用,不做任何进一步处理。