User Tools

Site Tools


tutorial:colorprovider

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:colorprovider [2019/10/25 17:48] draylartutorial:colorprovider [2023/11/18 08:37] (current) – [Registering a Block Color Provider] solidblock
Line 8: Line 8:
   * leather armor dying   * leather armor dying
   * redstone wire   * redstone wire
-  * plants such as melons, sugarcane, and lilypads+  * plants such as melons, sugarcane, and lily pads
   * tipped arrows   * tipped arrows
  
Line 20: Line 20:
 To register a block to the block color provider, you'll need to use Fabric's ''ColorProviderRegistry''. There is an instance of the ''BLOCK'' and ''ITEM'' provider inside this class, which you can call register on. The register method takes an instance of your color provider and a varargs of every block you want to color with the provider.  To register a block to the block color provider, you'll need to use Fabric's ''ColorProviderRegistry''. There is an instance of the ''BLOCK'' and ''ITEM'' provider inside this class, which you can call register on. The register method takes an instance of your color provider and a varargs of every block you want to color with the provider. 
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
-ColorProviderRegistry.BLOCK.register(new BlockColorProvider() { +ColorProviderRegistry.BLOCK.register((state, view, pos, tintIndex-> 0x3495eb, MY_BLOCK);
-        @Override +
- public int getColor(BlockState state, ExtendedBlockView worldBlockPos pos, int layer+
- return 0x3495eb+
-+
-}, MY_BLOCK);+
 </code> </code>
  
-All we do here is say, "Hi, ''MY_BLOCK'' should be colored 0x3495eb," which is a blue color. You have BlockState, World, and BlockPos context, which is how you can change colors based on biome or position. The final int is the layer; each one asks for a color individually, but in this case, we're always returning blue.+All we do here is say, "Hi, ''MY_BLOCK'' should be colored 0x3495eb," which is a blue color. You have ''BlockState''''World'', and ''BlockPos'' context, which is how you can change colors based on biome or position. The final int is the tintIndex; each one asks for a color individually, but in this case, we're always returning blue
 + 
 +If you need to access ''BlockEntity'' data in the color provider, you'll want to implement ''RenderAttachmentBlockEntity'' to return the data you need. This is because blocks can be rendered on separate threads, so accessing the data directly is not safe. Additionally, if you query blocks with ''getBlockState'' you won't be able to view the entire world - make sure you only query within ±2 blocks x/y/z of the current position.
  
 The model is also important: the main note here is that you are //required// to define a tintindex for each portion of the model you want to hue. To see an example of this, check out ''leaves.json'', which is the base model used for vanilla leaves. Here's the model used for our block: The model is also important: the main note here is that you are //required// to define a tintindex for each portion of the model you want to hue. To see an example of this, check out ''leaves.json'', which is the base model used for vanilla leaves. Here's the model used for our block:
-<code json [enable_line_numbers="false"]>+<code javascript [enable_line_numbers="false"]>
 { {
   "parent": "block/block",   "parent": "block/block",
Line 39: Line 36:
   },   },
   "elements": [   "elements": [
-    {   "from": [ 0, 0, 0 ],+    { "from": [ 0, 0, 0 ],
       "to": [ 16, 16, 16 ],       "to": [ 16, 16, 16 ],
       "faces": {       "faces": {
Line 53: Line 50:
 } }
 </code> </code>
-In this instance, we're adding a single tintindex, which is what would appear in the `layer` parameter (layer 0).+In this instance, we're adding a single tintindex, which is what would appear in the ''tintIndex'' parameter (tint index 0).
  
 Here's the final result-- note that the original model used the ''white_concrete'' texture: Here's the final result-- note that the original model used the ''white_concrete'' texture:
Line 61: Line 58:
 Items are similar; the difference is the context provided. Instead of having a state, world, or position, you have access to the ''ItemStack''. Items are similar; the difference is the context provided. Instead of having a state, world, or position, you have access to the ''ItemStack''.
 <code java [enable_line_numbers="false"]> <code java [enable_line_numbers="false"]>
-ColorProviderRegistry.ITEM.register((itemStacklayer) -> +ColorProviderRegistry.ITEM.register((stacktintIndex) -> 0x3495eb, COLORED_ITEM);
- return 0x3495eb+
-}, COLORED_ITEM);+
 </code> </code>
 This would hue the item in our inventory in the same fashion as the block. This would hue the item in our inventory in the same fashion as the block.
tutorial/colorprovider.1572025696.txt.gz · Last modified: 2019/10/25 17:48 by draylar