|
| 1 | +package org.embeddedt.modernfix.forge.dynresources; |
| 2 | + |
| 3 | +import com.google.common.collect.Lists; |
| 4 | +import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; |
| 5 | +import net.minecraft.client.resources.model.ModelResourceLocation; |
| 6 | +import net.minecraft.resources.ResourceLocation; |
| 7 | +import net.minecraft.world.level.block.Block; |
| 8 | +import net.minecraft.world.level.block.state.properties.Property; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +public class ModelLocationBuilder { |
| 15 | + private final Object2ObjectOpenHashMap<Property<?>, PropertyData> propertyToOptionStrings = new Object2ObjectOpenHashMap<>(); |
| 16 | + private final StringBuilder builder = new StringBuilder(); |
| 17 | + |
| 18 | + private record PropertyData(List<String> nameValuePairs, int maxPairLength) {} |
| 19 | + |
| 20 | + public void generateForBlock(Set<ResourceLocation> destinationSet, Block block, ResourceLocation baseLocation) { |
| 21 | + var props = block.getStateDefinition().getProperties(); |
| 22 | + List<List<String>> optionsList = new ArrayList<>(); |
| 23 | + int requiredBuilderSize = Math.max(0, props.size() - 1); // commas |
| 24 | + for (var prop : props) { |
| 25 | + var data = propertyToOptionStrings.computeIfAbsent(prop, ModelLocationBuilder::computePropertyOptions); |
| 26 | + optionsList.add(data.nameValuePairs); |
| 27 | + requiredBuilderSize += data.maxPairLength; |
| 28 | + } |
| 29 | + var product = Lists.cartesianProduct(optionsList); |
| 30 | + int count = product.size(); |
| 31 | + int tupleEntryCount = optionsList.size(); |
| 32 | + StringBuilder stringbuilder = this.builder; |
| 33 | + stringbuilder.ensureCapacity(requiredBuilderSize); |
| 34 | + for (int i = 0; i < count; i++) { |
| 35 | + stringbuilder.setLength(0); |
| 36 | + var result = product.get(i); |
| 37 | + for (int j = 0; j < tupleEntryCount; j++) { |
| 38 | + if (j != 0) { |
| 39 | + stringbuilder.append(','); |
| 40 | + } |
| 41 | + stringbuilder.append(result.get(j)); |
| 42 | + } |
| 43 | + destinationSet.add(new ModelResourceLocation(baseLocation, stringbuilder.toString())); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private static PropertyData computePropertyOptions(Property<?> prop) { |
| 48 | + List<String> valuesList = new ArrayList<>(prop.getPossibleValues().size()); |
| 49 | + int maxLength = 0; |
| 50 | + for (var val : prop.getPossibleValues()) { |
| 51 | + String pair = prop.getName() + "=" + getValueName(prop, val); |
| 52 | + valuesList.add(pair); |
| 53 | + maxLength = Math.max(pair.length(), maxLength); |
| 54 | + } |
| 55 | + return new PropertyData(List.copyOf(valuesList), maxLength); |
| 56 | + } |
| 57 | + |
| 58 | + private static <T extends Comparable<T>> String getValueName(Property<T> property, Comparable<?> value) { |
| 59 | + return property.getName((T)value); |
| 60 | + } |
| 61 | +} |
0 commit comments