| 1 | package net.minecraft.client.resources.model;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 5 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 6 | import java.util.HashMap;
|
| 7 | import java.util.Iterator;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Set;
|
| 11 | import net.fabricmc.api.EnvType;
|
| 12 | import net.fabricmc.api.Environment;
|
| 13 | import net.minecraft.client.color.block.BlockColors;
|
| 14 | import net.minecraft.client.renderer.block.dispatch.BlockStateModel;
|
| 15 | import net.minecraft.world.level.block.Block;
|
| 16 | import net.minecraft.world.level.block.RenderShape;
|
| 17 | import net.minecraft.world.level.block.state.BlockState;
|
| 18 | import net.minecraft.world.level.block.state.properties.Property;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class ModelGroupCollector {
|
| 22 | static final int SINGLETON_MODEL_GROUP = -1;
|
| 23 | private static final int INVISIBLE_MODEL_GROUP = 0;
|
| 24 |
|
| 25 | public static Object2IntMap<BlockState> build(final BlockColors blockColors, final BlockStateModelLoader.LoadedModels input) {
|
| 26 | Map<Block, List<Property<?>>> coloringPropertiesCache = new HashMap<>();
|
| 27 | Map<ModelGroupCollector.GroupKey, Set<BlockState>> modelGroups = new HashMap<>();
|
| 28 | input.models()
|
| 29 | .forEach(
|
| 30 | (state, loadedModel) -> {
|
| 31 | List<Property<?>> coloringProperties = coloringPropertiesCache.computeIfAbsent(
|
| 32 | state.getBlock(), block -> List.copyOf(blockColors.getColoringProperties(block))
|
| 33 | );
|
| 34 | ModelGroupCollector.GroupKey key = ModelGroupCollector.GroupKey.create(state, loadedModel, coloringProperties);
|
| 35 | modelGroups.computeIfAbsent(key, k -> Sets.newIdentityHashSet()).add(state);
|
| 36 | }
|
| 37 | );
|
| 38 | int nextModelGroup = 1;
|
| 39 | Object2IntMap<BlockState> result = new Object2IntOpenHashMap<>();
|
| 40 | result.defaultReturnValue(-1);
|
| 41 |
|
| 42 | for (Set<BlockState> states : modelGroups.values()) {
|
| 43 | Iterator<BlockState> it = states.iterator();
|
| 44 |
|
| 45 | while (it.hasNext()) {
|
| 46 | BlockState state = it.next();
|
| 47 | if (state.getRenderShape() != RenderShape.MODEL) {
|
| 48 | it.remove();
|
| 49 | result.put(state, 0);
|
| 50 | }
|
| 51 | }
|
| 52 |
|
| 53 | if (states.size() > 1) {
|
| 54 | int modelGroup = nextModelGroup++;
|
| 55 | states.forEach(blockState -> result.put(blockState, modelGroup));
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | return result;
|
| 60 | }
|
| 61 |
|
| 62 | @Environment(EnvType.CLIENT)
|
| 63 | private record GroupKey(Object equalityGroup, List<Object> coloringValues) {
|
| 64 | public static ModelGroupCollector.GroupKey create(final BlockState state, final BlockStateModel.UnbakedRoot model, final List<Property<?>> coloringProperties) {
|
| 65 | List<Object> coloringValues = getColoringValues(state, coloringProperties);
|
| 66 | Object equalityGroup = model.visualEqualityGroup(state);
|
| 67 | return new ModelGroupCollector.GroupKey(equalityGroup, coloringValues);
|
| 68 | }
|
| 69 |
|
| 70 | private static List<Object> getColoringValues(final BlockState state, final List<Property<?>> coloringProperties) {
|
| 71 | Object[] coloringValues = new Object[coloringProperties.size()];
|
| 72 |
|
| 73 | for (int i = 0; i < coloringProperties.size(); i++) {
|
| 74 | coloringValues[i] = state.getValue(coloringProperties.get(i));
|
| 75 | }
|
| 76 |
|
| 77 | return List.of(coloringValues);
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|