User Tools

Site Tools


tutorial:model_predicate_providers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revisionBoth sides next revision
tutorial:model_predicate_providers [2021/03/05 18:49] – created oroarmortutorial:model_predicate_providers [2021/03/05 19:11] – Add example oroarmor
Line 8: Line 8:
  
 For this example, let's say we have we have a custom bow item called ''EXAMPLE_BOW''. For this example, let's say we have we have a custom bow item called ''EXAMPLE_BOW''.
 +
 +Inside a method, we register a ''ModelPredicateProvider'' with our item and an ''Identifier''. For the ''Identifier''s use, you do not need to provide a namespace, as none of these should conflict with Minecraft's providers.
  
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
Line 26: Line 28:
 </code> </code>
  
 +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.
 +
 +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()'' 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.
 +
 +<code json [enable_line_numbers="true"]>
 +"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"
 +    }
 +]  
 +</code>
 +
 +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: 2022/06/01 11:48 by 127.0.0.1