| 1 | package net.minecraft.world.level.block.state.pattern;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.base.MoreObjects;
|
| 5 | import com.google.common.cache.CacheBuilder;
|
| 6 | import com.google.common.cache.CacheLoader;
|
| 7 | import com.google.common.cache.LoadingCache;
|
| 8 | import java.util.function.Predicate;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.Direction;
|
| 11 | import net.minecraft.core.Vec3i;
|
| 12 | import net.minecraft.world.level.LevelReader;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public class BlockPattern {
|
| 16 | private final Predicate<BlockInWorld>[][][] pattern;
|
| 17 | private final int depth;
|
| 18 | private final int height;
|
| 19 | private final int width;
|
| 20 |
|
| 21 | public BlockPattern(final Predicate<BlockInWorld>[][][] pattern) {
|
| 22 | this.pattern = pattern;
|
| 23 | this.depth = pattern.length;
|
| 24 | if (this.depth > 0) {
|
| 25 | this.height = pattern[0].length;
|
| 26 | if (this.height > 0) {
|
| 27 | this.width = pattern[0][0].length;
|
| 28 | } else {
|
| 29 | this.width = 0;
|
| 30 | }
|
| 31 | } else {
|
| 32 | this.height = 0;
|
| 33 | this.width = 0;
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | public int getDepth() {
|
| 38 | return this.depth;
|
| 39 | }
|
| 40 |
|
| 41 | public int getHeight() {
|
| 42 | return this.height;
|
| 43 | }
|
| 44 |
|
| 45 | public int getWidth() {
|
| 46 | return this.width;
|
| 47 | }
|
| 48 |
|
| 49 | @VisibleForTesting
|
| 50 | public Predicate<BlockInWorld>[][][] getPattern() {
|
| 51 | return this.pattern;
|
| 52 | }
|
| 53 |
|
| 54 | @VisibleForTesting
|
| 55 | public BlockPattern.@Nullable BlockPatternMatch matches(final LevelReader level, final BlockPos origin, final Direction forwards, final Direction up) {
|
| 56 | LoadingCache<BlockPos, BlockInWorld> cache = createLevelCache(level, false);
|
| 57 | return this.matches(origin, forwards, up, cache);
|
| 58 | }
|
| 59 |
|
| 60 | private BlockPattern.@Nullable BlockPatternMatch matches(
|
| 61 | final BlockPos origin, final Direction forwards, final Direction up, final LoadingCache<BlockPos, BlockInWorld> cache
|
| 62 | ) {
|
| 63 | for (int x = 0; x < this.width; x++) {
|
| 64 | for (int y = 0; y < this.height; y++) {
|
| 65 | for (int z = 0; z < this.depth; z++) {
|
| 66 | if (!this.pattern[z][y][x].test(cache.getUnchecked(translateAndRotate(origin, forwards, up, x, y, z)))) {
|
| 67 | return null;
|
| 68 | }
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return new BlockPattern.BlockPatternMatch(origin, forwards, up, cache, this.width, this.height, this.depth);
|
| 74 | }
|
| 75 |
|
| 76 | public BlockPattern.@Nullable BlockPatternMatch find(final LevelReader level, final BlockPos origin) {
|
| 77 | LoadingCache<BlockPos, BlockInWorld> cache = createLevelCache(level, false);
|
| 78 | int dist = Math.max(Math.max(this.width, this.height), this.depth);
|
| 79 |
|
| 80 | for (BlockPos testPos : BlockPos.betweenClosed(origin, origin.offset(dist - 1, dist - 1, dist - 1))) {
|
| 81 | for (Direction forwards : Direction.values()) {
|
| 82 | for (Direction up : Direction.values()) {
|
| 83 | if (up != forwards && up != forwards.getOpposite()) {
|
| 84 | BlockPattern.BlockPatternMatch match = this.matches(testPos, forwards, up, cache);
|
| 85 | if (match != null) {
|
| 86 | return match;
|
| 87 | }
|
| 88 | }
|
| 89 | }
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | return null;
|
| 94 | }
|
| 95 |
|
| 96 | public static LoadingCache<BlockPos, BlockInWorld> createLevelCache(final LevelReader level, final boolean loadChunks) {
|
| 97 | return CacheBuilder.newBuilder().build(new BlockPattern.BlockCacheLoader(level, loadChunks));
|
| 98 | }
|
| 99 |
|
| 100 | protected static BlockPos translateAndRotate(
|
| 101 | final BlockPos origin, final Direction forwardsDirection, final Direction upDirection, final int right, final int down, final int forwards
|
| 102 | ) {
|
| 103 | if (forwardsDirection != upDirection && forwardsDirection != upDirection.getOpposite()) {
|
| 104 | Vec3i forwardsVector = new Vec3i(forwardsDirection.getStepX(), forwardsDirection.getStepY(), forwardsDirection.getStepZ());
|
| 105 | Vec3i upVector = new Vec3i(upDirection.getStepX(), upDirection.getStepY(), upDirection.getStepZ());
|
| 106 | Vec3i rightVector = forwardsVector.cross(upVector);
|
| 107 | return origin.offset(
|
| 108 | upVector.getX() * -down + rightVector.getX() * right + forwardsVector.getX() * forwards,
|
| 109 | upVector.getY() * -down + rightVector.getY() * right + forwardsVector.getY() * forwards,
|
| 110 | upVector.getZ() * -down + rightVector.getZ() * right + forwardsVector.getZ() * forwards
|
| 111 | );
|
| 112 | } else {
|
| 113 | throw new IllegalArgumentException("Invalid forwards & up combination");
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | private static class BlockCacheLoader extends CacheLoader<BlockPos, BlockInWorld> {
|
| 118 | private final LevelReader level;
|
| 119 | private final boolean loadChunks;
|
| 120 |
|
| 121 | public BlockCacheLoader(final LevelReader level, final boolean loadChunks) {
|
| 122 | this.level = level;
|
| 123 | this.loadChunks = loadChunks;
|
| 124 | }
|
| 125 |
|
| 126 | public BlockInWorld load(final BlockPos key) {
|
| 127 | return new BlockInWorld(this.level, key, this.loadChunks);
|
| 128 | }
|
| 129 | }
|
| 130 |
|
| 131 | public static class BlockPatternMatch {
|
| 132 | private final BlockPos frontTopLeft;
|
| 133 | private final Direction forwards;
|
| 134 | private final Direction up;
|
| 135 | private final LoadingCache<BlockPos, BlockInWorld> cache;
|
| 136 | private final int width;
|
| 137 | private final int height;
|
| 138 | private final int depth;
|
| 139 |
|
| 140 | public BlockPatternMatch(
|
| 141 | final BlockPos frontTopLeft,
|
| 142 | final Direction forwards,
|
| 143 | final Direction up,
|
| 144 | final LoadingCache<BlockPos, BlockInWorld> cache,
|
| 145 | final int width,
|
| 146 | final int height,
|
| 147 | final int depth
|
| 148 | ) {
|
| 149 | this.frontTopLeft = frontTopLeft;
|
| 150 | this.forwards = forwards;
|
| 151 | this.up = up;
|
| 152 | this.cache = cache;
|
| 153 | this.width = width;
|
| 154 | this.height = height;
|
| 155 | this.depth = depth;
|
| 156 | }
|
| 157 |
|
| 158 | public BlockPos getFrontTopLeft() {
|
| 159 | return this.frontTopLeft;
|
| 160 | }
|
| 161 |
|
| 162 | public Direction getForwards() {
|
| 163 | return this.forwards;
|
| 164 | }
|
| 165 |
|
| 166 | public Direction getUp() {
|
| 167 | return this.up;
|
| 168 | }
|
| 169 |
|
| 170 | public int getWidth() {
|
| 171 | return this.width;
|
| 172 | }
|
| 173 |
|
| 174 | public int getHeight() {
|
| 175 | return this.height;
|
| 176 | }
|
| 177 |
|
| 178 | public int getDepth() {
|
| 179 | return this.depth;
|
| 180 | }
|
| 181 |
|
| 182 | public BlockInWorld getBlock(final int right, final int down, final int forwards) {
|
| 183 | return this.cache.getUnchecked(BlockPattern.translateAndRotate(this.frontTopLeft, this.getForwards(), this.getUp(), right, down, forwards));
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public String toString() {
|
| 188 | return MoreObjects.toStringHelper(this).add("up", this.up).add("forwards", this.forwards).add("frontTopLeft", this.frontTopLeft).toString();
|
| 189 | }
|
| 190 | }
|
| 191 | }
|
| 192 |
|