| 1 | package net.minecraft.world.level.block.state.pattern;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import it.unimi.dsi.fastutil.chars.CharOpenHashSet;
|
| 6 | import it.unimi.dsi.fastutil.chars.CharSet;
|
| 7 | import java.lang.reflect.Array;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.function.Predicate;
|
| 11 | import org.apache.commons.lang3.ArrayUtils;
|
| 12 | import org.apache.commons.lang3.StringUtils;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class BlockPatternBuilder {
|
| 16 | private final List<String[]> pattern = Lists.newArrayList();
|
| 17 | private final Map<Character, Predicate<@Nullable BlockInWorld>> lookup = Maps.newHashMap();
|
| 18 | private int height;
|
| 19 | private int width;
|
| 20 | private final CharSet unknownCharacters = new CharOpenHashSet();
|
| 21 |
|
| 22 | private BlockPatternBuilder() {
|
| 23 | this.lookup.put(' ', blockInWorld -> true);
|
| 24 | }
|
| 25 |
|
| 26 | public BlockPatternBuilder aisle(final String... aisle) {
|
| 27 | if (!ArrayUtils.isEmpty(aisle) && !StringUtils.isEmpty(aisle[0])) {
|
| 28 | if (this.pattern.isEmpty()) {
|
| 29 | this.height = aisle.length;
|
| 30 | this.width = aisle[0].length();
|
| 31 | }
|
| 32 |
|
| 33 | if (aisle.length != this.height) {
|
| 34 | throw new IllegalArgumentException("Expected aisle with height of " + this.height + ", but was given one with a height of " + aisle.length + ")");
|
| 35 | }
|
| 36 |
|
| 37 | for (String row : aisle) {
|
| 38 | if (row.length() != this.width) {
|
| 39 | throw new IllegalArgumentException(
|
| 40 | "Not all rows in the given aisle are the correct width (expected " + this.width + ", found one with " + row.length() + ")"
|
| 41 | );
|
| 42 | }
|
| 43 |
|
| 44 | for (char c : row.toCharArray()) {
|
| 45 | if (!this.lookup.containsKey(c)) {
|
| 46 | this.unknownCharacters.add(c);
|
| 47 | }
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | this.pattern.add(aisle);
|
| 52 | return this;
|
| 53 | } else {
|
| 54 | throw new IllegalArgumentException("Empty pattern for aisle");
|
| 55 | }
|
| 56 | }
|
| 57 |
|
| 58 | public static BlockPatternBuilder start() {
|
| 59 | return new BlockPatternBuilder();
|
| 60 | }
|
| 61 |
|
| 62 | public BlockPatternBuilder where(final char character, final Predicate<@Nullable BlockInWorld> predicate) {
|
| 63 | this.lookup.put(character, predicate);
|
| 64 | this.unknownCharacters.remove(character);
|
| 65 | return this;
|
| 66 | }
|
| 67 |
|
| 68 | public BlockPattern build() {
|
| 69 | return new BlockPattern(this.createPattern());
|
| 70 | }
|
| 71 |
|
| 72 | private Predicate<BlockInWorld>[][][] createPattern() {
|
| 73 | if (!this.unknownCharacters.isEmpty()) {
|
| 74 | throw new IllegalStateException("Predicates for character(s) " + this.unknownCharacters + " are missing");
|
| 75 | }
|
| 76 |
|
| 77 | Predicate<BlockInWorld>[][][] result = (Predicate<BlockInWorld>[][][])Array.newInstance(Predicate.class, this.pattern.size(), this.height, this.width);
|
| 78 |
|
| 79 | for (int aisle = 0; aisle < this.pattern.size(); aisle++) {
|
| 80 | for (int row = 0; row < this.height; row++) {
|
| 81 | for (int col = 0; col < this.width; col++) {
|
| 82 | result[aisle][row][col] = this.lookup.get(this.pattern.get(aisle)[row].charAt(col));
|
| 83 | }
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | return result;
|
| 88 | }
|
| 89 | }
|
| 90 |
|