| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import com.google.common.collect.AbstractIterator;
|
| 4 | import java.util.Iterator;
|
| 5 | import net.minecraft.entity.Entity;
|
| 6 |
|
| 7 | public class BlockPos extends Vec3i
|
| 8 | {
|
| 9 | /** The BlockPos with all coordinates 0 */
|
| 10 | public static final BlockPos ORIGIN = new BlockPos(0, 0, 0);
|
| 11 | private static final int NUM_X_BITS = 1 + MathHelper.calculateLogBaseTwo(MathHelper.roundUpToPowerOfTwo(30000000));
|
| 12 | private static final int NUM_Z_BITS = NUM_X_BITS;
|
| 13 | private static final int NUM_Y_BITS = 64 - NUM_X_BITS - NUM_Z_BITS;
|
| 14 | private static final int Y_SHIFT = 0 + NUM_Z_BITS;
|
| 15 | private static final int X_SHIFT = Y_SHIFT + NUM_Y_BITS;
|
| 16 | private static final long X_MASK = (1L << NUM_X_BITS) - 1L;
|
| 17 | private static final long Y_MASK = (1L << NUM_Y_BITS) - 1L;
|
| 18 | private static final long Z_MASK = (1L << NUM_Z_BITS) - 1L;
|
| 19 |
|
| 20 | public BlockPos(int x, int y, int z)
|
| 21 | {
|
| 22 | super(x, y, z);
|
| 23 | }
|
| 24 |
|
| 25 | public BlockPos(double x, double y, double z)
|
| 26 | {
|
| 27 | super(x, y, z);
|
| 28 | }
|
| 29 |
|
| 30 | public BlockPos(Entity source)
|
| 31 | {
|
| 32 | this(source.posX, source.posY, source.posZ);
|
| 33 | }
|
| 34 |
|
| 35 | public BlockPos(Vec3 source)
|
| 36 | {
|
| 37 | this(source.xCoord, source.yCoord, source.zCoord);
|
| 38 | }
|
| 39 |
|
| 40 | public BlockPos(Vec3i source)
|
| 41 | {
|
| 42 | this(source.getX(), source.getY(), source.getZ());
|
| 43 | }
|
| 44 |
|
| 45 | /**
|
| 46 | * Add the given coordinates to the coordinates of this BlockPos
|
| 47 | */
|
| 48 | public BlockPos add(double x, double y, double z)
|
| 49 | {
|
| 50 | return x == 0.0D && y == 0.0D && z == 0.0D ? this : new BlockPos((double)this.getX() + x, (double)this.getY() + y, (double)this.getZ() + z);
|
| 51 | }
|
| 52 |
|
| 53 | /**
|
| 54 | * Add the given coordinates to the coordinates of this BlockPos
|
| 55 | */
|
| 56 | public BlockPos add(int x, int y, int z)
|
| 57 | {
|
| 58 | return x == 0 && y == 0 && z == 0 ? this : new BlockPos(this.getX() + x, this.getY() + y, this.getZ() + z);
|
| 59 | }
|
| 60 |
|
| 61 | /**
|
| 62 | * Add the given Vector to this BlockPos
|
| 63 | */
|
| 64 | public BlockPos add(Vec3i vec)
|
| 65 | {
|
| 66 | return vec.getX() == 0 && vec.getY() == 0 && vec.getZ() == 0 ? this : new BlockPos(this.getX() + vec.getX(), this.getY() + vec.getY(), this.getZ() + vec.getZ());
|
| 67 | }
|
| 68 |
|
| 69 | /**
|
| 70 | * Subtract the given Vector from this BlockPos
|
| 71 | */
|
| 72 | public BlockPos subtract(Vec3i vec)
|
| 73 | {
|
| 74 | return vec.getX() == 0 && vec.getY() == 0 && vec.getZ() == 0 ? this : new BlockPos(this.getX() - vec.getX(), this.getY() - vec.getY(), this.getZ() - vec.getZ());
|
| 75 | }
|
| 76 |
|
| 77 | /**
|
| 78 | * Offset this BlockPos 1 block up
|
| 79 | */
|
| 80 | public BlockPos up()
|
| 81 | {
|
| 82 | return this.up(1);
|
| 83 | }
|
| 84 |
|
| 85 | /**
|
| 86 | * Offset this BlockPos n blocks up
|
| 87 | */
|
| 88 | public BlockPos up(int n)
|
| 89 | {
|
| 90 | return this.offset(EnumFacing.UP, n);
|
| 91 | }
|
| 92 |
|
| 93 | /**
|
| 94 | * Offset this BlockPos 1 block down
|
| 95 | */
|
| 96 | public BlockPos down()
|
| 97 | {
|
| 98 | return this.down(1);
|
| 99 | }
|
| 100 |
|
| 101 | /**
|
| 102 | * Offset this BlockPos n blocks down
|
| 103 | */
|
| 104 | public BlockPos down(int n)
|
| 105 | {
|
| 106 | return this.offset(EnumFacing.DOWN, n);
|
| 107 | }
|
| 108 |
|
| 109 | /**
|
| 110 | * Offset this BlockPos 1 block in northern direction
|
| 111 | */
|
| 112 | public BlockPos north()
|
| 113 | {
|
| 114 | return this.north(1);
|
| 115 | }
|
| 116 |
|
| 117 | /**
|
| 118 | * Offset this BlockPos n blocks in northern direction
|
| 119 | */
|
| 120 | public BlockPos north(int n)
|
| 121 | {
|
| 122 | return this.offset(EnumFacing.NORTH, n);
|
| 123 | }
|
| 124 |
|
| 125 | /**
|
| 126 | * Offset this BlockPos 1 block in southern direction
|
| 127 | */
|
| 128 | public BlockPos south()
|
| 129 | {
|
| 130 | return this.south(1);
|
| 131 | }
|
| 132 |
|
| 133 | /**
|
| 134 | * Offset this BlockPos n blocks in southern direction
|
| 135 | */
|
| 136 | public BlockPos south(int n)
|
| 137 | {
|
| 138 | return this.offset(EnumFacing.SOUTH, n);
|
| 139 | }
|
| 140 |
|
| 141 | /**
|
| 142 | * Offset this BlockPos 1 block in western direction
|
| 143 | */
|
| 144 | public BlockPos west()
|
| 145 | {
|
| 146 | return this.west(1);
|
| 147 | }
|
| 148 |
|
| 149 | /**
|
| 150 | * Offset this BlockPos n blocks in western direction
|
| 151 | */
|
| 152 | public BlockPos west(int n)
|
| 153 | {
|
| 154 | return this.offset(EnumFacing.WEST, n);
|
| 155 | }
|
| 156 |
|
| 157 | /**
|
| 158 | * Offset this BlockPos 1 block in eastern direction
|
| 159 | */
|
| 160 | public BlockPos east()
|
| 161 | {
|
| 162 | return this.east(1);
|
| 163 | }
|
| 164 |
|
| 165 | /**
|
| 166 | * Offset this BlockPos n blocks in eastern direction
|
| 167 | */
|
| 168 | public BlockPos east(int n)
|
| 169 | {
|
| 170 | return this.offset(EnumFacing.EAST, n);
|
| 171 | }
|
| 172 |
|
| 173 | /**
|
| 174 | * Offset this BlockPos 1 block in the given direction
|
| 175 | */
|
| 176 | public BlockPos offset(EnumFacing facing)
|
| 177 | {
|
| 178 | return this.offset(facing, 1);
|
| 179 | }
|
| 180 |
|
| 181 | /**
|
| 182 | * Offsets this BlockPos n blocks in the given direction
|
| 183 | */
|
| 184 | public BlockPos offset(EnumFacing facing, int n)
|
| 185 | {
|
| 186 | return n == 0 ? this : new BlockPos(this.getX() + facing.getFrontOffsetX() * n, this.getY() + facing.getFrontOffsetY() * n, this.getZ() + facing.getFrontOffsetZ() * n);
|
| 187 | }
|
| 188 |
|
| 189 | /**
|
| 190 | * Calculate the cross product of this and the given Vector
|
| 191 | */
|
| 192 | public BlockPos crossProduct(Vec3i vec)
|
| 193 | {
|
| 194 | return new BlockPos(this.getY() * vec.getZ() - this.getZ() * vec.getY(), this.getZ() * vec.getX() - this.getX() * vec.getZ(), this.getX() * vec.getY() - this.getY() * vec.getX());
|
| 195 | }
|
| 196 |
|
| 197 | /**
|
| 198 | * Serialize this BlockPos into a long value
|
| 199 | */
|
| 200 | public long toLong()
|
| 201 | {
|
| 202 | return ((long)this.getX() & X_MASK) << X_SHIFT | ((long)this.getY() & Y_MASK) << Y_SHIFT | ((long)this.getZ() & Z_MASK) << 0;
|
| 203 | }
|
| 204 |
|
| 205 | /**
|
| 206 | * Create a BlockPos from a serialized long value (created by toLong)
|
| 207 | */
|
| 208 | public static BlockPos fromLong(long serialized)
|
| 209 | {
|
| 210 | int i = (int)(serialized << 64 - X_SHIFT - NUM_X_BITS >> 64 - NUM_X_BITS);
|
| 211 | int j = (int)(serialized << 64 - Y_SHIFT - NUM_Y_BITS >> 64 - NUM_Y_BITS);
|
| 212 | int k = (int)(serialized << 64 - NUM_Z_BITS >> 64 - NUM_Z_BITS);
|
| 213 | return new BlockPos(i, j, k);
|
| 214 | }
|
| 215 |
|
| 216 | public static Iterable<BlockPos> getAllInBox(BlockPos from, BlockPos to)
|
| 217 | {
|
| 218 | final BlockPos blockpos = new BlockPos(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ()));
|
| 219 | final BlockPos blockpos1 = new BlockPos(Math.max(from.getX(), to.getX()), Math.max(from.getY(), to.getY()), Math.max(from.getZ(), to.getZ()));
|
| 220 | return new Iterable<BlockPos>()
|
| 221 | {
|
| 222 | public Iterator<BlockPos> iterator()
|
| 223 | {
|
| 224 | return new AbstractIterator<BlockPos>()
|
| 225 | {
|
| 226 | private BlockPos lastReturned = null;
|
| 227 | protected BlockPos computeNext()
|
| 228 | {
|
| 229 | if (this.lastReturned == null)
|
| 230 | {
|
| 231 | this.lastReturned = blockpos;
|
| 232 | return this.lastReturned;
|
| 233 | }
|
| 234 | else if (this.lastReturned.equals(blockpos1))
|
| 235 | {
|
| 236 | return (BlockPos)this.endOfData();
|
| 237 | }
|
| 238 | else
|
| 239 | {
|
| 240 | int i = this.lastReturned.getX();
|
| 241 | int j = this.lastReturned.getY();
|
| 242 | int k = this.lastReturned.getZ();
|
| 243 |
|
| 244 | if (i < blockpos1.getX())
|
| 245 | {
|
| 246 | ++i;
|
| 247 | }
|
| 248 | else if (j < blockpos1.getY())
|
| 249 | {
|
| 250 | i = blockpos.getX();
|
| 251 | ++j;
|
| 252 | }
|
| 253 | else if (k < blockpos1.getZ())
|
| 254 | {
|
| 255 | i = blockpos.getX();
|
| 256 | j = blockpos.getY();
|
| 257 | ++k;
|
| 258 | }
|
| 259 |
|
| 260 | this.lastReturned = new BlockPos(i, j, k);
|
| 261 | return this.lastReturned;
|
| 262 | }
|
| 263 | }
|
| 264 | };
|
| 265 | }
|
| 266 | };
|
| 267 | }
|
| 268 |
|
| 269 | public static Iterable<BlockPos.MutableBlockPos> getAllInBoxMutable(BlockPos from, BlockPos to)
|
| 270 | {
|
| 271 | final BlockPos blockpos = new BlockPos(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ()));
|
| 272 | final BlockPos blockpos1 = new BlockPos(Math.max(from.getX(), to.getX()), Math.max(from.getY(), to.getY()), Math.max(from.getZ(), to.getZ()));
|
| 273 | return new Iterable<BlockPos.MutableBlockPos>()
|
| 274 | {
|
| 275 | public Iterator<BlockPos.MutableBlockPos> iterator()
|
| 276 | {
|
| 277 | return new AbstractIterator<BlockPos.MutableBlockPos>()
|
| 278 | {
|
| 279 | private BlockPos.MutableBlockPos theBlockPos = null;
|
| 280 | protected BlockPos.MutableBlockPos computeNext()
|
| 281 | {
|
| 282 | if (this.theBlockPos == null)
|
| 283 | {
|
| 284 | this.theBlockPos = new BlockPos.MutableBlockPos(blockpos.getX(), blockpos.getY(), blockpos.getZ());
|
| 285 | return this.theBlockPos;
|
| 286 | }
|
| 287 | else if (this.theBlockPos.equals(blockpos1))
|
| 288 | {
|
| 289 | return (BlockPos.MutableBlockPos)this.endOfData();
|
| 290 | }
|
| 291 | else
|
| 292 | {
|
| 293 | int i = this.theBlockPos.getX();
|
| 294 | int j = this.theBlockPos.getY();
|
| 295 | int k = this.theBlockPos.getZ();
|
| 296 |
|
| 297 | if (i < blockpos1.getX())
|
| 298 | {
|
| 299 | ++i;
|
| 300 | }
|
| 301 | else if (j < blockpos1.getY())
|
| 302 | {
|
| 303 | i = blockpos.getX();
|
| 304 | ++j;
|
| 305 | }
|
| 306 | else if (k < blockpos1.getZ())
|
| 307 | {
|
| 308 | i = blockpos.getX();
|
| 309 | j = blockpos.getY();
|
| 310 | ++k;
|
| 311 | }
|
| 312 |
|
| 313 | this.theBlockPos.x = i;
|
| 314 | this.theBlockPos.y = j;
|
| 315 | this.theBlockPos.z = k;
|
| 316 | return this.theBlockPos;
|
| 317 | }
|
| 318 | }
|
| 319 | };
|
| 320 | }
|
| 321 | };
|
| 322 | }
|
| 323 |
|
| 324 | public static final class MutableBlockPos extends BlockPos
|
| 325 | {
|
| 326 | private int x;
|
| 327 | private int y;
|
| 328 | private int z;
|
| 329 |
|
| 330 | public MutableBlockPos()
|
| 331 | {
|
| 332 | this(0, 0, 0);
|
| 333 | }
|
| 334 |
|
| 335 | public MutableBlockPos(int x_, int y_, int z_)
|
| 336 | {
|
| 337 | super(0, 0, 0);
|
| 338 | this.x = x_;
|
| 339 | this.y = y_;
|
| 340 | this.z = z_;
|
| 341 | }
|
| 342 |
|
| 343 | public int getX()
|
| 344 | {
|
| 345 | return this.x;
|
| 346 | }
|
| 347 |
|
| 348 | public int getY()
|
| 349 | {
|
| 350 | return this.y;
|
| 351 | }
|
| 352 |
|
| 353 | public int getZ()
|
| 354 | {
|
| 355 | return this.z;
|
| 356 | }
|
| 357 |
|
| 358 | public BlockPos.MutableBlockPos set(int xIn, int yIn, int zIn)
|
| 359 | {
|
| 360 | this.x = xIn;
|
| 361 | this.y = yIn;
|
| 362 | this.z = zIn;
|
| 363 | return this;
|
| 364 | }
|
| 365 | }
|
| 366 | }
|