3,480 bytes added
, 03:58, 27 July 2021
Item properties are a way for the "properties" of items to be exposed to the model system. An example is the bow, where the most important property is how far the bow has been pulled. This information is then used to choose a model for the bow, creating an animation for pulling it.
An item property assigns a certain <code>float</code> value to every <code>ItemStack</code> it is registered for, and vanilla item model definitions can use these values to define “overrides”, where an item defaults to a certain model, but if an override matches, it overrides the model and uses another. They are useful mainly because of the fact that they are continuous. For example, bows use item properties to define their pull animation. Since the value of the property is a <code>float</code>, it increases continuously from 0 to 1. This allows resource packs to add as many models as they want for the bow pulling animation along that spectrum, instead of being stuck with four “slots” for their models in the animation. The same is true of the compass and clock.
== Adding Properties to Items ==
<code><nowiki>ItemModelsProperties::registerProperty</nowiki></code> is used to add a property to a specific item. The <code>ResourceLocation</code> parameter is the name given to the property (e.g. <code><nowiki>new ResourceLocation("pull")</nowiki></code>). The <code>IItemPropertyGetter</code> is a function that takes the <code>ItemStack</code>, the <code>ClientWorld</code> it’s in, and the <code>LivingEntity</code> that holds it, returning the <code>float</code> value for the property. Some examples are the <code><nowiki>pulling</nowiki></code> and <code><nowiki>pull</nowiki></code> properties for <code>Items#BOW</code>, and the several default ones in <code>ItemModelProperties</code>. For modded item properties, it is recommended that the modid of the mod is used as the namespace (e.g. <code><nowiki>examplemod:property</nowiki></code> and not just <code>property</code>, as that really means <code><nowiki>minecraft:property</nowiki></code>).
== Using Overrides ==
A good example can be found in <code><nowiki>model/item/bow.json</nowiki></code>. For reference, here is a hypothetical example of an item with an <code><nowiki>examplemod:power</nowiki></code> property. If the values have no match, the default is the current model.
<block important>A predicate applies to all values greater than or equal to the given value.</block>
<syntaxhighlight lang="json">
{
"parent": "item/generated",
"textures": {
"__comment": "Default",
"layer0": "examplemod:items/examplePartial"
},
"overrides": [
{
"__comment": "power >= .75",
"predicate": {
"examplemod:power": 0.75
},
"model": "examplemod:item/examplePowered"
}
]
}
</syntaxhighlight>
And here’s a hypothetical snippet from the supporting code. (This does not have to be client-only; it will work on a server too. In vanilla, properties are registered in the item’s constructor.)
<syntaxhighlight lang="java">
public void clientSetup(final FMLCLientSetupEvent event) {
ItemModelProperties.registerProperty(item, new ResourceLocation("examplemod:power"), new IItemPropertyGetter() {
@Override
public float call(ItemStack stack, @Nullable ClientWorld world, @Nullable LivingEntity entity) {
return (float)getPowerLevel(stack) / (float)getMaxPower(stack); // Some external methods
}
}
}
</syntaxhighlight>
[[Category:Models/1.16]]