Skip to content

Commit a9f2467

Browse files
authored
Replace ColorProviderRegistry with BlockColorRegistry (#5140)
* Replace ColorProviderRegistry with BlockColorRegistry ColorProviderRegistry was designed to provide a common interface over both block colors and item colors, but since item colors were refactored completely in 1.21.4, this design is unnecessarily complicated. The new class is as simple as possible. * Pass the block array directly
1 parent 4cbb866 commit a9f2467

File tree

7 files changed

+107
-147
lines changed

7 files changed

+107
-147
lines changed

fabric-particles-v1/src/testmodClient/java/net/fabricmc/fabric/test/particle/client/ParticleRenderEventTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020

2121
import net.fabricmc.api.ClientModInitializer;
2222
import net.fabricmc.fabric.api.client.particle.v1.ParticleRenderEvents;
23-
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
23+
import net.fabricmc.fabric.api.client.rendering.v1.BlockColorRegistry;
2424
import net.fabricmc.fabric.test.particle.ParticleTestSetup;
2525
import net.fabricmc.fabric.test.particle.ParticleTintTestBlock;
2626

2727
public final class ParticleRenderEventTests implements ClientModInitializer {
2828
@Override
2929
public void onInitializeClient() {
30-
ColorProviderRegistry.BLOCK.register((state, level, pos, tintIndex) -> {
30+
BlockColorRegistry.register((state, level, pos, tintIndex) -> {
3131
if (tintIndex == 0) {
3232
return ((ParticleTintTestBlock) state.getBlock()).color;
3333
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.fabricmc.fabric.api.client.rendering.v1;
18+
19+
import net.minecraft.client.Minecraft;
20+
import net.minecraft.client.color.block.BlockColor;
21+
import net.minecraft.client.color.block.BlockColors;
22+
import net.minecraft.world.level.block.Block;
23+
24+
import net.fabricmc.fabric.impl.client.rendering.BlockColorRegistryImpl;
25+
26+
/**
27+
* The registry for {@link BlockColor}s.
28+
*/
29+
public final class BlockColorRegistry {
30+
private BlockColorRegistry() {
31+
}
32+
33+
/**
34+
* Register a block color for one or more blocks. Overriding existing registrations is allowed.
35+
*
36+
* <p>Mods must use this method instead of {@link BlockColors#register(BlockColor, Block...)} during mod
37+
* initialization as it runs before {@link Minecraft#getBlockColors()} is available.
38+
*
39+
* @param color The block color.
40+
* @param blocks The blocks which should be colored using the given color.
41+
*/
42+
public static void register(BlockColor color, Block... blocks) {
43+
BlockColorRegistryImpl.register(color, blocks);
44+
}
45+
}

fabric-rendering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/ColorProviderRegistry.java

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.fabricmc.fabric.impl.client.rendering;
18+
19+
import java.util.IdentityHashMap;
20+
import java.util.Map;
21+
22+
import org.jspecify.annotations.Nullable;
23+
24+
import net.minecraft.client.color.block.BlockColor;
25+
import net.minecraft.client.color.block.BlockColors;
26+
import net.minecraft.world.level.block.Block;
27+
28+
public final class BlockColorRegistryImpl {
29+
@Nullable
30+
private static BlockColors blockColors;
31+
@Nullable
32+
private static Map<Block, BlockColor> map = new IdentityHashMap<>();
33+
34+
public static void initialize(BlockColors blockColors) {
35+
if (BlockColorRegistryImpl.blockColors != null) {
36+
return;
37+
}
38+
39+
BlockColorRegistryImpl.blockColors = blockColors;
40+
41+
map.forEach((block, color) -> blockColors.register(color, block));
42+
map = null;
43+
}
44+
45+
public static void register(BlockColor color, Block... blocks) {
46+
if (blockColors != null) {
47+
blockColors.register(color, blocks);
48+
} else {
49+
for (Block block : blocks) {
50+
map.put(block, color);
51+
}
52+
}
53+
}
54+
}

fabric-rendering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/ColorProviderRegistryImpl.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

fabric-rendering-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/BlockColorsMixin.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,19 @@
1616

1717
package net.fabricmc.fabric.mixin.client.rendering;
1818

19-
import org.spongepowered.asm.mixin.Final;
2019
import org.spongepowered.asm.mixin.Mixin;
21-
import org.spongepowered.asm.mixin.Shadow;
2220
import org.spongepowered.asm.mixin.injection.At;
2321
import org.spongepowered.asm.mixin.injection.Inject;
2422
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
2523

26-
import net.minecraft.client.color.block.BlockColor;
2724
import net.minecraft.client.color.block.BlockColors;
28-
import net.minecraft.core.IdMapper;
29-
import net.minecraft.core.registries.BuiltInRegistries;
30-
import net.minecraft.world.level.block.Block;
3125

32-
import net.fabricmc.fabric.impl.client.rendering.ColorProviderRegistryImpl;
26+
import net.fabricmc.fabric.impl.client.rendering.BlockColorRegistryImpl;
3327

3428
@Mixin(BlockColors.class)
35-
public class BlockColorsMixin implements ColorProviderRegistryImpl.ColorMapperHolder<Block, BlockColor> {
36-
@Shadow
37-
@Final
38-
private IdMapper<BlockColor> blockColors;
39-
29+
abstract class BlockColorsMixin {
4030
@Inject(method = "createDefault", at = @At("RETURN"))
41-
private static void create(CallbackInfoReturnable<BlockColors> info) {
42-
ColorProviderRegistryImpl.BLOCK.initialize(info.getReturnValue());
43-
}
44-
45-
@Override
46-
public BlockColor get(Block block) {
47-
return blockColors.byId(BuiltInRegistries.BLOCK.getId(block));
31+
private static void create(CallbackInfoReturnable<BlockColors> ci) {
32+
BlockColorRegistryImpl.initialize(ci.getReturnValue());
4833
}
4934
}

fabric-rendering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/CustomColorResolverTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import net.minecraft.world.level.ColorResolver;
2020

2121
import net.fabricmc.api.ClientModInitializer;
22-
import net.fabricmc.fabric.api.client.rendering.v1.ColorProviderRegistry;
22+
import net.fabricmc.fabric.api.client.rendering.v1.BlockColorRegistry;
2323
import net.fabricmc.fabric.api.client.rendering.v1.ColorResolverRegistry;
2424
import net.fabricmc.fabric.test.rendering.CustomColorResolverTestInit;
2525

@@ -36,7 +36,7 @@ public class CustomColorResolverTest implements ClientModInitializer {
3636
public void onInitializeClient() {
3737
ColorResolverRegistry.register(TEST_COLOR_RESOLVER);
3838

39-
ColorProviderRegistry.BLOCK.register((state, level, pos, tintIndex) -> {
39+
BlockColorRegistry.register((state, level, pos, tintIndex) -> {
4040
if (level != null && pos != null) {
4141
return level.getBlockTint(pos, TEST_COLOR_RESOLVER);
4242
} else {

0 commit comments

Comments
 (0)