This is an old revision of the document!
模型谓词提供器
介绍
模型谓词提供器用于动态改变Model providers are used to dynamically change the model of items based on data from ItemStack
s. A common example is the bow, which has different textures based on how long the bow has been pulled. All providers are then used in the model file for the item, in the overrides
section.
可行示例
在此例中,我们假定有了自定义的弓,叫做EXAMPLE_BOW
。
在其方法中,使用其物品和一个Identifier
注册一个ModelPredicateProvider
。对于Identifier
的使用,不需要提供命名空间,因为这些不会与Minecraft的提供器冲突。这些必须在你的ClientModInitializer
的onInitializeClient
方法中完成。如果你没有ClientModInitializer
,可以看看入口点教程以添加一个。
FabricModelPredicateProviderRegistry.register(EXAMPLE_BOW, new Identifier("pull"), (itemStack, clientWorld, livingEntity) -> { if (livingEntity == null) { return 0.0F; } return livingEntity.getActiveItem() != itemStack ? 0.0F : (itemStack.getMaxUseTime() - livingEntity.getItemUseTimeLeft()) / 20.0F; }); FabricModelPredicateProviderRegistry.register(EXAMPLE_BOW, new Identifier("pulling"), (itemStack, clientWorld, livingEntity) -> { if (livingEntity == null) { return 0.0F; } return livingEntity.isUsingItem() && livingEntity.getActiveItem() == itemStack ? 1.0F : 0.0F; });
ModelPredicateProvider
时功能性的接口,需要一个ItemStack
作为当前被渲染的物品堆,一个ClientWorld
作为客户端所在的当前世界,以及一个LivingEntity
作为物品的用户(在1.17+,还需要接收一个int
)。
所有的ModelPredicateProvider
返回浮点数,用来代表模型的不同状态,或者通过返回1.0f
和0f
来分别表示真假值。
仔细看看pull
谓词,我们会看到我们首先检查实体是否无效(null),因为物品可以在其使用的环境之外渲染(例如掉落在地上)。我们确保实体使用我们的物品,否则物品会让模型谓词在不被使用的时候应用。最后,我们将itemStack.getMaxUseTime()
减去livingEntity.getItemUseTimeLeft()
以表示物品所被持有的刻数。因为完全拉满一把弓需要20刻即1秒的时间,所以我们将这个数字除以20.0f
以使其介于0f
到1.0f
之间来表示正常的拉伸进度。
所有这些都是有用的,但为了让我们的物品改变其模型,我们只需要实现其一部分的特性。
"overrides": [ { "predicate": { "pulling": 1 }, "model": "tutorial:item/example_bow_pulling_0" }, { "predicate": { "pulling": 1, "pull": 0.65 }, "model": "tutorial:item/example_bow_pulling_1" }, { "predicate": { "pulling": 1, "pull": 0.9 }, "model": "tutorial:item/example_bow_pulling_2" } ]
Minecraft运作的方式是,在指定的值中,检查模型大于等于的值的最后一个值。所以拉伸值为0.8
时,会使用tutorial:item/example_bow_pulling_1
,而值为0.5
时使用tutorial:item/example_bow_pulling_0
。