| 1 | package net.minecraft.client.renderer.block.dispatch;
|
| 2 |
|
| 3 | import com.google.common.base.Splitter;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.Iterator;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Objects;
|
| 8 | import java.util.Map.Entry;
|
| 9 | import java.util.function.Predicate;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 13 | import net.minecraft.world.level.block.state.StateHolder;
|
| 14 | import net.minecraft.world.level.block.state.properties.Property;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class VariantSelector {
|
| 19 | private static final Splitter COMMA_SPLITTER = Splitter.on(',');
|
| 20 | private static final Splitter EQUAL_SPLITTER = Splitter.on('=').limit(2);
|
| 21 |
|
| 22 | public static <O, S extends StateHolder<O, S>> Predicate<StateHolder<O, S>> predicate(final StateDefinition<O, S> stateDefinition, final String properties) {
|
| 23 | Map<Property<?>, Comparable<?>> map = new HashMap<>();
|
| 24 |
|
| 25 | for (String keyValue : COMMA_SPLITTER.split(properties)) {
|
| 26 | Iterator<String> iterator = EQUAL_SPLITTER.split(keyValue).iterator();
|
| 27 | if (iterator.hasNext()) {
|
| 28 | String propertyName = iterator.next();
|
| 29 | Property<?> property = stateDefinition.getProperty(propertyName);
|
| 30 | if (property != null && iterator.hasNext()) {
|
| 31 | String propertyValue = iterator.next();
|
| 32 | Comparable<?> value = getValueHelper((Property<Comparable<?>>)property, propertyValue);
|
| 33 | if (value == null) {
|
| 34 | throw new RuntimeException("Unknown value: '" + propertyValue + "' for blockstate property: '" + propertyName + "' " + property.getPossibleValues());
|
| 35 | }
|
| 36 |
|
| 37 | map.put(property, value);
|
| 38 | } else if (!propertyName.isEmpty()) {
|
| 39 | throw new RuntimeException("Unknown blockstate property: '" + propertyName + "'");
|
| 40 | }
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | return input -> {
|
| 45 | for (Entry<Property<?>, Comparable<?>> entry : map.entrySet()) {
|
| 46 | if (!Objects.equals(input.getValue(entry.getKey()), entry.getValue())) {
|
| 47 | return false;
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | return true;
|
| 52 | };
|
| 53 | }
|
| 54 |
|
| 55 | private static <T extends Comparable<T>> @Nullable T getValueHelper(final Property<T> property, final String next) {
|
| 56 | return property.getValue(next).orElse(null);
|
| 57 | }
|
| 58 | }
|
| 59 |
|