| 1 | package net.minecraft.block.state.pattern;
|
| 2 |
|
| 3 | import com.google.common.base.Objects;
|
| 4 | import com.google.common.base.Predicate;
|
| 5 | import com.google.common.cache.CacheBuilder;
|
| 6 | import com.google.common.cache.LoadingCache;
|
| 7 | import net.minecraft.block.state.BlockWorldState;
|
| 8 | import net.minecraft.util.BlockPos;
|
| 9 | import net.minecraft.util.EnumFacing;
|
| 10 | import net.minecraft.util.Vec3i;
|
| 11 | import net.minecraft.world.World;
|
| 12 |
|
| 13 | public class BlockPattern
|
| 14 | {
|
| 15 | private final Predicate<BlockWorldState>[][][] blockMatches;
|
| 16 | private final int fingerLength;
|
| 17 | private final int thumbLength;
|
| 18 | private final int palmLength;
|
| 19 |
|
| 20 | public BlockPattern(Predicate<BlockWorldState>[][][] predicatesIn)
|
| 21 | {
|
| 22 | this.blockMatches = predicatesIn;
|
| 23 | this.fingerLength = predicatesIn.length;
|
| 24 |
|
| 25 | if (this.fingerLength > 0)
|
| 26 | {
|
| 27 | this.thumbLength = predicatesIn[0].length;
|
| 28 |
|
| 29 | if (this.thumbLength > 0)
|
| 30 | {
|
| 31 | this.palmLength = predicatesIn[0][0].length;
|
| 32 | }
|
| 33 | else
|
| 34 | {
|
| 35 | this.palmLength = 0;
|
| 36 | }
|
| 37 | }
|
| 38 | else
|
| 39 | {
|
| 40 | this.thumbLength = 0;
|
| 41 | this.palmLength = 0;
|
| 42 | }
|
| 43 | }
|
| 44 |
|
| 45 | public int getThumbLength()
|
| 46 | {
|
| 47 | return this.thumbLength;
|
| 48 | }
|
| 49 |
|
| 50 | public int getPalmLength()
|
| 51 | {
|
| 52 | return this.palmLength;
|
| 53 | }
|
| 54 |
|
| 55 | /**
|
| 56 | * checks that the given pattern & rotation is at the block co-ordinates.
|
| 57 | */
|
| 58 | private BlockPattern.PatternHelper checkPatternAt(BlockPos pos, EnumFacing finger, EnumFacing thumb, LoadingCache<BlockPos, BlockWorldState> lcache)
|
| 59 | {
|
| 60 | for (int i = 0; i < this.palmLength; ++i)
|
| 61 | {
|
| 62 | for (int j = 0; j < this.thumbLength; ++j)
|
| 63 | {
|
| 64 | for (int k = 0; k < this.fingerLength; ++k)
|
| 65 | {
|
| 66 | if (!this.blockMatches[k][j][i].apply(lcache.getUnchecked(translateOffset(pos, finger, thumb, i, j, k))))
|
| 67 | {
|
| 68 | return null;
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | return new BlockPattern.PatternHelper(pos, finger, thumb, lcache, this.palmLength, this.thumbLength, this.fingerLength);
|
| 75 | }
|
| 76 |
|
| 77 | /**
|
| 78 | * Calculates whether the given world position matches the pattern. Warning, fairly heavy function. @return a
|
| 79 | * BlockPattern.PatternHelper if found, null otherwise.
|
| 80 | */
|
| 81 | public BlockPattern.PatternHelper match(World worldIn, BlockPos pos)
|
| 82 | {
|
| 83 | LoadingCache<BlockPos, BlockWorldState> loadingcache = func_181627_a(worldIn, false);
|
| 84 | int i = Math.max(Math.max(this.palmLength, this.thumbLength), this.fingerLength);
|
| 85 |
|
| 86 | for (BlockPos blockpos : BlockPos.getAllInBox(pos, pos.add(i - 1, i - 1, i - 1)))
|
| 87 | {
|
| 88 | for (EnumFacing enumfacing : EnumFacing.values())
|
| 89 | {
|
| 90 | for (EnumFacing enumfacing1 : EnumFacing.values())
|
| 91 | {
|
| 92 | if (enumfacing1 != enumfacing && enumfacing1 != enumfacing.getOpposite())
|
| 93 | {
|
| 94 | BlockPattern.PatternHelper blockpattern$patternhelper = this.checkPatternAt(blockpos, enumfacing, enumfacing1, loadingcache);
|
| 95 |
|
| 96 | if (blockpattern$patternhelper != null)
|
| 97 | {
|
| 98 | return blockpattern$patternhelper;
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | return null;
|
| 106 | }
|
| 107 |
|
| 108 | public static LoadingCache<BlockPos, BlockWorldState> func_181627_a(World p_181627_0_, boolean p_181627_1_)
|
| 109 | {
|
| 110 | return CacheBuilder.newBuilder().<BlockPos, BlockWorldState>build(new BlockPattern.CacheLoader(p_181627_0_, p_181627_1_));
|
| 111 | }
|
| 112 |
|
| 113 | /**
|
| 114 | * Offsets the position of pos in the direction of finger and thumb facing by offset amounts, follows the right-hand
|
| 115 | * rule for cross products (finger, thumb, palm) @return A new BlockPos offset in the facing directions
|
| 116 | */
|
| 117 | protected static BlockPos translateOffset(BlockPos pos, EnumFacing finger, EnumFacing thumb, int palmOffset, int thumbOffset, int fingerOffset)
|
| 118 | {
|
| 119 | if (finger != thumb && finger != thumb.getOpposite())
|
| 120 | {
|
| 121 | Vec3i vec3i = new Vec3i(finger.getFrontOffsetX(), finger.getFrontOffsetY(), finger.getFrontOffsetZ());
|
| 122 | Vec3i vec3i1 = new Vec3i(thumb.getFrontOffsetX(), thumb.getFrontOffsetY(), thumb.getFrontOffsetZ());
|
| 123 | Vec3i vec3i2 = vec3i.crossProduct(vec3i1);
|
| 124 | return pos.add(vec3i1.getX() * -thumbOffset + vec3i2.getX() * palmOffset + vec3i.getX() * fingerOffset, vec3i1.getY() * -thumbOffset + vec3i2.getY() * palmOffset + vec3i.getY() * fingerOffset, vec3i1.getZ() * -thumbOffset + vec3i2.getZ() * palmOffset + vec3i.getZ() * fingerOffset);
|
| 125 | }
|
| 126 | else
|
| 127 | {
|
| 128 | throw new IllegalArgumentException("Invalid forwards & up combination");
|
| 129 | }
|
| 130 | }
|
| 131 |
|
| 132 | static class CacheLoader extends com.google.common.cache.CacheLoader<BlockPos, BlockWorldState>
|
| 133 | {
|
| 134 | private final World world;
|
| 135 | private final boolean field_181626_b;
|
| 136 |
|
| 137 | public CacheLoader(World worldIn, boolean p_i46460_2_)
|
| 138 | {
|
| 139 | this.world = worldIn;
|
| 140 | this.field_181626_b = p_i46460_2_;
|
| 141 | }
|
| 142 |
|
| 143 | public BlockWorldState load(BlockPos p_load_1_) throws Exception
|
| 144 | {
|
| 145 | return new BlockWorldState(this.world, p_load_1_, this.field_181626_b);
|
| 146 | }
|
| 147 | }
|
| 148 |
|
| 149 | public static class PatternHelper
|
| 150 | {
|
| 151 | private final BlockPos pos;
|
| 152 | private final EnumFacing finger;
|
| 153 | private final EnumFacing thumb;
|
| 154 | private final LoadingCache<BlockPos, BlockWorldState> lcache;
|
| 155 | private final int field_181120_e;
|
| 156 | private final int field_181121_f;
|
| 157 | private final int field_181122_g;
|
| 158 |
|
| 159 | public PatternHelper(BlockPos posIn, EnumFacing fingerIn, EnumFacing thumbIn, LoadingCache<BlockPos, BlockWorldState> lcacheIn, int p_i46378_5_, int p_i46378_6_, int p_i46378_7_)
|
| 160 | {
|
| 161 | this.pos = posIn;
|
| 162 | this.finger = fingerIn;
|
| 163 | this.thumb = thumbIn;
|
| 164 | this.lcache = lcacheIn;
|
| 165 | this.field_181120_e = p_i46378_5_;
|
| 166 | this.field_181121_f = p_i46378_6_;
|
| 167 | this.field_181122_g = p_i46378_7_;
|
| 168 | }
|
| 169 |
|
| 170 | public BlockPos getPos()
|
| 171 | {
|
| 172 | return this.pos;
|
| 173 | }
|
| 174 |
|
| 175 | public EnumFacing getFinger()
|
| 176 | {
|
| 177 | return this.finger;
|
| 178 | }
|
| 179 |
|
| 180 | public EnumFacing getThumb()
|
| 181 | {
|
| 182 | return this.thumb;
|
| 183 | }
|
| 184 |
|
| 185 | public int func_181118_d()
|
| 186 | {
|
| 187 | return this.field_181120_e;
|
| 188 | }
|
| 189 |
|
| 190 | public int func_181119_e()
|
| 191 | {
|
| 192 | return this.field_181121_f;
|
| 193 | }
|
| 194 |
|
| 195 | public BlockWorldState translateOffset(int palmOffset, int thumbOffset, int fingerOffset)
|
| 196 | {
|
| 197 | return (BlockWorldState)this.lcache.getUnchecked(BlockPattern.translateOffset(this.pos, this.getFinger(), this.getThumb(), palmOffset, thumbOffset, fingerOffset));
|
| 198 | }
|
| 199 |
|
| 200 | public String toString()
|
| 201 | {
|
| 202 | return Objects.toStringHelper(this).add("up", this.thumb).add("forwards", this.finger).add("frontTopLeft", this.pos).toString();
|
| 203 | }
|
| 204 | }
|
| 205 | }
|
| 206 |
|