package net.minecraft.client.renderer.block.dispatch; import com.google.common.base.Splitter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Objects; import java.util.Map.Entry; import java.util.function.Predicate; import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.StateHolder; import net.minecraft.world.level.block.state.properties.Property; import org.jspecify.annotations.Nullable; public class VariantSelector { private static final Splitter COMMA_SPLITTER = Splitter.on(','); private static final Splitter EQUAL_SPLITTER = Splitter.on('=').limit(2); public static > Predicate> predicate(final StateDefinition stateDefinition, final String properties) { Map, Comparable> map = new HashMap(); for (String keyValue : COMMA_SPLITTER.split(properties)) { Iterator iterator = EQUAL_SPLITTER.split(keyValue).iterator(); if (iterator.hasNext()) { String propertyName = (String)iterator.next(); Property property = stateDefinition.getProperty(propertyName); if (property != null && iterator.hasNext()) { String propertyValue = (String)iterator.next(); Comparable value = getValueHelper((Property>)property, propertyValue); if (value == null) { throw new RuntimeException("Unknown value: '" + propertyValue + "' for blockstate property: '" + propertyName + "' " + property.getPossibleValues()); } map.put(property, value); } else if (!propertyName.isEmpty()) { throw new RuntimeException("Unknown blockstate property: '" + propertyName + "'"); } } } return input -> { for (Entry, Comparable> entry : map.entrySet()) { if (!Objects.equals(input.getValue((Property)entry.getKey()), entry.getValue())) { return false; } } return true; }; } @Nullable private static > T getValueHelper(final Property property, final String next) { return (T)property.getValue(next).orElse(null); } }