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

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:model_predicate_providers [2021/03/05 19:18] – Add that model predicates need to be initialized in client oroarmortutorial:model_predicate_providers [2022/06/01 11:48] (current) – external edit 127.0.0.1
Line 13: Line 13:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
  
-FabricModelPredicateProviderRegistry.register(EXAMPLE_BOW, new Identifier("pull"), (itemStack, clientWorld, livingEntity) -> {+ModelPredicateProviderRegistry.register(EXAMPLE_BOW, new Identifier("pull"), (itemStack, clientWorld, livingEntity) -> {
  if (livingEntity == null) {  if (livingEntity == null) {
  return 0.0F;  return 0.0F;
Line 20: Line 20:
 }); });
  
-FabricModelPredicateProviderRegistry.register(EXAMPLE_BOW, new Identifier("pulling"), (itemStack, clientWorld, livingEntity) -> {+ModelPredicateProviderRegistry.register(EXAMPLE_BOW, new Identifier("pulling"), (itemStack, clientWorld, livingEntity) -> {
  if (livingEntity == null) {  if (livingEntity == null) {
  return 0.0F;  return 0.0F;
Line 27: Line 27:
 }); });
 </code> </code>
 +
 +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''). 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.+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()'' 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. 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.
tutorial/model_predicate_providers.1614971932.txt.gz · Last modified: 2021/03/05 19:18 by oroarmor