zh_cn:tutorial:colorprovider
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| zh_cn:tutorial:colorprovider [2024/08/27 03:13] – solidblock | zh_cn:tutorial:colorprovider [2025/04/01 12:11] (current) – solidblock | ||
|---|---|---|---|
| Line 27: | Line 27: | ||
| [...] | [...] | ||
| + | // 1.21.2 之前: | ||
| public static final Block COLOR_BLOCK = register(" | public static final Block COLOR_BLOCK = register(" | ||
| + | | ||
| + | // 1.21.2 及之后: | ||
| + | public static final Block COLOR_BLOCK = register(" | ||
| } | } | ||
| </ | </ | ||
| Line 33: | Line 37: | ||
| 简单添加个方块状态文件: | 简单添加个方块状态文件: | ||
| - | Then add a simple block states file: | ||
| <code javascript src/ | <code javascript src/ | ||
| { | { | ||
| Line 72: | Line 75: | ||
| 然后创建带有 // | 然后创建带有 // | ||
| - | 【【 | ||
| - | 我们在这里所做的只是说:“Hi,'' | ||
| - | 如果你需要在颜色提供器中访问 '' | ||
| - | 】】 | ||
| <code javascript src/ | <code javascript src/ | ||
| { | { | ||
| Line 100: | Line 99: | ||
| </ | </ | ||
| - | 在这个实例里面,我们添加了单一的 tintindex,出现在 '' | + | 在这个实例里面,我们添加了单一的 tintindex,出现在 '' |
| <code javascript src/ | <code javascript src/ | ||
| { | { | ||
| Line 118: | Line 117: | ||
| ===== 带有颜色提供器的方块实体 ===== | ===== 带有颜色提供器的方块实体 ===== | ||
| - | If you need to access | + | 如果你需要在颜色提供器中访问 |
| - | This is because blocks can be rendered on separate threads, so accessing the data directly is not safe. Additionally, | + | 这是因为方块可以在单独的线程渲染,所以直接访问数据并不安全。而且,如果使用 |
| - | In this case, we create a '' | + | 在这个例子中,我们创建一个 |
| <code java ColorBlock.java> | <code java ColorBlock.java> | ||
| Line 141: | Line 140: | ||
| } | } | ||
| + | // 自从 1.21.4 开始,不再需要这个方法,因为所有方块实体都默认使用方块模型。 | ||
| @Override | @Override | ||
| protected BlockRenderType getRenderType(BlockState state) { | protected BlockRenderType getRenderType(BlockState state) { | ||
| Line 156: | Line 156: | ||
| } | } | ||
| | | ||
| - | // The following two methods specify serialization of color data. | + | // 以下两个方块指定了颜色数据的序列化。 |
| @Override | @Override | ||
| protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { | protected void readNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) { | ||
| super.readNbt(nbt, | super.readNbt(nbt, | ||
| - | color = nbt.getInt(" | ||
| | | ||
| - | // When the data is modified through | + | // 对于 1.21.5 之前的版本,请直接使用 nbt.getInt("color" |
| - | // or placed by an item with "block_entity_data" component, | + | color = nbt.getInt(" |
| - | // the render color will be updated. | + | |
| + | // 当数据通过“/ | ||
| + | // 需要同步更新。 | ||
| if (world != null) { | if (world != null) { | ||
| world.updateListeners(pos, | world.updateListeners(pos, | ||
| Line 190: | Line 191: | ||
| @Override | @Override | ||
| public @Nullable Object getRenderData() { | public @Nullable Object getRenderData() { | ||
| - | // this is the method from `RenderDataBlockEntity` | + | // 这是来自 |
| return color; | return color; | ||
| } | } | ||
| Line 196: | Line 197: | ||
| </ | </ | ||
| - | In the '' | + | 在 '' |
| <code java> | <code java> | ||
| + | // 1.21.2 之前: | ||
| public static final ColorBlock COLOR_BLOCK = register(" | public static final ColorBlock COLOR_BLOCK = register(" | ||
| + | | ||
| + | // 1.21.2 及之后: | ||
| + | public static final Block COLOR_BLOCK = register(" | ||
| </ | </ | ||
| - | In the '' | + | 在 '' |
| <code java> | <code java> | ||
| public static final BlockEntityType< | public static final BlockEntityType< | ||
| Line 207: | Line 212: | ||
| </ | </ | ||
| - | Now we modify | + | > 不要忘了在你的模组初始化器中,静态加载 '' |
| + | |||
| + | 现在我们修改 | ||
| <code java ColorBlock.java> | <code java ColorBlock.java> | ||
| public class ColorBlock extends BlockWithEntity { | public class ColorBlock extends BlockWithEntity { | ||
| Line 213: | Line 220: | ||
| | | ||
| @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 231: | Line 238: | ||
| </ | </ | ||
| - | Finally, modify the color provider to use the render data. We call '' | + | 最后,修改颜色提供器以使用渲染数据。我们调用 |
| <code java ExampleModClient.java> | <code java ExampleModClient.java> | ||
| @Environment(EnvType.CLIENT) | @Environment(EnvType.CLIENT) | ||
| Line 245: | Line 252: | ||
| </ | </ | ||
| - | Now done! Then you can check whether the following work correctly: | + | 搞定!现在你可以检查下面这些是否都正常起作用: |
| - | * When you interact the block with a dye, the color should change. | + | * 使用染料交互,颜色应该改变。 |
| - | * When you modify the color through | + | * 通过 |
| - | * When you pick (press mouse wheel) the block with '' | + | * 按下 |
| - | * When you leave the world and re-enter, the color should be kept. | + | * 离开世界重进,颜色应该保留。 |
| - | ===== 物品颜色提供器 ===== | + | ===== 自定义物品着色(1.21.4 及之后) ===== |
| - | 物品是类似的,区别在于提供的上下文。不访问状态、世界和位置,而是访问 '' | + | 从 1.21.4 开始,物品的着色是由物品模型映射指定的。原版提供了几种常见的着色来源的类型,参见 [[https:// |
| + | <code javascript / | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | " | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | ] | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 如果需要指定自定义的着色来源,可以使用原版提供的 '' | ||
| + | |||
| + | > 如果着色不生效,检查下使用的模型中的 tintindex 的值,应该与物品模型映射中的 '' | ||
| + | |||
| + | ===== 物品颜色提供器(1.21.4 之前) | ||
| + | 在 1.21.3 以及之前的版本,物品的颜色提供器也是可通过 Fabric API 注册的。与方块不同,物品的颜色提供器提供的上下文不访问状态、世界和位置,而是访问 '' | ||
| 物品模型可以直接继承使用 tintindex 的方块模型: | 物品模型可以直接继承使用 tintindex 的方块模型: | ||
| + | |||
| + | 首先,物品需要一个直接继承方块模型的物品模型: | ||
| + | <code javascript / | ||
| + | { | ||
| + | " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 然后,再在客户端环境中,注册颜色提供器。 | ||
| <code java ExampleModClient.java> | <code java ExampleModClient.java> | ||
zh_cn/tutorial/colorprovider.1724728430.txt.gz · Last modified: 2024/08/27 03:13 by solidblock