| 1 | package net.minecraft.block.state;
|
| 2 |
|
| 3 | import com.google.common.base.Function;
|
| 4 | import com.google.common.base.Joiner;
|
| 5 | import com.google.common.base.Objects;
|
| 6 | import com.google.common.collect.HashBasedTable;
|
| 7 | import com.google.common.collect.ImmutableList;
|
| 8 | import com.google.common.collect.ImmutableMap;
|
| 9 | import com.google.common.collect.ImmutableTable;
|
| 10 | import com.google.common.collect.Iterables;
|
| 11 | import com.google.common.collect.Lists;
|
| 12 | import com.google.common.collect.Maps;
|
| 13 | import com.google.common.collect.Table;
|
| 14 | import java.util.Arrays;
|
| 15 | import java.util.Collection;
|
| 16 | import java.util.Collections;
|
| 17 | import java.util.Comparator;
|
| 18 | import java.util.List;
|
| 19 | import java.util.Map;
|
| 20 | import net.minecraft.block.Block;
|
| 21 | import net.minecraft.block.properties.IProperty;
|
| 22 | import net.minecraft.util.Cartesian;
|
| 23 | import net.minecraft.util.MapPopulator;
|
| 24 |
|
| 25 | public class BlockState
|
| 26 | {
|
| 27 | private static final Joiner COMMA_JOINER = Joiner.on(", ");
|
| 28 | private static final Function<IProperty, String> GET_NAME_FUNC = new Function<IProperty, String>()
|
| 29 | {
|
| 30 | public String apply(IProperty p_apply_1_)
|
| 31 | {
|
| 32 | return p_apply_1_ == null ? "<NULL>" : p_apply_1_.getName();
|
| 33 | }
|
| 34 | };
|
| 35 | private final Block block;
|
| 36 | private final ImmutableList<IProperty> properties;
|
| 37 | private final ImmutableList<IBlockState> validStates;
|
| 38 |
|
| 39 | public BlockState(Block blockIn, IProperty... properties)
|
| 40 | {
|
| 41 | this.block = blockIn;
|
| 42 | Arrays.sort(properties, new Comparator<IProperty>()
|
| 43 | {
|
| 44 | public int compare(IProperty p_compare_1_, IProperty p_compare_2_)
|
| 45 | {
|
| 46 | return p_compare_1_.getName().compareTo(p_compare_2_.getName());
|
| 47 | }
|
| 48 | });
|
| 49 | this.properties = ImmutableList.copyOf(properties);
|
| 50 | Map<Map<IProperty, Comparable>, BlockState.StateImplementation> map = Maps.<Map<IProperty, Comparable>, BlockState.StateImplementation>newLinkedHashMap();
|
| 51 | List<BlockState.StateImplementation> list = Lists.<BlockState.StateImplementation>newArrayList();
|
| 52 |
|
| 53 | for (List<Comparable> list1 : Cartesian.cartesianProduct(this.getAllowedValues()))
|
| 54 | {
|
| 55 | Map<IProperty, Comparable> map1 = MapPopulator.<IProperty, Comparable>createMap(this.properties, list1);
|
| 56 | BlockState.StateImplementation blockstate$stateimplementation = new BlockState.StateImplementation(blockIn, ImmutableMap.copyOf(map1));
|
| 57 | map.put(map1, blockstate$stateimplementation);
|
| 58 | list.add(blockstate$stateimplementation);
|
| 59 | }
|
| 60 |
|
| 61 | for (BlockState.StateImplementation blockstate$stateimplementation1 : list)
|
| 62 | {
|
| 63 | blockstate$stateimplementation1.buildPropertyValueTable(map);
|
| 64 | }
|
| 65 |
|
| 66 | this.validStates = ImmutableList.<IBlockState>copyOf(list);
|
| 67 | }
|
| 68 |
|
| 69 | public ImmutableList<IBlockState> getValidStates()
|
| 70 | {
|
| 71 | return this.validStates;
|
| 72 | }
|
| 73 |
|
| 74 | private List<Iterable<Comparable>> getAllowedValues()
|
| 75 | {
|
| 76 | List<Iterable<Comparable>> list = Lists.<Iterable<Comparable>>newArrayList();
|
| 77 |
|
| 78 | for (int i = 0; i < this.properties.size(); ++i)
|
| 79 | {
|
| 80 | list.add(((IProperty)this.properties.get(i)).getAllowedValues());
|
| 81 | }
|
| 82 |
|
| 83 | return list;
|
| 84 | }
|
| 85 |
|
| 86 | public IBlockState getBaseState()
|
| 87 | {
|
| 88 | return (IBlockState)this.validStates.get(0);
|
| 89 | }
|
| 90 |
|
| 91 | public Block getBlock()
|
| 92 | {
|
| 93 | return this.block;
|
| 94 | }
|
| 95 |
|
| 96 | public Collection<IProperty> getProperties()
|
| 97 | {
|
| 98 | return this.properties;
|
| 99 | }
|
| 100 |
|
| 101 | public String toString()
|
| 102 | {
|
| 103 | return Objects.toStringHelper(this).add("block", Block.blockRegistry.getNameForObject(this.block)).add("properties", Iterables.transform(this.properties, GET_NAME_FUNC)).toString();
|
| 104 | }
|
| 105 |
|
| 106 | static class StateImplementation extends BlockStateBase
|
| 107 | {
|
| 108 | private final Block block;
|
| 109 | private final ImmutableMap<IProperty, Comparable> properties;
|
| 110 | private ImmutableTable<IProperty, Comparable, IBlockState> propertyValueTable;
|
| 111 |
|
| 112 | private StateImplementation(Block blockIn, ImmutableMap<IProperty, Comparable> propertiesIn)
|
| 113 | {
|
| 114 | this.block = blockIn;
|
| 115 | this.properties = propertiesIn;
|
| 116 | }
|
| 117 |
|
| 118 | public Collection<IProperty> getPropertyNames()
|
| 119 | {
|
| 120 | return Collections.<IProperty>unmodifiableCollection(this.properties.keySet());
|
| 121 | }
|
| 122 |
|
| 123 | public <T extends Comparable<T>> T getValue(IProperty<T> property)
|
| 124 | {
|
| 125 | if (!this.properties.containsKey(property))
|
| 126 | {
|
| 127 | throw new IllegalArgumentException("Cannot get property " + property + " as it does not exist in " + this.block.getBlockState());
|
| 128 | }
|
| 129 | else
|
| 130 | {
|
| 131 | return (T)((Comparable)property.getValueClass().cast(this.properties.get(property)));
|
| 132 | }
|
| 133 | }
|
| 134 |
|
| 135 | public <T extends Comparable<T>, V extends T> IBlockState withProperty(IProperty<T> property, V value)
|
| 136 | {
|
| 137 | if (!this.properties.containsKey(property))
|
| 138 | {
|
| 139 | throw new IllegalArgumentException("Cannot set property " + property + " as it does not exist in " + this.block.getBlockState());
|
| 140 | }
|
| 141 | else if (!property.getAllowedValues().contains(value))
|
| 142 | {
|
| 143 | throw new IllegalArgumentException("Cannot set property " + property + " to " + value + " on block " + Block.blockRegistry.getNameForObject(this.block) + ", it is not an allowed value");
|
| 144 | }
|
| 145 | else
|
| 146 | {
|
| 147 | return (IBlockState)(this.properties.get(property) == value ? this : (IBlockState)this.propertyValueTable.get(property, value));
|
| 148 | }
|
| 149 | }
|
| 150 |
|
| 151 | public ImmutableMap<IProperty, Comparable> getProperties()
|
| 152 | {
|
| 153 | return this.properties;
|
| 154 | }
|
| 155 |
|
| 156 | public Block getBlock()
|
| 157 | {
|
| 158 | return this.block;
|
| 159 | }
|
| 160 |
|
| 161 | public boolean equals(Object p_equals_1_)
|
| 162 | {
|
| 163 | return this == p_equals_1_;
|
| 164 | }
|
| 165 |
|
| 166 | public int hashCode()
|
| 167 | {
|
| 168 | return this.properties.hashCode();
|
| 169 | }
|
| 170 |
|
| 171 | public void buildPropertyValueTable(Map<Map<IProperty, Comparable>, BlockState.StateImplementation> map)
|
| 172 | {
|
| 173 | if (this.propertyValueTable != null)
|
| 174 | {
|
| 175 | throw new IllegalStateException();
|
| 176 | }
|
| 177 | else
|
| 178 | {
|
| 179 | Table<IProperty, Comparable, IBlockState> table = HashBasedTable.<IProperty, Comparable, IBlockState>create();
|
| 180 |
|
| 181 | for (IProperty <? extends Comparable > iproperty : this.properties.keySet())
|
| 182 | {
|
| 183 | for (Comparable comparable : iproperty.getAllowedValues())
|
| 184 | {
|
| 185 | if (comparable != this.properties.get(iproperty))
|
| 186 | {
|
| 187 | table.put(iproperty, comparable, map.get(this.getPropertiesWithValue(iproperty, comparable)));
|
| 188 | }
|
| 189 | }
|
| 190 | }
|
| 191 |
|
| 192 | this.propertyValueTable = ImmutableTable.<IProperty, Comparable, IBlockState>copyOf(table);
|
| 193 | }
|
| 194 | }
|
| 195 |
|
| 196 | private Map<IProperty, Comparable> getPropertiesWithValue(IProperty property, Comparable value)
|
| 197 | {
|
| 198 | Map<IProperty, Comparable> map = Maps.<IProperty, Comparable>newHashMap(this.properties);
|
| 199 | map.put(property, value);
|
| 200 | return map;
|
| 201 | }
|
| 202 | }
|
| 203 | }
|
| 204 |
|