tutorial:colorprovider
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:colorprovider [2024/08/26 08:08] – [Block Color Provider] solidblock | tutorial:colorprovider [2025/04/01 12:10] (current) – solidblock | ||
|---|---|---|---|
| Line 18: | Line 18: | ||
| ===== Block Color Provider ===== | ===== Block Color Provider ===== | ||
| - | To register a block to the block color provider, you'll need to use Fabric' | + | To register a block to the block color provider, you'll need to use Fabric' |
| At first, we create the block in '' | At first, we create the block in '' | ||
| Line 24: | Line 24: | ||
| public final class TutorialBlocks { | public final class TutorialBlocks { | ||
| [...] | [...] | ||
| + | // Before 1.21.2 | ||
| public static final Block COLOR_BLOCK = register(" | public static final Block COLOR_BLOCK = register(" | ||
| + | | ||
| + | // 1.21.2 and after: | ||
| + | public static final Block COLOR_BLOCK = register(" | ||
| } | } | ||
| </ | </ | ||
| Line 67: | Line 70: | ||
| </ | </ | ||
| - | Then we create the block model with // | + | Then we create the block model with // |
| - | + | ||
| - | The model is also important: the main note here is that you are // | + | |
| <code javascript src/ | <code javascript src/ | ||
| { | { | ||
| Line 93: | Line 94: | ||
| </ | </ | ||
| - | In this instance, we're adding a single tintindex, which is what would appear in the '' | + | In this instance, we're adding a single tintindex, which is what would appear in the '' |
| <code javascript src/ | <code javascript src/ | ||
| { | { | ||
| Line 133: | 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 153: | Line 155: | ||
| protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { | protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { | ||
| super.readNbt(nbt, | super.readNbt(nbt, | ||
| + | | ||
| + | // For versions before 1.21.5, please directly use nbt.getInt(" | ||
| color = nbt.getInt(" | color = nbt.getInt(" | ||
| | | ||
| Line 158: | Line 162: | ||
| // or placed by an item with " | // or placed by an item with " | ||
| // the render color will be updated. | // the render color will be updated. | ||
| - | | + | |
| + | | ||
| + | } | ||
| } | } | ||
| Line 188: | Line 194: | ||
| In the '' | In the '' | ||
| <code java> | <code java> | ||
| + | // Before 1.21.2: | ||
| public static final ColorBlock COLOR_BLOCK = register(" | public static final ColorBlock COLOR_BLOCK = register(" | ||
| + | | ||
| + | // Since 1.21.2: | ||
| + | public static final Block COLOR_BLOCK = register(" | ||
| </ | </ | ||
| Line 203: | Line 213: | ||
| | | ||
| @Override | @Override | ||
| - | protected | + | protected |
| 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, | stack.decrementUnlessCreative(1, | ||
| colorBlockEntity.markDirty(); | colorBlockEntity.markDirty(); | ||
| Line 240: | Line 250: | ||
| * When you pick (press mouse wheel) the block with '' | * When you pick (press mouse wheel) the block with '' | ||
| * 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 '' | ||
| - | For item models, we can directly inherite the block model, which uses tintindex: | + | ===== 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:// | ||
| + | <code javascript / | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | If you need to specify a custom tint source, you can register with '' | ||
| + | |||
| + | > If the tint does not work, check the value of tintindex in the model, which should be consistent with the element subscript in the '' | ||
| + | |||
| + | 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 '' | ||
| + | |||
| + | For item models, we can directly inherite the block model that uses tintindex: | ||
| <code javascript src/ | <code javascript src/ | ||
| { | { | ||
tutorial/colorprovider.1724659713.txt.gz · Last modified: 2024/08/26 08:08 by solidblock