BlockEntityWithoutLevelRenderer
BlockEntityWithoutLevelRenderer(BEWLR)用于处理物品的动态渲染。它比旧的 ItemStack 系统简单得多;旧系统需要 BlockEntity,且无法访问 ItemStack。
原创说明图,依据 Forge 1.21.x 官方文档 绘制。
使用 BlockEntityWithoutLevelRenderer
BEWLR 允许使用以下方法渲染物品:
public void renderByItem(ItemStack itemStack, ItemDisplayContext ctx, PoseStack poseStack, MultiBufferSource bufferSource, int combinedLight, int combinedOverlay)
要使用 BEWLR,首先物品模型的 BakedModel#isCustomRenderer 必须返回 true。如果没有自己的渲染器,它将使用默认的 ItemRenderer#getBlockEntityRenderer。返回 true 后,将访问物品的 BEWLR 进行渲染。
注意:如果
Block#getRenderShape设为RenderShape#ENTITYBLOCK_ANIMATED,Block也会使用 BEWLR 渲染。
要为物品设置 BEWLR,必须在 Item#initializeClient 中消费一个匿名 IClientItemExtensions 实例,并在其中重写 IClientItemExtensions#getCustomRenderer,返回你的 BEWLR 实例:
// 在物品类中
@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
consumer.accept(new IClientItemExtensions() {
@Override
public BlockEntityWithoutLevelRenderer getCustomRenderer() {
return myBEWLRInstance;
}
});
}
重要:每个 Mod 应当只有一个自定义 BEWLR 实例。
使用 BEWLR 不需要其他设置。