package net.minecraft.world.level.block.state.properties; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; import net.minecraft.util.StringRepresentable; public final class EnumProperty & StringRepresentable> extends Property { private final List values; /** * Map of names to Enum values */ private final Map names; private final int[] ordinalToIndex; private EnumProperty(String name, Class clazz, List values) { super(name, clazz); if (values.isEmpty()) { throw new IllegalArgumentException("Trying to make empty EnumProperty '" + name + "'"); } else { this.values = List.copyOf(values); T[] enums = (T[])clazz.getEnumConstants(); this.ordinalToIndex = new int[enums.length]; for (T enum_ : enums) { this.ordinalToIndex[enum_.ordinal()] = values.indexOf(enum_); } Builder builder = ImmutableMap.builder(); for (T enum2 : values) { String string = enum2.getSerializedName(); builder.put(string, enum2); } this.names = builder.buildOrThrow(); } } @Override public List getPossibleValues() { return this.values; } @Override public Optional getValue(String value) { return Optional.ofNullable((Enum)this.names.get(value)); } public String getName(T value) { return value.getSerializedName(); } public int getInternalIndex(T enum_) { return this.ordinalToIndex[enum_.ordinal()]; } @Override public boolean equals(Object object) { if (this == object) { return true; } else { return object instanceof EnumProperty enumProperty && super.equals(object) ? this.values.equals(enumProperty.values) : false; } } @Override public int generateHashCode() { int i = super.generateHashCode(); return 31 * i + this.values.hashCode(); } /** * Create a new EnumProperty with all Enum constants of the given class. */ public static & StringRepresentable> EnumProperty create(String name, Class clazz) { return create(name, clazz, (Predicate)(enum_ -> true)); } /** * Create a new EnumProperty with all Enum constants of the given class that match the given Predicate. */ public static & StringRepresentable> EnumProperty create(String name, Class clazz, Predicate filter) { return create(name, clazz, (List)Arrays.stream((Enum[])clazz.getEnumConstants()).filter(filter).collect(Collectors.toList())); } /** * Create a new EnumProperty with the specified values */ @SafeVarargs public static & StringRepresentable> EnumProperty create(String name, Class clazz, T... values) { return create(name, clazz, List.of(values)); } public static & StringRepresentable> EnumProperty create(String name, Class clazz, List values) { return new EnumProperty<>(name, clazz, values); } }