| 1 | package net.minecraft.block.properties;
|
| 2 |
|
| 3 | import com.google.common.base.Predicate;
|
| 4 | import com.google.common.base.Predicates;
|
| 5 | import com.google.common.collect.Collections2;
|
| 6 | import com.google.common.collect.ImmutableSet;
|
| 7 | import com.google.common.collect.Lists;
|
| 8 | import com.google.common.collect.Maps;
|
| 9 | import java.util.Collection;
|
| 10 | import java.util.Map;
|
| 11 | import net.minecraft.util.IStringSerializable;
|
| 12 |
|
| 13 | public class PropertyEnum<T extends Enum<T> & IStringSerializable> extends PropertyHelper<T>
|
| 14 | {
|
| 15 | private final ImmutableSet<T> allowedValues;
|
| 16 | private final Map<String, T> nameToValue = Maps.<String, T>newHashMap();
|
| 17 |
|
| 18 | protected PropertyEnum(String name, Class<T> valueClass, Collection<T> allowedValues)
|
| 19 | {
|
| 20 | super(name, valueClass);
|
| 21 | this.allowedValues = ImmutableSet.copyOf(allowedValues);
|
| 22 |
|
| 23 | for (T t : allowedValues)
|
| 24 | {
|
| 25 | String s = ((IStringSerializable)t).getName();
|
| 26 |
|
| 27 | if (this.nameToValue.containsKey(s))
|
| 28 | {
|
| 29 | throw new IllegalArgumentException("Multiple values have the same name \'" + s + "\'");
|
| 30 | }
|
| 31 |
|
| 32 | this.nameToValue.put(s, t);
|
| 33 | }
|
| 34 | }
|
| 35 |
|
| 36 | public Collection<T> getAllowedValues()
|
| 37 | {
|
| 38 | return this.allowedValues;
|
| 39 | }
|
| 40 |
|
| 41 | /**
|
| 42 | * Get the name for the given value.
|
| 43 | */
|
| 44 | public String getName(T value)
|
| 45 | {
|
| 46 | return ((IStringSerializable)value).getName();
|
| 47 | }
|
| 48 |
|
| 49 | public static <T extends Enum<T> & IStringSerializable> PropertyEnum<T> create(String name, Class<T> clazz)
|
| 50 | {
|
| 51 | return create(name, clazz, Predicates.<T>alwaysTrue());
|
| 52 | }
|
| 53 |
|
| 54 | public static <T extends Enum<T> & IStringSerializable> PropertyEnum<T> create(String name, Class<T> clazz, Predicate<T> filter)
|
| 55 | {
|
| 56 | return create(name, clazz, Collections2.<T>filter(Lists.newArrayList(clazz.getEnumConstants()), filter));
|
| 57 | }
|
| 58 |
|
| 59 | public static <T extends Enum<T> & IStringSerializable> PropertyEnum<T> create(String name, Class<T> clazz, T... values)
|
| 60 | {
|
| 61 | return create(name, clazz, Lists.newArrayList(values));
|
| 62 | }
|
| 63 |
|
| 64 | public static <T extends Enum<T> & IStringSerializable> PropertyEnum<T> create(String name, Class<T> clazz, Collection<T> values)
|
| 65 | {
|
| 66 | return new PropertyEnum(name, clazz, values);
|
| 67 | }
|
| 68 | }
|
| 69 |
|