User Tools

Site Tools


zh_cn:tutorial:model_predicate_providers

This is an old revision of the document!


模型谓词提供器

介绍

模型谓词提供器用于动态改变Model providers are used to dynamically change the model of items based on data from ItemStacks. 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的提供器冲突。这些必须在你的ClientModInitializeronInitializeClient方法中完成。如果你没有ClientModInitializer,可以看看入口点教程以添加一个。

  1. FabricModelPredicateProviderRegistry.register(EXAMPLE_BOW, new Identifier("pull"), (itemStack, clientWorld, livingEntity) -> {
  2. if (livingEntity == null) {
  3. return 0.0F;
  4. }
  5. return livingEntity.getActiveItem() != itemStack ? 0.0F : (itemStack.getMaxUseTime() - livingEntity.getItemUseTimeLeft()) / 20.0F;
  6. });
  7.  
  8. FabricModelPredicateProviderRegistry.register(EXAMPLE_BOW, new Identifier("pulling"), (itemStack, clientWorld, livingEntity) -> {
  9. if (livingEntity == null) {
  10. return 0.0F;
  11. }
  12. return livingEntity.isUsingItem() && livingEntity.getActiveItem() == itemStack ? 1.0F : 0.0F;
  13. });

ModelPredicateProvider时功能性的接口,需要一个ItemStack作为当前被渲染的物品堆,一个ClientWorld作为客户端所在的当前世界,以及一个LivingEntity作为物品的用户(在1.17+,还需要接收一个int)。

所有的ModelPredicateProvider返回浮点数,用来代表模型的不同状态,或者通过返回1.0f0f来分别表示真假值。

仔细看看pull谓词,我们会看到我们首先检查实体是否无效(null),因为物品可以在其使用的环境之外渲染(例如掉落在地上)。我们确保实体使用我们的物品,否则物品会让模型谓词在不被使用的时候应用。最后,我们将itemStack.getMaxUseTime()减去livingEntity.getItemUseTimeLeft()以表示物品所被持有的刻数。因为完全拉满一把弓需要20刻即1秒的时间,所以我们将这个数字除以20.0f以使其介于0f1.0f之间来表示正常的拉伸进度。

所有这些都是有用的,但为了让我们的物品改变其模型,我们只需要实现其一部分的特性。

  1. "overrides": [
  2. {
  3. "predicate": {
  4. "pulling": 1
  5. },
  6. "model": "tutorial:item/example_bow_pulling_0"
  7. },
  8. {
  9. "predicate": {
  10. "pulling": 1,
  11. "pull": 0.65
  12. },
  13. "model": "tutorial:item/example_bow_pulling_1"
  14. },
  15. {
  16. "predicate": {
  17. "pulling": 1,
  18. "pull": 0.9
  19. },
  20. "model": "tutorial:item/example_bow_pulling_2"
  21. }
  22. ]

Minecraft运作的方式是,在指定的值中,检查模型大于等于的值的最后一个值。所以拉伸值为0.8时,会使用tutorial:item/example_bow_pulling_1,而值为0.5时使用tutorial:item/example_bow_pulling_0

zh_cn/tutorial/model_predicate_providers.1627178123.txt.gz · Last modified: 2021/07/25 01:55 by solidblock