Registrar
一个用于 Minecraft 模组开发的注册表辅助工具
将 Registrar 添加到你的项目
Modrinth Maven
如果你的 repositories 块中还没有 Modrinth maven,请按如下方式添加:
build.gradle:
maven {
url("https://api.modrinth.com/maven")
content {
includeGroupAndSubgroups("maven.modrinth")
}
}
或者 build.gradle.kts:
maven("https://api.modrinth.com/maven") {
content {
includeGroupAndSubgroups("maven.modrinth")
}
}
然后,将此项目添加到你的依赖项中。
如果你使用的是未混淆的 Minecraft:
api("maven.modrinth:registrar:${project.registrar_version}") // 或 implementation
include("maven.modrinth:registrar:${project.registrar_version}")
如果你使用的是混淆的 Minecraft:
modApi("maven.modrinth:registrar:${project.registrar_version}") // 或 modImplementation
include("maven.modrinth:registrar:${project.registrar_version}")
mavenLocal
首先,fork 或下载此仓库并将其发布到你的 mavenLocal。
然后,在 repositories 块中添加以下内容:
mavenLocal()
然后,将此项目添加到你的依赖项中。
如果你使用的是未混淆的 Minecraft:
api("survivalblock.atmosphere:registrar:${project.registrar_version}") // 或 implementation
include("survivalblock.atmosphere:registrar:${project.registrar_version}")
如果你使用的是混淆的 Minecraft:
modApi("survivalblock.atmosphere:registrar:${project.registrar_version}") // 或 modImplementation
include("survivalblock.atmosphere:registrar:${project.registrar_version}")
常规注册器 (Registrants)
首先,创建一个所需类型的 Registrant 实例,如下所示:
private static final BlockRegistrant BLOCK_REGISTRANT = new BlockRegistrant(ExampleMod.MOD_ID);
如果不存在所需类型的注册器,你可以创建一个!
private static final Registrant RECIPE_SERIALIZER_REGISTRANT = new Registrant(ExampleMod.MOD_ID, BuiltinRegistries.RECIPE_SERIALIZER);
如果有需要添加其他常见注册器的情况,请随时提交 issue 通知我。
要注册一个对象,只需调用 Registrant#register。
REGISTRANT.register("name", THING);
请注意,某些注册器类具有特殊的注册方法。
例如,ItemRegistrant 有一个接受三个参数的 register 方法。
ITEM_REGISTRANT.register("name", Item::new, new Item.Properties());
ItemRegistrant 还可以通过单独的 register 重载来注册 BlockItem。
更多信息,请参阅 文档。
延迟注册器 (Delayed Registrants)
所有常规注册器都有一个对应的延迟版本,可以在 delayed 包中找到。
延迟注册器与常规注册器相同,唯一的例外是:
必须调用 DelayedRegistrant#consumeAll 才能真正注册这些对象。
public static void init() {
REGISTRANT.consumeAll();
}
动态注册器 (Dynamic Registrants)
动态注册器是为动态注册表设计的注册器。
目前,基础库中仅包含针对 DamageTypes(伤害类型)和 Enchantments(附魔)的注册器。
参见以下链接以了解如何注册伤害类型和附魔的示例。
要为数据生成添加注册器,请在模组的 DataGeneratorEntrypoint 中重写 buildRegistry,如下所示:
@Override
public void buildRegistry(RegistrySetBuilder registryBuilder) {
DYNAMIC_REGISTRANT.bootstrap(registryBuilder);
}
其中 bootstrap 是 registryBuilder.addRegistry(REGISTRY_KEY, DYNAMIC_REGISTRANT::bootstrap); 的简写。
特殊注册器 (Special Registrants)
特殊注册器是没有底层 Registry 支持的注册器。它们可能扩展了 Registrant,也可能没有。(注意,在 1.21.11 之前的 GameRuleRegistrant 从技术上讲属于特殊注册器。)
目前,这仅包括 AttachmentTypeRegistrant。
文档
Registrar 提供了一些 javadoc,但如果你不想下载源 jar 以避免查看我的多版本注释,请阅读此处内容。
附加类型 (Attachment Types)
AttachmentTypeRegistrant 包含使用构建器注册或使用方法参数进行注册的重载。
方块实体类型 (Block Entity Types)
BlockEntityTypeRegistrant 和 DelayedBlockEntityTypeRegistrant 实现了 IBlockEntityTypeRegistrant,
它可以根据版本注册 BlockEntityType.Builder / FabricBlockEntityTypeBuilder。
它还可以接受方块实体的构造函数(作为 lambda 表达式)和一个 Block 可变参数数组来构造 BlockEntityType。
方块 (Blocks)
BlockRegistrant 和 DelayedBlockRegistrant 实现了 IBlockRegistrant,它可以根据构造函数及其 BlockBehaviour$Properties 注册 Blocks。请注意,BlockBehaviour$Properties#setId 会自动为你调用。
根据类型,也可以创建并获取 ItemRegistrant 或 DelayedItemRegistrant。
registerAndPresent 返回一个包含 Block 及其 ResourceKey(以及在支持版本中的 BlockItemId)的 BlockPresenter。对 BlockPresenter 使用 ConstructItem 注解将根据本文档“物品”部分列出的规则尝试进行物品注册。请注意,IItemRegistrant#registerFromAnnotations 会忽略 BlockPresenter 字段。
ConstructBlock 注解可用于 Block 或 BlockPresenter 字段,以允许在不指定名称字符串的情况下注册该方块。将此注解应用于类将对此类中的所有字段启用此行为。
在 26.2 中,提供了 createId 方法来创建 BlockItemIds。
游戏规则
由于在 1.21.11 之前不存在 BuiltinRegistries.GAME_RULE,在这些版本中 GameRuleRegistrant 的 registry 为 null。在这些版本中的注册是通过 Fabric API 的 GameRuleRegistry 完成的,它并非一个真正的 Registry。
如前所述原因,DelayedGameRuleRegistrant 在 1.21.11 之前也不存在。
GameRuleRegistrant 和 DelayedGameRuleRegistrant 均实现了 IGameRuleRegistrant,其中包含根据版本注册 GameRules$Key/GameRules 的方法。存在用于注册布尔类型规则的重载方法,但由于在 1.21.11 之前构建规则的方式差异较大,这些方法并不涵盖所有情况。建议根据需要自行编写辅助方法。
IGameRuleRegistrant 还包含一些从 GameRules 获取值的跨版本实用工具。这些工具也可以在实际的 registrants 中找到,后者会对某些规则执行拆箱操作。
物品
ItemRegistrant 和 DelayedItemRegistrant 实现了 IItemRegistrant(有趣的“I”双重含义),可以根据构造函数及其 Item$Properties 注册 Items。请注意,会自动为你调用 Item$Properties#setId。
这两个 IItemRegistrant 还能够通过其他注册重载方法注册 BlockItems。此外,还支持使用 ConstructItem 注解对 BlockItems 进行反射式注册。这需要调用 IItemRegistrant#registerFromAnnotations,该方法返回一个从 Blocks 到已注册 Items 的映射。类参数指定检查哪个类的字段,而 tryAllByDefault 则告知 registrant 即使没有注解存在也构造物品。
在 ConstructItem 中将 useBlockTranslation 设置为 true 将在 Item$Properties 上调用 useBlockDescriptionPrefix。这在 1.21.1 中是不必要的,因为它是 由 BlockItem#getDescriptionId 决定的。
你还可以通过设置 exclude = true 将物品排除在自动注册之外。当需要手动注册具有超过 Block block, Item.Properties properties 参数的构造函数(例如告示牌和旗帜)的 BlockItems 时,这很有帮助,因为这些情况不受反射式注册的支持。对于具有这些参数的构造函数的类,你可以指定类为 constructor = CustomBlockItem.class。
ConstructItem 必须注解在 public static 的 Block 或 BlockItemId 字段上。请注意,在 26.2 之后以这种方式注解 Block 字段已被弃用,并且除非你在注解中设置 suppressIdWarnings = true,否则会在你的日志中产生大量警告信息。
Registrar
A registry helper for Minecraft modding
Adding Registrar To Your Project
Modrinth Maven
If you don't have the Modrinth maven in your repositories block, add it like so:
build.gradle:
maven {
url("https://api.modrinth.com/maven")
content {
includeGroupAndSubgroups("maven.modrinth")
}
}
or build.gradle.kts:
maven("https://api.modrinth.com/maven") {
content {
includeGroupAndSubgroups("maven.modrinth")
}
}
Then, add this project to your dependencies.
If you're using deobfuscated Minecraft:
api("maven.modrinth:registrar:${project.registrar_version}") // or implementation
include("maven.modrinth:registrar:${project.registrar_version}")
If you're using obfuscated Minecraft:
modApi("maven.modrinth:registrar:${project.registrar_version}") // or modImplementation
include("maven.modrinth:registrar:${project.registrar_version}")
mavenLocal
First, fork or download this repository and publish it to your mavenLocal.
Then, adding the following to your repositories block:
mavenLocal()
Then, add this project to your dependencies.
If you're using deobfuscated Minecraft:
api("survivalblock.atmosphere:registrar:${project.registrar_version}") // or implementation
include("survivalblock.atmosphere:registrar:${project.registrar_version}")
If you're using obfuscated Minecraft:
modApi("survivalblock.atmosphere:registrar:${project.registrar_version}") // or modImplementation
include("survivalblock.atmosphere:registrar:${project.registrar_version}")
Normal Registrants
To start, create a Registrant instance of the type you need, like so:
private static final BlockRegistrant BLOCK_REGISTRANT = new BlockRegistrant(ExampleMod.MOD_ID);
If a registrant of the required type does not exist, you can create one!
private static final Registrant RECIPE_SERIALIZER_REGISTRANT = new Registrant(ExampleMod.MOD_ID, BuiltinRegistries.RECIPE_SERIALIZER);
Feel free to open an issue to notify me if there is a need to add other common registrants.
To register an object, simply call Registrant#register.
REGISTRANT.register("name", THING);
Note that some registrant classes have special register methods.
For example, ItemRegistrant has a register method that takes three parameters.
ITEM_REGISTRANT.register("name", Item::new, new Item.Properties());
ItemRegistrant is also capable of registering BlockItems via separate register overloads.
For more information, see the Documentation.
Delayed Registrants
All normal registrants have a delayed counterpart, which can be found in the delayed package.
Delayed registrants are identical to normal registrants, with one exception:
DelayedRegistrant#consumeAll must be called to actually register the objects.
public static void init() {
REGISTRANT.consumeAll();
}
Dynamic Registrants
Dynamic registrants are registrants for dynamic registries.
Currently, only registrants for DamageTypes and Enchantments exist in the base library.
See these links for examples of registering damage types and enchantments.
To add the registrants for datagen, override buildRegistry in your mod's DataGeneratorEntrypoint like so:
@Override
public void buildRegistry(RegistrySetBuilder registryBuilder) {
DYNAMIC_REGISTRANT.bootstrap(registryBuilder);
}
where bootstrap is a shorthand for registryBuilder.addRegistry(REGISTRY_KEY, DYNAMIC_REGISTRANT::bootstrap);
Special Registrants
Special registrants are registrants which have no backing Registry. They may or may not
extend Registrant. (Note that GameRuleRegistrant before 1.21.11 is technically a special
registrant.)
Currently, this only includes AttachmentTypeRegistrant.
Documentation
Registrar provides some javadocs, but if you don't want to download the source jar to avoid my multiversioned comments, read this.
Attachment Types
AttachmentTypeRegistrant contains overloads to register with the builder or register with method parameters.
Block Entity Types
BlockEntityTypeRegistrant and DelayedBlockEntityTypeRegistrant implement IBlockEntityTypeRegistrant,
which can register BlockEntityType.Builder/FabricBlockEntityTypeBuilders depending on the version. It
can also take the BE's constructor (as a lambda) and a Block varargs to construct the BlockEntityType.
Blocks
BlockRegistrant and DelayedBlockRegistrant implement IBlockRegistrant, which can register Blocks
based on a constructor and their BlockBehaviour$Properties. Note that BlockBehaviour$Properties#setId
is called for you.
An ItemRegistrant or DelayedItemRegistrant can also be created and obtained, depending on the type.
registerAndPresent returns a BlockPresenter that contains the Block and its ResourceKey (and its
BlockItemId in supported versions). Annotating a BlockPresenter with ConstructItem will attempt
Item registration according to the rules listed in the Items section of this documentation. Note that
IItemRegistrant#registerFromAnnotations ignores BlockPresenter fields.
The ConstructBlock annotation can be used on a Block or BlockPresenter field to allow registration
of the block without specifying a String for the name. Applying this to a class will unlock this behaviour
for all fields.
In 26.2, createId methods are available to create BlockItemIds.
Game Rules
Due to BuiltinRegistries.GAME_RULE not existing before 1.21.11, GameRuleRegistrant's registry is
null in those versions. Registration in those versions is done via Fabric API's GameRuleRegistry, which
is not a real Registry.
DelayedGameRuleRegistrant does not exist before 1.21.11 for reasons described above.
Both GameRuleRegistrant and DelayedGameRuleRegistrant implement IGameRuleRegistrant, which contains
methods to register GameRules$Key/GameRules depending on the version. Overloads to register a boolean
rule exist, but due to the larger variation of ways to construct rules pre-1.21.11, these are not exhaustive.
It is recommended to make your own helpers as necessary.
IGameRuleRegistrant also contains minor multiversion utilities to obtain values from GameRules. These
can also be found in the actual registrants, which perform unboxing for some rules.
Items
ItemRegistrant and DelayedItemRegistrant implement IItemRegistrant (funny double I), which can register
Items based on a constructor and their Item$Properties. Note that Item$Properties#setId is called for
you.
The two IItemRegistrants are also capable of registering BlockItems with other register overloads. In
addition, reflective registration for BlockItems is also supported with the ConstructItem annotation.
This requires the invocation of IItemRegistrant#registerFromAnnotations, which returns a map of the Blocks
to registered Items. The class parameter dictates which class's fields to check, while tryAllByDefault
will tell the registrant to construct items even if no annotation is present.
Setting useBlockTranslation to true in ConstructItem will invoke useBlockDescriptionPrefix on the
Item$Properties. This isn't necessary in 1.21.1, because it is determined by BlockItem#getDescriptionId.
You can also exclude the item from being automatically registered with exclude = true. This is helpful when
manual registration is required for BlockItems that have more than Block block, Item.Properties properties
in their constructors (such as signs and banners), because these are unsupported by the reflective registration.
For classes with a constructor with those parameters, you can specify the class with
constructor = CustomBlockItem.class.
ConstructItem must be annotated on a public static Block or BlockItemId field. Note that annotating a
Block field in this way beyond 26.2 is deprecated and will result in spammed warnings in your logs unless you
set suppressIdWarnings = true in the annotation.
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 Registrar
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
Registrar 0.2.3
- BlockPresenter 现在实现了 Supplier 接口
- 新增
BlockPresenter#getBlockItemIdOrThrow
- 新增
IGameRuleRegistrant现在可以指定一个默认分类,用于添加其GameRule- 除非使用包含分类参数的重载方法,否则将使用该默认分类
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
模组
一个用于收集生物掉落物的多区块模组。
模组
显示你正在注视的方块的信息。(适用于 Minecraft 1.16+ 的 Hwyla/Waila 分支)
模组
在游戏画面角落显示附近世界地形、玩家、生物和实体的地图。可创建路径点,帮助你找到标记过的位置。
模组
查看物品与配方
模组
使用 JavaScript 编辑配方、添加自定义物品、编写世界事件!
模组
AE2:一款流行的自动化与存储模组
评论 0
登录后参与评分
还没有评论,来抢沙发吧