| 1 | package net.minecraft.block.state.pattern;
|
| 2 |
|
| 3 | import com.google.common.base.Joiner;
|
| 4 | import com.google.common.base.Predicate;
|
| 5 | import com.google.common.base.Predicates;
|
| 6 | import com.google.common.collect.Lists;
|
| 7 | import com.google.common.collect.Maps;
|
| 8 | import java.lang.reflect.Array;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Map.Entry;
|
| 12 | import net.minecraft.block.state.BlockWorldState;
|
| 13 | import org.apache.commons.lang3.ArrayUtils;
|
| 14 | import org.apache.commons.lang3.StringUtils;
|
| 15 |
|
| 16 | public class FactoryBlockPattern
|
| 17 | {
|
| 18 | private static final Joiner COMMA_JOIN = Joiner.on(",");
|
| 19 | private final List<String[]> depth = Lists.<String[]>newArrayList();
|
| 20 | private final Map<Character, Predicate<BlockWorldState>> symbolMap = Maps.<Character, Predicate<BlockWorldState>>newHashMap();
|
| 21 | private int aisleHeight;
|
| 22 | private int rowWidth;
|
| 23 |
|
| 24 | private FactoryBlockPattern()
|
| 25 | {
|
| 26 | this.symbolMap.put(' ', Predicates.<BlockWorldState>alwaysTrue());
|
| 27 | }
|
| 28 |
|
| 29 | public FactoryBlockPattern aisle(String... aisle)
|
| 30 | {
|
| 31 | if (!ArrayUtils.isEmpty((Object[])aisle) && !StringUtils.isEmpty(aisle[0]))
|
| 32 | {
|
| 33 | if (this.depth.isEmpty())
|
| 34 | {
|
| 35 | this.aisleHeight = aisle.length;
|
| 36 | this.rowWidth = aisle[0].length();
|
| 37 | }
|
| 38 |
|
| 39 | if (aisle.length != this.aisleHeight)
|
| 40 | {
|
| 41 | throw new IllegalArgumentException("Expected aisle with height of " + this.aisleHeight + ", but was given one with a height of " + aisle.length + ")");
|
| 42 | }
|
| 43 | else
|
| 44 | {
|
| 45 | for (String s : aisle)
|
| 46 | {
|
| 47 | if (s.length() != this.rowWidth)
|
| 48 | {
|
| 49 | throw new IllegalArgumentException("Not all rows in the given aisle are the correct width (expected " + this.rowWidth + ", found one with " + s.length() + ")");
|
| 50 | }
|
| 51 |
|
| 52 | for (char c0 : s.toCharArray())
|
| 53 | {
|
| 54 | if (!this.symbolMap.containsKey(Character.valueOf(c0)))
|
| 55 | {
|
| 56 | this.symbolMap.put(Character.valueOf(c0), (Predicate<BlockWorldState>)null);
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | this.depth.add(aisle);
|
| 62 | return this;
|
| 63 | }
|
| 64 | }
|
| 65 | else
|
| 66 | {
|
| 67 | throw new IllegalArgumentException("Empty pattern for aisle");
|
| 68 | }
|
| 69 | }
|
| 70 |
|
| 71 | public static FactoryBlockPattern start()
|
| 72 | {
|
| 73 | return new FactoryBlockPattern();
|
| 74 | }
|
| 75 |
|
| 76 | public FactoryBlockPattern where(char symbol, Predicate<BlockWorldState> blockMatcher)
|
| 77 | {
|
| 78 | this.symbolMap.put(Character.valueOf(symbol), blockMatcher);
|
| 79 | return this;
|
| 80 | }
|
| 81 |
|
| 82 | public BlockPattern build()
|
| 83 | {
|
| 84 | return new BlockPattern(this.makePredicateArray());
|
| 85 | }
|
| 86 |
|
| 87 | private Predicate<BlockWorldState>[][][] makePredicateArray()
|
| 88 | {
|
| 89 | this.checkMissingPredicates();
|
| 90 | Predicate<BlockWorldState>[][][] predicate = (Predicate[][][])((Predicate[][][])Array.newInstance(Predicate.class, new int[] {this.depth.size(), this.aisleHeight, this.rowWidth}));
|
| 91 |
|
| 92 | for (int i = 0; i < this.depth.size(); ++i)
|
| 93 | {
|
| 94 | for (int j = 0; j < this.aisleHeight; ++j)
|
| 95 | {
|
| 96 | for (int k = 0; k < this.rowWidth; ++k)
|
| 97 | {
|
| 98 | predicate[i][j][k] = (Predicate)this.symbolMap.get(Character.valueOf(((String[])this.depth.get(i))[j].charAt(k)));
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | return predicate;
|
| 104 | }
|
| 105 |
|
| 106 | private void checkMissingPredicates()
|
| 107 | {
|
| 108 | List<Character> list = Lists.<Character>newArrayList();
|
| 109 |
|
| 110 | for (Entry<Character, Predicate<BlockWorldState>> entry : this.symbolMap.entrySet())
|
| 111 | {
|
| 112 | if (entry.getValue() == null)
|
| 113 | {
|
| 114 | list.add(entry.getKey());
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | if (!list.isEmpty())
|
| 119 | {
|
| 120 | throw new IllegalStateException("Predicates for character(s) " + COMMA_JOIN.join(list) + " are missing");
|
| 121 | }
|
| 122 | }
|
| 123 | }
|
| 124 |
|