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/04/01 12:10] (current) solidblock
Line 24: Line 24:
 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 134:
   }   }
  
 +  // 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 155:
   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 194:
 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 213:
      
   @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 250:
   * 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 item models definitions. Some common tint source types are provided in vanilla, see [[https://minecraft.wiki/w/Item models definition|Minecraft Wiki]]. In this example, we directly set the color of the item, so we can write the item models 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 are also 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