RegistryKey
这一页是签名与行为参考。注册内容的身份键,命名空间必须等于模组 id。直接做一遍:教程 · 添加判定等级。
注册表键:ResourceLocation + 值类型,是注册内容的唯一身份(单源头)。被注册的值对象不自带 id,因此「键与对象自带 id 不一致」这类错误在结构上不可能出现。类型参数 T 让「往音符类型注册表塞游戏模式」这类错误在编译期就被拦下。
两个构造器都是私有的,只能经 of(...) / parse(...) 工厂创建——这样每次创建都会走格式校验与 type 非空校验。
方法速览
| 方法 | 用途 |
|---|---|
of(namespace, path, type) | 由命名空间、路径与值类型构造 |
of(location, type) | 由资源标识与值类型构造 |
parse(id, type) | 解析 namespace:path(可省略命名空间) |
location() | 取资源标识 |
type() | 取值类型 |
toString() | 返回 namespace:path |
equals(o) / hashCode() | 基于 location 与 type 的身份比较 |
完整签名
public final class RegistryKey<T> {
public static <T> RegistryKey<T> of(String namespace, String path, Class<T> type);
public static <T> RegistryKey<T> of(ResourceLocation location, Class<T> type);
public static <T> RegistryKey<T> parse(String id, Class<T> type);
public ResourceLocation location();
public Class<T> type();
public String toString();
public boolean equals(Object o);
public int hashCode();
}of(namespace, path, type)
签名:
public static <T> RegistryKey<T> of(String namespace, String path, Class<T> type);行为:先按 ResourceLocation 的规则校验并构造标识(namespace / path 格式非法,或为 null/空,抛 IllegalArgumentException),再校验 type 非 null(否则 IllegalArgumentException("非法注册表键:type 不能为 null"))。namespace 的约定值就是模组 id,内置内容用 seecmd。
示例:
RegistryKey<NoteTypeDefinition> key =
RegistryKey.of("mymod", "slide", NoteTypeDefinition.class);of(location, type)
签名:
public static <T> RegistryKey<T> of(ResourceLocation location, Class<T> type);行为:复用已有的资源标识。location 为 null 抛 IllegalArgumentException(先于 type 校验),type 为 null 同样抛 IllegalArgumentException。适合「id 已在别处构造好」或「从配置/谱面读到的 id」场景。
示例:
ResourceLocation id = ResourceLocation.of("mymod", "slide");
RegistryKey<NoteTypeDefinition> key = RegistryKey.of(id, NoteTypeDefinition.class);parse(id, type)
签名:
public static <T> RegistryKey<T> parse(String id, Class<T> type);行为:把 namespace:path 字符串解析成键。不含 : 时用默认命名空间 seecmd;空串、含多个 :、或任一段格式非法抛 IllegalArgumentException。解析先于 type 校验发生,因此 id 非法时报的是 id 的错误。type 为 null 抛 IllegalArgumentException。
示例:
RegistryKey<NoteTypeDefinition> hold = RegistryKey.parse("seecmd:hold", NoteTypeDefinition.class);
RegistryKey<NoteTypeDefinition> mine = RegistryKey.parse("slide", NoteTypeDefinition.class);location()
签名:
public ResourceLocation location();行为:返回键的资源标识(namespace:path)。运行期需要 id 时用它,不要自己拼字符串。
示例:
ResourceLocation id = key.location();
String namespace = id.namespace();type()
签名:
public Class<T> type();行为:返回键在创建时声明的值类型。注册表用它做类型收敛与类型匹配校验(见 Registry#register)。
示例:
Class<NoteTypeDefinition> t = key.type();toString()
签名:
public String toString();行为:返回 location.toString(),即 namespace:path,与 location() 的字符串形式完全一致。日志与异常文案里直接打印键即可。
示例:
ctx.getLogger().info("registering " + key);equals(o) / hashCode()
签名:
public boolean equals(Object o);
public int hashCode();行为:equals 只在 location 与 type 都相同时返回 true(同一 id、不同类型是两个不同的键);hashCode 为 31 * location.hashCode() + type.hashCode(),两者可安全用作 Map / Set 的键。注意注册表的重复判定按 location 进行,与 equals 口径不同。
示例:
Map<RegistryKey<?>, String> names = new HashMap<>();
names.put(key, "slide");线程:不可变类型,构造完成后可跨线程共享。
生命周期阶段:在 onPreInit 内构造用于注册;作为身份标识亦可在运行期读取 location() / type()。
稳定性:2.0 起冻结:2.x 内只增不破(禁止删签名 / 改签名 / 改语义)。