How do I tint blocks and items? #1211
-
Some blocks, like leaves and other foliage, and some items, such as leather armor, have dynamic colors assigned to them. How can I implement this in my own Fabric mod? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
This functionality is enabled by a system called color providers. Although Minecraft does not easily allow new color providers to be added, Fabric API provides a set of hooks in the form of Both block and item color providers require a tint index to be specified. This is useful for two reasons:
BlocksFor block models, each face of a cube can have its own tint index, specified as an integer using the Block model with tint indices{
"parent": "block/block",
"textures": {
"all": "block/white_wool",
"particle": "#all"
},
"elements": [{
"from": [
0,
0,
0
],
"to": [
16,
16,
16
],
"faces": {
"down": {
"uv": [
0,
0,
16,
16
],
"texture": "#all",
"cullface": "down",
"tintindex": 0
},
"up": {
"uv": [
0,
0,
16,
16
],
"texture": "#all",
"cullface": "up",
"tintindex": 0
},
"north": {
"uv": [
0,
0,
16,
16
],
"texture": "#all",
"cullface": "north",
"tintindex": 0
},
"south": {
"uv": [
0,
0,
16,
16
],
"texture": "#all",
"cullface": "south",
"tintindex": 0
},
"west": {
"uv": [
0,
0,
16,
16
],
"texture": "#all",
"cullface": "west",
"tintindex": 0
},
"east": {
"uv": [
0,
0,
16,
16
],
"texture": "#all",
"cullface": "east",
"tintindex": 0
}
}
}]
} The color provider must then be registered to compute the tint when a tint index is specified: ColorProviderRegistry.BLOCK.register((state, world, pos, tintIndex) -> {
return 0xFF0000;
}, TINTED_BLOCK); Returning ItemsBecause item models are the same as block models, the {
"parent": "item/generated",
"textures": {
"layer0": "fabricexample:item/base_texture",
"layer1": "fabricexample:item/tinted_overlay"
}
} The color provider is registered with the item stack as context. ColorProviderRegistry.ITEM.register((stack, tintIndex) -> {
return 0xFF0000;
}, TINTED_ITEM); |
Beta Was this translation helpful? Give feedback.
-
You've missed something. |
Beta Was this translation helpful? Give feedback.
This functionality is enabled by a system called color providers. Although Minecraft does not easily allow new color providers to be added, Fabric API provides a set of hooks in the form of
ColorProviderRegistry
to allow mods to register their own color providers. Color providers are completely client-side, and thus this should be registered in the client-only mod initializer only.Both block and item color providers require a tint index to be specified. This is useful for two reasons:
Blocks
For block models, each face of a cube can have its own tint index, specified as an integer using the
tin…