59 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| package net.minecraft.client.renderer.block.model;
 | |
| 
 | |
| 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.fabricmc.api.EnvType;
 | |
| import net.fabricmc.api.Environment;
 | |
| 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.jetbrains.annotations.Nullable;
 | |
| 
 | |
| @Environment(EnvType.CLIENT)
 | |
| public class VariantSelector {
 | |
| 	private static final Splitter COMMA_SPLITTER = Splitter.on(',');
 | |
| 	private static final Splitter EQUAL_SPLITTER = Splitter.on('=').limit(2);
 | |
| 
 | |
| 	public static <O, S extends StateHolder<O, S>> Predicate<StateHolder<O, S>> predicate(StateDefinition<O, S> stateDefinition, String value) {
 | |
| 		Map<Property<?>, Comparable<?>> map = new HashMap();
 | |
| 
 | |
| 		for (String string : COMMA_SPLITTER.split(value)) {
 | |
| 			Iterator<String> iterator = EQUAL_SPLITTER.split(string).iterator();
 | |
| 			if (iterator.hasNext()) {
 | |
| 				String string2 = (String)iterator.next();
 | |
| 				Property<?> property = stateDefinition.getProperty(string2);
 | |
| 				if (property != null && iterator.hasNext()) {
 | |
| 					String string3 = (String)iterator.next();
 | |
| 					Comparable<?> comparable = getValueHelper((Property<Comparable<?>>)property, string3);
 | |
| 					if (comparable == null) {
 | |
| 						throw new RuntimeException("Unknown value: '" + string3 + "' for blockstate property: '" + string2 + "' " + property.getPossibleValues());
 | |
| 					}
 | |
| 
 | |
| 					map.put(property, comparable);
 | |
| 				} else if (!string2.isEmpty()) {
 | |
| 					throw new RuntimeException("Unknown blockstate property: '" + string2 + "'");
 | |
| 				}
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		return stateHolder -> {
 | |
| 			for (Entry<Property<?>, Comparable<?>> entry : map.entrySet()) {
 | |
| 				if (!Objects.equals(stateHolder.getValue((Property)entry.getKey()), entry.getValue())) {
 | |
| 					return false;
 | |
| 				}
 | |
| 			}
 | |
| 
 | |
| 			return true;
 | |
| 		};
 | |
| 	}
 | |
| 
 | |
| 	@Nullable
 | |
| 	private static <T extends Comparable<T>> T getValueHelper(Property<T> property, String value) {
 | |
| 		return (T)property.getValue(value).orElse(null);
 | |
| 	}
 | |
| }
 |