| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.google.common.collect.AbstractIterator;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 7 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 8 | import java.util.ArrayDeque;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Queue;
|
| 11 | import java.util.function.BiConsumer;
|
| 12 | import java.util.function.Consumer;
|
| 13 | import java.util.function.Function;
|
| 14 | import java.util.stream.IntStream;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import java.util.stream.StreamSupport;
|
| 17 | import javax.annotation.concurrent.Immutable;
|
| 18 | import net.minecraft.network.FriendlyByteBuf;
|
| 19 | import net.minecraft.network.codec.StreamCodec;
|
| 20 | import net.minecraft.util.Mth;
|
| 21 | import net.minecraft.util.RandomSource;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import net.minecraft.world.level.block.Rotation;
|
| 24 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 25 | import net.minecraft.world.phys.AABB;
|
| 26 | import net.minecraft.world.phys.Vec3;
|
| 27 | import org.apache.commons.lang3.Validate;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | @Immutable
|
| 31 | public class BlockPos extends Vec3i {
|
| 32 | public static final Codec<BlockPos> CODEC = Codec.INT_STREAM
|
| 33 | .<BlockPos>comapFlatMap(
|
| 34 | input -> Util.fixedSize(input, 3).map(ints -> new BlockPos(ints[0], ints[1], ints[2])), pos -> IntStream.of(pos.getX(), pos.getY(), pos.getZ())
|
| 35 | )
|
| 36 | .stable();
|
| 37 | public static final StreamCodec<ByteBuf, BlockPos> STREAM_CODEC = new StreamCodec<ByteBuf, BlockPos>() {
|
| 38 | public BlockPos decode(final ByteBuf input) {
|
| 39 | return FriendlyByteBuf.readBlockPos(input);
|
| 40 | }
|
| 41 |
|
| 42 | public void encode(final ByteBuf output, final BlockPos value) {
|
| 43 | FriendlyByteBuf.writeBlockPos(output, value);
|
| 44 | }
|
| 45 | };
|
| 46 | public static final BlockPos ZERO = new BlockPos(0, 0, 0);
|
| 47 | public static final int PACKED_HORIZONTAL_LENGTH = 1 + Mth.log2(Mth.smallestEncompassingPowerOfTwo(30000000));
|
| 48 | public static final int PACKED_Y_LENGTH = 64 - 2 * PACKED_HORIZONTAL_LENGTH;
|
| 49 | private static final long PACKED_X_MASK = (1L << PACKED_HORIZONTAL_LENGTH) - 1L;
|
| 50 | private static final long PACKED_Y_MASK = (1L << PACKED_Y_LENGTH) - 1L;
|
| 51 | private static final long PACKED_Z_MASK = (1L << PACKED_HORIZONTAL_LENGTH) - 1L;
|
| 52 | private static final int Y_OFFSET = 0;
|
| 53 | private static final int Z_OFFSET = PACKED_Y_LENGTH;
|
| 54 | private static final int X_OFFSET = PACKED_Y_LENGTH + PACKED_HORIZONTAL_LENGTH;
|
| 55 | public static final int MAX_HORIZONTAL_COORDINATE = (1 << PACKED_HORIZONTAL_LENGTH) / 2 - 1;
|
| 56 |
|
| 57 | public BlockPos(final int x, final int y, final int z) {
|
| 58 | super(x, y, z);
|
| 59 | }
|
| 60 |
|
| 61 | public static long offset(final long blockNode, final Direction offset) {
|
| 62 | return offset(blockNode, offset.getStepX(), offset.getStepY(), offset.getStepZ());
|
| 63 | }
|
| 64 |
|
| 65 | public static long offset(final long blockNode, final int stepX, final int stepY, final int stepZ) {
|
| 66 | return asLong(getX(blockNode) + stepX, getY(blockNode) + stepY, getZ(blockNode) + stepZ);
|
| 67 | }
|
| 68 |
|
| 69 | public static int getX(final long blockNode) {
|
| 70 | return (int)(blockNode << 64 - X_OFFSET - PACKED_HORIZONTAL_LENGTH >> 64 - PACKED_HORIZONTAL_LENGTH);
|
| 71 | }
|
| 72 |
|
| 73 | public static int getY(final long blockNode) {
|
| 74 | return (int)(blockNode << 64 - PACKED_Y_LENGTH >> 64 - PACKED_Y_LENGTH);
|
| 75 | }
|
| 76 |
|
| 77 | public static int getZ(final long blockNode) {
|
| 78 | return (int)(blockNode << 64 - Z_OFFSET - PACKED_HORIZONTAL_LENGTH >> 64 - PACKED_HORIZONTAL_LENGTH);
|
| 79 | }
|
| 80 |
|
| 81 | public static BlockPos of(final long blockNode) {
|
| 82 | return new BlockPos(getX(blockNode), getY(blockNode), getZ(blockNode));
|
| 83 | }
|
| 84 |
|
| 85 | public static BlockPos containing(final double x, final double y, final double z) {
|
| 86 | return new BlockPos(Mth.floor(x), Mth.floor(y), Mth.floor(z));
|
| 87 | }
|
| 88 |
|
| 89 | public static BlockPos containing(final Position pos) {
|
| 90 | return containing(pos.x(), pos.y(), pos.z());
|
| 91 | }
|
| 92 |
|
| 93 | public static BlockPos min(final BlockPos a, final BlockPos b) {
|
| 94 | return new BlockPos(Math.min(a.getX(), b.getX()), Math.min(a.getY(), b.getY()), Math.min(a.getZ(), b.getZ()));
|
| 95 | }
|
| 96 |
|
| 97 | public static BlockPos max(final BlockPos a, final BlockPos b) {
|
| 98 | return new BlockPos(Math.max(a.getX(), b.getX()), Math.max(a.getY(), b.getY()), Math.max(a.getZ(), b.getZ()));
|
| 99 | }
|
| 100 |
|
| 101 | public long asLong() {
|
| 102 | return asLong(this.getX(), this.getY(), this.getZ());
|
| 103 | }
|
| 104 |
|
| 105 | public static long asLong(final int x, final int y, final int z) {
|
| 106 | long node = 0L;
|
| 107 | node |= (x & PACKED_X_MASK) << X_OFFSET;
|
| 108 | node |= (y & PACKED_Y_MASK) << 0;
|
| 109 | return node | (z & PACKED_Z_MASK) << Z_OFFSET;
|
| 110 | }
|
| 111 |
|
| 112 | public static long getFlatIndex(final long neighborBlockNode) {
|
| 113 | return neighborBlockNode & -16L;
|
| 114 | }
|
| 115 |
|
| 116 | public BlockPos offset(final int x, final int y, final int z) {
|
| 117 | return x == 0 && y == 0 && z == 0 ? this : new BlockPos(this.getX() + x, this.getY() + y, this.getZ() + z);
|
| 118 | }
|
| 119 |
|
| 120 | public BlockPos offset(final Vec3i vec) {
|
| 121 | return this.offset(vec.getX(), vec.getY(), vec.getZ());
|
| 122 | }
|
| 123 |
|
| 124 | public BlockPos subtract(final Vec3i vec) {
|
| 125 | return this.offset(-vec.getX(), -vec.getY(), -vec.getZ());
|
| 126 | }
|
| 127 |
|
| 128 | public BlockPos multiply(final int scale) {
|
| 129 | if (scale == 1) {
|
| 130 | return this;
|
| 131 | } else {
|
| 132 | return scale == 0 ? ZERO : new BlockPos(this.getX() * scale, this.getY() * scale, this.getZ() * scale);
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | public BlockPos above() {
|
| 137 | return this.relative(Direction.UP);
|
| 138 | }
|
| 139 |
|
| 140 | public BlockPos above(final int steps) {
|
| 141 | return this.relative(Direction.UP, steps);
|
| 142 | }
|
| 143 |
|
| 144 | public BlockPos below() {
|
| 145 | return this.relative(Direction.DOWN);
|
| 146 | }
|
| 147 |
|
| 148 | public BlockPos below(final int steps) {
|
| 149 | return this.relative(Direction.DOWN, steps);
|
| 150 | }
|
| 151 |
|
| 152 | public BlockPos north() {
|
| 153 | return this.relative(Direction.NORTH);
|
| 154 | }
|
| 155 |
|
| 156 | public BlockPos north(final int steps) {
|
| 157 | return this.relative(Direction.NORTH, steps);
|
| 158 | }
|
| 159 |
|
| 160 | public BlockPos south() {
|
| 161 | return this.relative(Direction.SOUTH);
|
| 162 | }
|
| 163 |
|
| 164 | public BlockPos south(final int steps) {
|
| 165 | return this.relative(Direction.SOUTH, steps);
|
| 166 | }
|
| 167 |
|
| 168 | public BlockPos west() {
|
| 169 | return this.relative(Direction.WEST);
|
| 170 | }
|
| 171 |
|
| 172 | public BlockPos west(final int steps) {
|
| 173 | return this.relative(Direction.WEST, steps);
|
| 174 | }
|
| 175 |
|
| 176 | public BlockPos east() {
|
| 177 | return this.relative(Direction.EAST);
|
| 178 | }
|
| 179 |
|
| 180 | public BlockPos east(final int steps) {
|
| 181 | return this.relative(Direction.EAST, steps);
|
| 182 | }
|
| 183 |
|
| 184 | public BlockPos relative(final Direction direction) {
|
| 185 | return new BlockPos(this.getX() + direction.getStepX(), this.getY() + direction.getStepY(), this.getZ() + direction.getStepZ());
|
| 186 | }
|
| 187 |
|
| 188 | public BlockPos relative(final Direction direction, final int steps) {
|
| 189 | return steps == 0
|
| 190 | ? this
|
| 191 | : new BlockPos(this.getX() + direction.getStepX() * steps, this.getY() + direction.getStepY() * steps, this.getZ() + direction.getStepZ() * steps);
|
| 192 | }
|
| 193 |
|
| 194 | public BlockPos relative(final Direction.Axis axis, final int steps) {
|
| 195 | if (steps == 0) {
|
| 196 | return this;
|
| 197 | }
|
| 198 |
|
| 199 | int xStep = axis == Direction.Axis.X ? steps : 0;
|
| 200 | int yStep = axis == Direction.Axis.Y ? steps : 0;
|
| 201 | int zStep = axis == Direction.Axis.Z ? steps : 0;
|
| 202 | return new BlockPos(this.getX() + xStep, this.getY() + yStep, this.getZ() + zStep);
|
| 203 | }
|
| 204 |
|
| 205 | public BlockPos rotate(final Rotation rotation) {
|
| 206 | return switch (rotation) {
|
| 207 | case CLOCKWISE_90 -> new BlockPos(-this.getZ(), this.getY(), this.getX());
|
| 208 | case CLOCKWISE_180 -> new BlockPos(-this.getX(), this.getY(), -this.getZ());
|
| 209 | case COUNTERCLOCKWISE_90 -> new BlockPos(this.getZ(), this.getY(), -this.getX());
|
| 210 | case NONE -> this;
|
| 211 | };
|
| 212 | }
|
| 213 |
|
| 214 | public BlockPos cross(final Vec3i upVector) {
|
| 215 | return new BlockPos(
|
| 216 | this.getY() * upVector.getZ() - this.getZ() * upVector.getY(),
|
| 217 | this.getZ() * upVector.getX() - this.getX() * upVector.getZ(),
|
| 218 | this.getX() * upVector.getY() - this.getY() * upVector.getX()
|
| 219 | );
|
| 220 | }
|
| 221 |
|
| 222 | public BlockPos atY(final int y) {
|
| 223 | return new BlockPos(this.getX(), y, this.getZ());
|
| 224 | }
|
| 225 |
|
| 226 | public BlockPos immutable() {
|
| 227 | return this;
|
| 228 | }
|
| 229 |
|
| 230 | public BlockPos.MutableBlockPos mutable() {
|
| 231 | return new BlockPos.MutableBlockPos(this.getX(), this.getY(), this.getZ());
|
| 232 | }
|
| 233 |
|
| 234 | public Vec3 clampLocationWithin(final Vec3 location) {
|
| 235 | return new Vec3(
|
| 236 | Mth.clamp(location.x, this.getX() + 1.0E-5F, this.getX() + 1.0 - 1.0E-5F),
|
| 237 | Mth.clamp(location.y, this.getY() + 1.0E-5F, this.getY() + 1.0 - 1.0E-5F),
|
| 238 | Mth.clamp(location.z, this.getZ() + 1.0E-5F, this.getZ() + 1.0 - 1.0E-5F)
|
| 239 | );
|
| 240 | }
|
| 241 |
|
| 242 | public static Iterable<BlockPos> randomInCube(final RandomSource random, final int limit, final BlockPos center, final int sizeToScanInAllDirections) {
|
| 243 | return randomBetweenClosed(
|
| 244 | random,
|
| 245 | limit,
|
| 246 | center.getX() - sizeToScanInAllDirections,
|
| 247 | center.getY() - sizeToScanInAllDirections,
|
| 248 | center.getZ() - sizeToScanInAllDirections,
|
| 249 | center.getX() + sizeToScanInAllDirections,
|
| 250 | center.getY() + sizeToScanInAllDirections,
|
| 251 | center.getZ() + sizeToScanInAllDirections
|
| 252 | );
|
| 253 | }
|
| 254 |
|
| 255 | public static Iterable<BlockPos> randomBetweenClosed(
|
| 256 | final RandomSource random, final int limit, final int minX, final int minY, final int minZ, final int maxX, final int maxY, final int maxZ
|
| 257 | ) {
|
| 258 | int width = maxX - minX + 1;
|
| 259 | int height = maxY - minY + 1;
|
| 260 | int depth = maxZ - minZ + 1;
|
| 261 | return () -> new AbstractIterator<BlockPos>() {
|
| 262 | private final BlockPos.MutableBlockPos nextPos = new BlockPos.MutableBlockPos();
|
| 263 | private int counter = limit;
|
| 264 |
|
| 265 | protected BlockPos computeNext() {
|
| 266 | if (this.counter <= 0) {
|
| 267 | return this.endOfData();
|
| 268 | }
|
| 269 |
|
| 270 | BlockPos next = this.nextPos.set(minX + random.nextInt(width), minY + random.nextInt(height), minZ + random.nextInt(depth));
|
| 271 | this.counter--;
|
| 272 | return next;
|
| 273 | }
|
| 274 | };
|
| 275 | }
|
| 276 |
|
| 277 | public static Iterable<BlockPos> withinManhattan(final BlockPos origin, final int reach) {
|
| 278 | return manhattanOrdered(origin, reach, reach, reach, reach);
|
| 279 | }
|
| 280 |
|
| 281 | public static Iterable<BlockPos> withinClippedManhattan(final BlockPos origin, final int reachX, final int reachY, final int reachZ) {
|
| 282 | return manhattanOrdered(origin, reachX, reachY, reachZ, Math.max(Math.max(reachX, reachY), reachZ));
|
| 283 | }
|
| 284 |
|
| 285 | public static Iterable<BlockPos> withinBoxByManhattanDistance(final BlockPos origin, final int reachX, final int reachY, final int reachZ) {
|
| 286 | return manhattanOrdered(origin, reachX, reachY, reachZ, reachX + reachY + reachZ);
|
| 287 | }
|
| 288 |
|
| 289 | private static Iterable<BlockPos> manhattanOrdered(final BlockPos origin, final int reachX, final int reachY, final int reachZ, final int maxDepth) {
|
| 290 | int originX = origin.getX();
|
| 291 | int originY = origin.getY();
|
| 292 | int originZ = origin.getZ();
|
| 293 | return () -> new AbstractIterator<BlockPos>() {
|
| 294 | private final BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos();
|
| 295 | private int currentDepth;
|
| 296 | private int maxX;
|
| 297 | private int maxY;
|
| 298 | private int x;
|
| 299 | private int y;
|
| 300 | private boolean zMirror;
|
| 301 |
|
| 302 | protected @Nullable BlockPos computeNext() {
|
| 303 | if (this.zMirror) {
|
| 304 | this.zMirror = false;
|
| 305 | this.cursor.setZ(originZ - (this.cursor.getZ() - originZ));
|
| 306 | return this.cursor;
|
| 307 | }
|
| 308 |
|
| 309 | BlockPos found;
|
| 310 | for (found = null; found == null; this.y++) {
|
| 311 | if (this.y > this.maxY) {
|
| 312 | this.x++;
|
| 313 | if (this.x > this.maxX) {
|
| 314 | this.currentDepth++;
|
| 315 | if (this.currentDepth > maxDepth) {
|
| 316 | return this.endOfData();
|
| 317 | }
|
| 318 |
|
| 319 | this.maxX = Math.min(reachX, this.currentDepth);
|
| 320 | this.x = -this.maxX;
|
| 321 | }
|
| 322 |
|
| 323 | this.maxY = Math.min(reachY, this.currentDepth - Math.abs(this.x));
|
| 324 | this.y = -this.maxY;
|
| 325 | }
|
| 326 |
|
| 327 | int xx = this.x;
|
| 328 | int yy = this.y;
|
| 329 | int zz = this.currentDepth - Math.abs(xx) - Math.abs(yy);
|
| 330 | if (zz <= reachZ) {
|
| 331 | this.zMirror = zz != 0;
|
| 332 | found = this.cursor.set(originX + xx, originY + yy, originZ + zz);
|
| 333 | }
|
| 334 | }
|
| 335 |
|
| 336 | return found;
|
| 337 | }
|
| 338 | };
|
| 339 | }
|
| 340 |
|
| 341 | public static Iterable<BlockPos> betweenClosed(final AABB box) {
|
| 342 | BlockPos startPos = containing(box.minX, box.minY, box.minZ);
|
| 343 | BlockPos endPos = containing(box.maxX, box.maxY, box.maxZ);
|
| 344 | return betweenClosed(startPos, endPos);
|
| 345 | }
|
| 346 |
|
| 347 | public static Iterable<BlockPos> betweenClosed(final BlockPos a, final BlockPos b) {
|
| 348 | return betweenClosed(
|
| 349 | Math.min(a.getX(), b.getX()),
|
| 350 | Math.min(a.getY(), b.getY()),
|
| 351 | Math.min(a.getZ(), b.getZ()),
|
| 352 | Math.max(a.getX(), b.getX()),
|
| 353 | Math.max(a.getY(), b.getY()),
|
| 354 | Math.max(a.getZ(), b.getZ())
|
| 355 | );
|
| 356 | }
|
| 357 |
|
| 358 | public static Stream<BlockPos> betweenClosedStream(final BlockPos a, final BlockPos b) {
|
| 359 | return StreamSupport.stream(betweenClosed(a, b).spliterator(), false);
|
| 360 | }
|
| 361 |
|
| 362 | public static Stream<BlockPos> betweenClosedStream(final BoundingBox boundingBox) {
|
| 363 | return betweenClosedStream(
|
| 364 | Math.min(boundingBox.minX(), boundingBox.maxX()),
|
| 365 | Math.min(boundingBox.minY(), boundingBox.maxY()),
|
| 366 | Math.min(boundingBox.minZ(), boundingBox.maxZ()),
|
| 367 | Math.max(boundingBox.minX(), boundingBox.maxX()),
|
| 368 | Math.max(boundingBox.minY(), boundingBox.maxY()),
|
| 369 | Math.max(boundingBox.minZ(), boundingBox.maxZ())
|
| 370 | );
|
| 371 | }
|
| 372 |
|
| 373 | public static Stream<BlockPos> betweenClosedStream(final AABB box) {
|
| 374 | return betweenClosedStream(Mth.floor(box.minX), Mth.floor(box.minY), Mth.floor(box.minZ), Mth.floor(box.maxX), Mth.floor(box.maxY), Mth.floor(box.maxZ));
|
| 375 | }
|
| 376 |
|
| 377 | public static Stream<BlockPos> betweenClosedStream(final int minX, final int minY, final int minZ, final int maxX, final int maxY, final int maxZ) {
|
| 378 | return StreamSupport.stream(betweenClosed(minX, minY, minZ, maxX, maxY, maxZ).spliterator(), false);
|
| 379 | }
|
| 380 |
|
| 381 | public static Iterable<BlockPos> betweenClosed(final int minX, final int minY, final int minZ, final int maxX, final int maxY, final int maxZ) {
|
| 382 | int width = maxX - minX + 1;
|
| 383 | int height = maxY - minY + 1;
|
| 384 | int depth = maxZ - minZ + 1;
|
| 385 | int end = width * height * depth;
|
| 386 | return () -> new AbstractIterator<BlockPos>() {
|
| 387 | private final BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos();
|
| 388 | private int index;
|
| 389 |
|
| 390 | protected BlockPos computeNext() {
|
| 391 | if (this.index == end) {
|
| 392 | return this.endOfData();
|
| 393 | }
|
| 394 |
|
| 395 | int x = this.index % width;
|
| 396 | int slice = this.index / width;
|
| 397 | int y = slice % height;
|
| 398 | int z = slice / height;
|
| 399 | this.index++;
|
| 400 | return this.cursor.set(minX + x, minY + y, minZ + z);
|
| 401 | }
|
| 402 | };
|
| 403 | }
|
| 404 |
|
| 405 | public static Iterable<BlockPos> neighborColumn(final int startX, final int startY, final int startZ, final int endY) {
|
| 406 | int yDirection = endY > startY ? 1 : -1;
|
| 407 | int height = Math.abs(endY - startY) + 1;
|
| 408 | Vec3i[] steps = new Vec3i[]{
|
| 409 | new Vec3i(0, 0, 0), Direction.NORTH.getUnitVec3i(), Direction.EAST.getUnitVec3i(), Direction.SOUTH.getUnitVec3i(), Direction.WEST.getUnitVec3i()
|
| 410 | };
|
| 411 | int stepCount = steps.length * height;
|
| 412 | return () -> new AbstractIterator<BlockPos>() {
|
| 413 | private final BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos();
|
| 414 | private int index;
|
| 415 |
|
| 416 | protected BlockPos computeNext() {
|
| 417 | if (this.index == stepCount) {
|
| 418 | return this.endOfData();
|
| 419 | }
|
| 420 |
|
| 421 | int y = this.index % height;
|
| 422 | int stepIndex = this.index / height;
|
| 423 | Vec3i step = steps[stepIndex];
|
| 424 | this.index++;
|
| 425 | return this.cursor.set(startX + step.getX(), startY + y * yDirection, startZ + step.getZ());
|
| 426 | }
|
| 427 | };
|
| 428 | }
|
| 429 |
|
| 430 | public static Iterable<BlockPos.MutableBlockPos> spiralAround(
|
| 431 | final BlockPos center, final int radius, final Direction firstDirection, final Direction secondDirection
|
| 432 | ) {
|
| 433 | Validate.validState(firstDirection.getAxis() != secondDirection.getAxis(), "The two directions cannot be on the same axis");
|
| 434 | return () -> new AbstractIterator<BlockPos.MutableBlockPos>() {
|
| 435 | private final Direction[] directions = new Direction[]{firstDirection, secondDirection, firstDirection.getOpposite(), secondDirection.getOpposite()};
|
| 436 | private final BlockPos.MutableBlockPos cursor = center.mutable().move(secondDirection);
|
| 437 | private final int legs = 4 * radius;
|
| 438 | private int leg = -1;
|
| 439 | private int legSize;
|
| 440 | private int legIndex;
|
| 441 | private int lastX = this.cursor.getX();
|
| 442 | private int lastY = this.cursor.getY();
|
| 443 | private int lastZ = this.cursor.getZ();
|
| 444 |
|
| 445 | protected BlockPos.MutableBlockPos computeNext() {
|
| 446 | this.cursor.set(this.lastX, this.lastY, this.lastZ).move(this.directions[(this.leg + 4) % 4]);
|
| 447 | this.lastX = this.cursor.getX();
|
| 448 | this.lastY = this.cursor.getY();
|
| 449 | this.lastZ = this.cursor.getZ();
|
| 450 | if (this.legIndex >= this.legSize) {
|
| 451 | if (this.leg >= this.legs) {
|
| 452 | return this.endOfData();
|
| 453 | }
|
| 454 |
|
| 455 | this.leg++;
|
| 456 | this.legIndex = 0;
|
| 457 | this.legSize = this.leg / 2 + 1;
|
| 458 | }
|
| 459 |
|
| 460 | this.legIndex++;
|
| 461 | return this.cursor;
|
| 462 | }
|
| 463 | };
|
| 464 | }
|
| 465 |
|
| 466 | public static int breadthFirstTraversal(
|
| 467 | final BlockPos startPos,
|
| 468 | final int maxDepth,
|
| 469 | final int maxCount,
|
| 470 | final BiConsumer<BlockPos, Consumer<BlockPos>> neighbourProvider,
|
| 471 | final Function<BlockPos, BlockPos.TraversalNodeStatus> nodeProcessor
|
| 472 | ) {
|
| 473 | record Node(BlockPos pos, int depth) {
|
| 474 | }
|
| 475 |
|
| 476 | Queue<Node> nodes = new ArrayDeque<>();
|
| 477 | LongSet visited = new LongOpenHashSet();
|
| 478 | nodes.add(new Node(startPos, 0));
|
| 479 | int count = 0;
|
| 480 |
|
| 481 | while (!nodes.isEmpty()) {
|
| 482 | Node node = nodes.poll();
|
| 483 | BlockPos currentPos = node.pos;
|
| 484 | int depth = node.depth;
|
| 485 | long currentPosLong = currentPos.asLong();
|
| 486 | if (visited.add(currentPosLong)) {
|
| 487 | BlockPos.TraversalNodeStatus next = nodeProcessor.apply(currentPos);
|
| 488 | if (next != BlockPos.TraversalNodeStatus.SKIP) {
|
| 489 | if (next == BlockPos.TraversalNodeStatus.STOP) {
|
| 490 | break;
|
| 491 | }
|
| 492 |
|
| 493 | if (++count >= maxCount) {
|
| 494 | return count;
|
| 495 | }
|
| 496 |
|
| 497 | if (depth < maxDepth) {
|
| 498 | neighbourProvider.accept(currentPos, pos -> nodes.add(new Node(pos, depth + 1)));
|
| 499 | }
|
| 500 | }
|
| 501 | }
|
| 502 | }
|
| 503 |
|
| 504 | return count;
|
| 505 | }
|
| 506 |
|
| 507 | public static Iterable<BlockPos> betweenCornersInDirection(final AABB aabb, final Vec3 direction) {
|
| 508 | Vec3 minCorner = aabb.getMinPosition();
|
| 509 | int firstCornerX = Mth.floor(minCorner.x());
|
| 510 | int firstCornerY = Mth.floor(minCorner.y());
|
| 511 | int firstCornerZ = Mth.floor(minCorner.z());
|
| 512 | Vec3 maxCorner = aabb.getMaxPosition();
|
| 513 | int secondCornerX = Mth.floor(maxCorner.x());
|
| 514 | int secondCornerY = Mth.floor(maxCorner.y());
|
| 515 | int secondCornerZ = Mth.floor(maxCorner.z());
|
| 516 | return betweenCornersInDirection(firstCornerX, firstCornerY, firstCornerZ, secondCornerX, secondCornerY, secondCornerZ, direction);
|
| 517 | }
|
| 518 |
|
| 519 | public static Iterable<BlockPos> betweenCornersInDirection(final BlockPos firstCorner, final BlockPos secondCorner, final Vec3 direction) {
|
| 520 | return betweenCornersInDirection(
|
| 521 | firstCorner.getX(), firstCorner.getY(), firstCorner.getZ(), secondCorner.getX(), secondCorner.getY(), secondCorner.getZ(), direction
|
| 522 | );
|
| 523 | }
|
| 524 |
|
| 525 | public static Iterable<BlockPos> betweenCornersInDirection(
|
| 526 | final int firstCornerX,
|
| 527 | final int firstCornerY,
|
| 528 | final int firstCornerZ,
|
| 529 | final int secondCornerX,
|
| 530 | final int secondCornerY,
|
| 531 | final int secondCornerZ,
|
| 532 | final Vec3 direction
|
| 533 | ) {
|
| 534 | int minCornerX = Math.min(firstCornerX, secondCornerX);
|
| 535 | int minCornerY = Math.min(firstCornerY, secondCornerY);
|
| 536 | int minCornerZ = Math.min(firstCornerZ, secondCornerZ);
|
| 537 | int maxCornerX = Math.max(firstCornerX, secondCornerX);
|
| 538 | int maxCornerY = Math.max(firstCornerY, secondCornerY);
|
| 539 | int maxCornerZ = Math.max(firstCornerZ, secondCornerZ);
|
| 540 | int diffX = maxCornerX - minCornerX;
|
| 541 | int diffY = maxCornerY - minCornerY;
|
| 542 | int diffZ = maxCornerZ - minCornerZ;
|
| 543 | int startCornerX = direction.x >= 0.0 ? minCornerX : maxCornerX;
|
| 544 | int startCornerY = direction.y >= 0.0 ? minCornerY : maxCornerY;
|
| 545 | int startCornerZ = direction.z >= 0.0 ? minCornerZ : maxCornerZ;
|
| 546 | List<Direction.Axis> axes = Direction.axisStepOrder(direction);
|
| 547 | Direction.Axis firstVisitAxis = axes.get(0);
|
| 548 | Direction.Axis secondVisitAxis = axes.get(1);
|
| 549 | Direction.Axis thirdVisitAxis = axes.get(2);
|
| 550 | Direction firstVisitDir = direction.get(firstVisitAxis) >= 0.0 ? firstVisitAxis.getPositive() : firstVisitAxis.getNegative();
|
| 551 | Direction secondVisitDir = direction.get(secondVisitAxis) >= 0.0 ? secondVisitAxis.getPositive() : secondVisitAxis.getNegative();
|
| 552 | Direction thirdVisitDir = direction.get(thirdVisitAxis) >= 0.0 ? thirdVisitAxis.getPositive() : thirdVisitAxis.getNegative();
|
| 553 | int firstMax = firstVisitAxis.choose(diffX, diffY, diffZ);
|
| 554 | int secondMax = secondVisitAxis.choose(diffX, diffY, diffZ);
|
| 555 | int thirdMax = thirdVisitAxis.choose(diffX, diffY, diffZ);
|
| 556 | return () -> new AbstractIterator<BlockPos>() {
|
| 557 | private final BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos();
|
| 558 | private int firstIndex;
|
| 559 | private int secondIndex;
|
| 560 | private int thirdIndex;
|
| 561 | private boolean end;
|
| 562 | private final int firstDirX = firstVisitDir.getStepX();
|
| 563 | private final int firstDirY = firstVisitDir.getStepY();
|
| 564 | private final int firstDirZ = firstVisitDir.getStepZ();
|
| 565 | private final int secondDirX = secondVisitDir.getStepX();
|
| 566 | private final int secondDirY = secondVisitDir.getStepY();
|
| 567 | private final int secondDirZ = secondVisitDir.getStepZ();
|
| 568 | private final int thirdDirX = thirdVisitDir.getStepX();
|
| 569 | private final int thirdDirY = thirdVisitDir.getStepY();
|
| 570 | private final int thirdDirZ = thirdVisitDir.getStepZ();
|
| 571 |
|
| 572 | protected BlockPos computeNext() {
|
| 573 | if (this.end) {
|
| 574 | return this.endOfData();
|
| 575 | }
|
| 576 |
|
| 577 | this.cursor
|
| 578 | .set(
|
| 579 | startCornerX + this.firstDirX * this.firstIndex + this.secondDirX * this.secondIndex + this.thirdDirX * this.thirdIndex,
|
| 580 | startCornerY + this.firstDirY * this.firstIndex + this.secondDirY * this.secondIndex + this.thirdDirY * this.thirdIndex,
|
| 581 | startCornerZ + this.firstDirZ * this.firstIndex + this.secondDirZ * this.secondIndex + this.thirdDirZ * this.thirdIndex
|
| 582 | );
|
| 583 | if (this.thirdIndex < thirdMax) {
|
| 584 | this.thirdIndex++;
|
| 585 | } else if (this.secondIndex < secondMax) {
|
| 586 | this.secondIndex++;
|
| 587 | this.thirdIndex = 0;
|
| 588 | } else if (this.firstIndex < firstMax) {
|
| 589 | this.firstIndex++;
|
| 590 | this.thirdIndex = 0;
|
| 591 | this.secondIndex = 0;
|
| 592 | } else {
|
| 593 | this.end = true;
|
| 594 | }
|
| 595 |
|
| 596 | return this.cursor;
|
| 597 | }
|
| 598 | };
|
| 599 | }
|
| 600 |
|
| 601 | public static class MutableBlockPos extends BlockPos {
|
| 602 | public MutableBlockPos() {
|
| 603 | this(0, 0, 0);
|
| 604 | }
|
| 605 |
|
| 606 | public MutableBlockPos(final int x, final int y, final int z) {
|
| 607 | super(x, y, z);
|
| 608 | }
|
| 609 |
|
| 610 | public MutableBlockPos(final double x, final double y, final double z) {
|
| 611 | this(Mth.floor(x), Mth.floor(y), Mth.floor(z));
|
| 612 | }
|
| 613 |
|
| 614 | @Override
|
| 615 | public BlockPos offset(final int x, final int y, final int z) {
|
| 616 | return super.offset(x, y, z).immutable();
|
| 617 | }
|
| 618 |
|
| 619 | @Override
|
| 620 | public BlockPos multiply(final int scale) {
|
| 621 | return super.multiply(scale).immutable();
|
| 622 | }
|
| 623 |
|
| 624 | @Override
|
| 625 | public BlockPos relative(final Direction direction, final int steps) {
|
| 626 | return super.relative(direction, steps).immutable();
|
| 627 | }
|
| 628 |
|
| 629 | @Override
|
| 630 | public BlockPos relative(final Direction.Axis axis, final int steps) {
|
| 631 | return super.relative(axis, steps).immutable();
|
| 632 | }
|
| 633 |
|
| 634 | @Override
|
| 635 | public BlockPos rotate(final Rotation rotation) {
|
| 636 | return super.rotate(rotation).immutable();
|
| 637 | }
|
| 638 |
|
| 639 | public BlockPos.MutableBlockPos set(final int x, final int y, final int z) {
|
| 640 | this.setX(x);
|
| 641 | this.setY(y);
|
| 642 | this.setZ(z);
|
| 643 | return this;
|
| 644 | }
|
| 645 |
|
| 646 | public BlockPos.MutableBlockPos set(final double x, final double y, final double z) {
|
| 647 | return this.set(Mth.floor(x), Mth.floor(y), Mth.floor(z));
|
| 648 | }
|
| 649 |
|
| 650 | public BlockPos.MutableBlockPos set(final Vec3i vec) {
|
| 651 | return this.set(vec.getX(), vec.getY(), vec.getZ());
|
| 652 | }
|
| 653 |
|
| 654 | public BlockPos.MutableBlockPos set(final long pos) {
|
| 655 | return this.set(getX(pos), getY(pos), getZ(pos));
|
| 656 | }
|
| 657 |
|
| 658 | public BlockPos.MutableBlockPos set(final AxisCycle transform, final int x, final int y, final int z) {
|
| 659 | return this.set(transform.cycle(x, y, z, Direction.Axis.X), transform.cycle(x, y, z, Direction.Axis.Y), transform.cycle(x, y, z, Direction.Axis.Z));
|
| 660 | }
|
| 661 |
|
| 662 | public BlockPos.MutableBlockPos setWithOffset(final Vec3i pos, final Direction direction) {
|
| 663 | return this.set(pos.getX() + direction.getStepX(), pos.getY() + direction.getStepY(), pos.getZ() + direction.getStepZ());
|
| 664 | }
|
| 665 |
|
| 666 | public BlockPos.MutableBlockPos setWithOffset(final Vec3i pos, final int x, final int y, final int z) {
|
| 667 | return this.set(pos.getX() + x, pos.getY() + y, pos.getZ() + z);
|
| 668 | }
|
| 669 |
|
| 670 | public BlockPos.MutableBlockPos setWithOffset(final Vec3i pos, final Vec3i offset) {
|
| 671 | return this.set(pos.getX() + offset.getX(), pos.getY() + offset.getY(), pos.getZ() + offset.getZ());
|
| 672 | }
|
| 673 |
|
| 674 | public BlockPos.MutableBlockPos move(final Direction direction) {
|
| 675 | return this.move(direction, 1);
|
| 676 | }
|
| 677 |
|
| 678 | public BlockPos.MutableBlockPos move(final Direction direction, final int steps) {
|
| 679 | return this.set(this.getX() + direction.getStepX() * steps, this.getY() + direction.getStepY() * steps, this.getZ() + direction.getStepZ() * steps);
|
| 680 | }
|
| 681 |
|
| 682 | public BlockPos.MutableBlockPos move(final int x, final int y, final int z) {
|
| 683 | return this.set(this.getX() + x, this.getY() + y, this.getZ() + z);
|
| 684 | }
|
| 685 |
|
| 686 | public BlockPos.MutableBlockPos move(final Vec3i pos) {
|
| 687 | return this.set(this.getX() + pos.getX(), this.getY() + pos.getY(), this.getZ() + pos.getZ());
|
| 688 | }
|
| 689 |
|
| 690 | public BlockPos.MutableBlockPos clamp(final Direction.Axis axis, final int minimum, final int maximum) {
|
| 691 | return switch (axis) {
|
| 692 | case X -> this.set(Mth.clamp(this.getX(), minimum, maximum), this.getY(), this.getZ());
|
| 693 | case Y -> this.set(this.getX(), Mth.clamp(this.getY(), minimum, maximum), this.getZ());
|
| 694 | case Z -> this.set(this.getX(), this.getY(), Mth.clamp(this.getZ(), minimum, maximum));
|
| 695 | };
|
| 696 | }
|
| 697 |
|
| 698 | public BlockPos.MutableBlockPos setX(final int x) {
|
| 699 | super.setX(x);
|
| 700 | return this;
|
| 701 | }
|
| 702 |
|
| 703 | public BlockPos.MutableBlockPos setY(final int y) {
|
| 704 | super.setY(y);
|
| 705 | return this;
|
| 706 | }
|
| 707 |
|
| 708 | public BlockPos.MutableBlockPos setZ(final int z) {
|
| 709 | super.setZ(z);
|
| 710 | return this;
|
| 711 | }
|
| 712 |
|
| 713 | @Override
|
| 714 | public BlockPos immutable() {
|
| 715 | return new BlockPos(this.getX(), this.getY(), this.getZ());
|
| 716 | }
|
| 717 | }
|
| 718 |
|
| 719 | public enum TraversalNodeStatus {
|
| 720 | ACCEPT,
|
| 721 | SKIP,
|
| 722 | STOP;
|
| 723 | }
|
| 724 | }
|
| 725 |
|