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 [2024/08/27 03:13] solidblocktutorial:colorprovider [2025/12/05 17:03] (current) – redirect to docs cassiancc
Line 1: Line 1:
 +~~REDIRECT>https://docs.fabricmc.net/develop/blocks/transparency-and-tinting~~
 +~~REDIRECT>https://docs.fabricmc.net/develop/items/item-appearance~~
 ======= Color Providers ======= ======= Color Providers =======
 Ever wonder how grass and leaves change hues depending on the biome, or how leather armor can have seemingly infinite color patterns? Meet **color providers**, which allow you to hue and tint block & item model textures based on properties such as location, NBT, or block states. Ever wonder how grass and leaves change hues depending on the biome, or how leather armor can have seemingly infinite color patterns? Meet **color providers**, which allow you to hue and tint block & item model textures based on properties such as location, NBT, or block states.
Line 24: Line 26:
 public final class TutorialBlocks { public final class TutorialBlocks {
   [...]   [...]
 +  // Before 1.21.2
   public static final Block COLOR_BLOCK = register("color_block", new Block(AbstractBlock.Settings.create()));   public static final Block COLOR_BLOCK = register("color_block", new Block(AbstractBlock.Settings.create()));
 +  
 +  // 1.21.2 and after:
 +  public static final Block COLOR_BLOCK = register("color_block", Block::new, AbstractBlock.Settings.create());
 } }
 </code> </code>
Line 131: Line 136:
   }   }
  
 +  // Since 1.21.4, this method is not required anymore, because all block entities use their block model by default.
   @Override   @Override
   protected BlockRenderType getRenderType(BlockState state) {   protected BlockRenderType getRenderType(BlockState state) {
Line 151: Line 157:
   protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {   protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
     super.readNbt(nbt, registryLookup);     super.readNbt(nbt, registryLookup);
 +    
 +    // For versions before 1.21.5, please directly use nbt.getInt("color");
     color = nbt.getInt("color");     color = nbt.getInt("color");
          
Line 188: Line 196:
 In the ''TutorialBlocks'' class, replace ''new Block'' with ''new ColorBlock'': In the ''TutorialBlocks'' class, replace ''new Block'' with ''new ColorBlock'':
 <code java> <code java>
 +  // Before 1.21.2:
   public static final ColorBlock COLOR_BLOCK = register("color_block", new ColorBlock(AbstractBlock.Settings.create()));   public static final ColorBlock COLOR_BLOCK = register("color_block", new ColorBlock(AbstractBlock.Settings.create()));
 +  
 +  // Since 1.21.2:
 +  public static final Block COLOR_BLOCK = register("color_block", ColorBlock::new, AbstractBlock.Settings.create());
 </code> </code>
  
Line 203: Line 215:
      
   @Override   @Override
-  protected ItemActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {+  protected ActionResult onUseWithItem(ItemStack stack, BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
     if (stack.getItem() instanceof DyeItem dyeItem) {     if (stack.getItem() instanceof DyeItem dyeItem) {
       if (world.getBlockEntity(pos) instanceof ColorBlockEntity colorBlockEntity) {       if (world.getBlockEntity(pos) instanceof ColorBlockEntity colorBlockEntity) {
         final int newColor = dyeItem.getColor().getEntityColor();         final int newColor = dyeItem.getColor().getEntityColor();
         final int originalColor = colorBlockEntity.color;         final int originalColor = colorBlockEntity.color;
-        colorBlockEntity.color = ColorHelper.Argb.averageArgb(newColor, originalColor);+        colorBlockEntity.color = ColorHelper.average(newColor, originalColor);
         stack.decrementUnlessCreative(1, player);         stack.decrementUnlessCreative(1, player);
         colorBlockEntity.markDirty();         colorBlockEntity.markDirty();
Line 240: Line 252:
   * When you pick (press mouse wheel) the block with ''Ctrl'' pressed, and place the block, it should display as the expected color.   * When you pick (press mouse wheel) the block with ''Ctrl'' pressed, and place the block, it should display as the expected color.
   * When you leave the world and re-enter, the color should be kept.   * When you leave the world and re-enter, the color should be kept.
-===== Item Color Provider ===== + 
-Items are similar; the difference is the context provided. Instead of having a state, world, or position, you have access to the ''ItemStack''.+===== Custom item tint (1.21.4 and after) ===== 
 +Since 1.21.4, tints of items are defined in the Items model definition. Some common tint source types are provided in vanilla, see [[https://minecraft.wiki/w/Items_model_definition|Minecraft Wiki]]. In this example, we directly set the color of the item, so we can write the items model definition as follows: 
 +<code javascript /resources/assets/tutorial/items/color_block.json> 
 +
 +  "model":
 +    "type": "model", 
 +    "model": "tutorial:block/color_block", 
 +    "tints":
 +      { 
 +        "type": "constant", 
 +        "value": 3446251 
 +      } 
 +    ] 
 +  } 
 +
 +</code> 
 + 
 +If you need to specify a custom tint source, you can register with ''TintResourceTypes.//ID_MAPPER//.put(...)'' provided in vanilla. Note that this should be done in client environment. The ID used in the registration is the value of ''%%"type"%%'' in the ''%%"tints"%%'' in the item model definition above. 
 + 
 +> If the tint does not work, check the value of tintindex in the model, which should be consistent with the element subscript in the ''%%"tints"%%'' list in the item models definition. For example, if the tintindex is 2, it uses the third tint source in the item models definition. 
 + 
 +In version 1.21.3 and before, item model providers were instead registered with Fabric API. Different from blocks, the context provided, instead of having a state, world, or position, has access to the ''ItemStack''.
  
 For item models, we can directly inherite the block model that uses tintindex: For item models, we can directly inherite the block model that uses tintindex:
tutorial/colorprovider.1724728405.txt.gz · Last modified: 2024/08/27 03:13 by solidblock