User Tools

Site Tools


tutorial:model_predicate_providers

Model Predicate Providers

Introduction

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.

Practical Example

For this example, assume we have we have a custom bow item called TutorialItems.EXAMPLE_BOW.

Inside a method, we register a ModelPredicateProvider with our item and an Identifier. For the Identifiers use, you do not need to provide a namespace, as none of these should conflict with Minecraft's providers. This must be done in the onInitializeClient method of your ClientModInitializer. If you do not have a ClientModInitializer, look at the entrypoints tutorial on how to add one.

  1. @Environment(EnvType.CLIENT)
  2. public class ExampleModClient implements ClientModInitializer {
  3. public static void registerModelPredicateProviders() {
  4. // For versions before 1.21, replace 'Identifier.ofVanilla' with 'new Identifier'.
  5. ModelPredicateProviderRegistry.register(EXAMPLE_BOW, Identifer.ofVanilla("pull"), (itemStack, clientWorld, livingEntity, seed) -> {
  6. if (livingEntity == null) {
  7. return 0.0F;
  8. }
  9. return livingEntity.getActiveItem() != itemStack ? 0.0F : (itemStack.getMaxUseTime(livingEntity) - livingEntity.getItemUseTimeLeft()) / 20.0F;
  10. });
  11.  
  12. ModelPredicateProviderRegistry.register(EXAMPLE_BOW, Identifier.ofVanilla("pulling"), (itemStack, clientWorld, livingEntity, seed) -> {
  13. if (livingEntity == null) {
  14. return 0.0F;
  15. }
  16. return livingEntity.isUsingItem() && livingEntity.getActiveItem() == itemStack ? 1.0F : 0.0F;
  17. });
  18. }
  19.  
  20. @Override
  21. public void onInitializeClient() {
  22. // ...
  23. registerModelPredicateProviders();
  24. }
  25. }

If the ModelPredicateProviderRegistry does not exist in some versions, you may use FabricModelPredicateProviderRegistry.

The ModelPredicateProvider is a Functional Interface that takes in an ItemStack for the current stack that is being rendered, a ClientWorld for the current world that the client is in, and a LivingEntity as the user of the item (In 1.17+, it also takes in an int).

All ModelPredicateProvider returns a float, which can be used to represent different states for the model, or a true/false value by returning 1.0f and 0f respectively. The float value will be clamped between 0.0f and 1.0f.

Taking a closer look at the pull predicate, we see that we first check if the entity is null, as items can be rendered outside of a context where they are being used (i.e. dropped on the ground). We then make sure that the entity is using our item, otherwise the item could have the model predicate apply when not being used. Finally, we subtract the livingEntity.getItemUseTimeLeft() from the itemStack.getMaxUseTime(livingEntity) which tells us how many ticks the item has been held for. Because it takes 20 ticks or one second to fully charge a bow, we then divide this number by 20.0f to normalize it to between 0f and 1.0f for the normal pull progress.

All of this is useful, but is only half of the features we need to implement in order to have our item change its model.

resources/data/tutorial/models/item/example_bow.json
{
    // [...]
    "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"
        }
    ]
}

The way that Minecraft works is that it checks to see which model is last in the list that either matches or has values greater than specified. so with a pull value of 0.8, we would use model tutorial:item/example_bow_pulling_1. 0.5 would be tutorial:item/example_bow_pulling_0.

tutorial/model_predicate_providers.txt · Last modified: 2024/08/23 14:05 by solidblock