| 1 | package net.minecraft.world.level.levelgen.structure;
|
| 2 |
|
| 3 | import com.google.common.base.MoreObjects;
|
| 4 | import com.mojang.logging.LogUtils;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import io.netty.buffer.ByteBuf;
|
| 7 | import java.util.Iterator;
|
| 8 | import java.util.Objects;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import java.util.stream.IntStream;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.minecraft.core.BlockPos;
|
| 14 | import net.minecraft.core.Direction;
|
| 15 | import net.minecraft.core.SectionPos;
|
| 16 | import net.minecraft.core.Vec3i;
|
| 17 | import net.minecraft.network.codec.StreamCodec;
|
| 18 | import net.minecraft.util.Util;
|
| 19 | import net.minecraft.world.level.ChunkPos;
|
| 20 | import org.slf4j.Logger;
|
| 21 |
|
| 22 | public class BoundingBox {
|
| 23 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 24 | public static final Codec<BoundingBox> CODEC = Codec.INT_STREAM
|
| 25 | .<BoundingBox>comapFlatMap(
|
| 26 | input -> Util.fixedSize(input, 6).map(ints -> new BoundingBox(ints[0], ints[1], ints[2], ints[3], ints[4], ints[5])),
|
| 27 | bb -> IntStream.of(bb.minX, bb.minY, bb.minZ, bb.maxX, bb.maxY, bb.maxZ)
|
| 28 | )
|
| 29 | .stable();
|
| 30 | public static final StreamCodec<ByteBuf, BoundingBox> STREAM_CODEC = StreamCodec.composite(
|
| 31 | BlockPos.STREAM_CODEC,
|
| 32 | box -> new BlockPos(box.minX, box.minY, box.minZ),
|
| 33 | BlockPos.STREAM_CODEC,
|
| 34 | box -> new BlockPos(box.maxX, box.maxY, box.maxZ),
|
| 35 | (min, max) -> new BoundingBox(min.getX(), min.getY(), min.getZ(), max.getX(), max.getY(), max.getZ())
|
| 36 | );
|
| 37 | private int minX;
|
| 38 | private int minY;
|
| 39 | private int minZ;
|
| 40 | private int maxX;
|
| 41 | private int maxY;
|
| 42 | private int maxZ;
|
| 43 |
|
| 44 | public BoundingBox(final BlockPos content) {
|
| 45 | this(content.getX(), content.getY(), content.getZ(), content.getX(), content.getY(), content.getZ());
|
| 46 | }
|
| 47 |
|
| 48 | public BoundingBox(final int minX, final int minY, final int minZ, final int maxX, final int maxY, final int maxZ) {
|
| 49 | this.minX = minX;
|
| 50 | this.minY = minY;
|
| 51 | this.minZ = minZ;
|
| 52 | this.maxX = maxX;
|
| 53 | this.maxY = maxY;
|
| 54 | this.maxZ = maxZ;
|
| 55 | if (maxX < minX || maxY < minY || maxZ < minZ) {
|
| 56 | Util.logAndPauseIfInIde("Invalid bounding box data, inverted bounds for: " + this);
|
| 57 | this.minX = Math.min(minX, maxX);
|
| 58 | this.minY = Math.min(minY, maxY);
|
| 59 | this.minZ = Math.min(minZ, maxZ);
|
| 60 | this.maxX = Math.max(minX, maxX);
|
| 61 | this.maxY = Math.max(minY, maxY);
|
| 62 | this.maxZ = Math.max(minZ, maxZ);
|
| 63 | }
|
| 64 | }
|
| 65 |
|
| 66 | public static BoundingBox fromCorners(final Vec3i pos0, final Vec3i pos1) {
|
| 67 | return new BoundingBox(
|
| 68 | Math.min(pos0.getX(), pos1.getX()),
|
| 69 | Math.min(pos0.getY(), pos1.getY()),
|
| 70 | Math.min(pos0.getZ(), pos1.getZ()),
|
| 71 | Math.max(pos0.getX(), pos1.getX()),
|
| 72 | Math.max(pos0.getY(), pos1.getY()),
|
| 73 | Math.max(pos0.getZ(), pos1.getZ())
|
| 74 | );
|
| 75 | }
|
| 76 |
|
| 77 | public static BoundingBox infinite() {
|
| 78 | return new BoundingBox(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
|
| 79 | }
|
| 80 |
|
| 81 | public static BoundingBox orientBox(
|
| 82 | final int footX,
|
| 83 | final int footY,
|
| 84 | final int footZ,
|
| 85 | final int offX,
|
| 86 | final int offY,
|
| 87 | final int offZ,
|
| 88 | final int width,
|
| 89 | final int height,
|
| 90 | final int depth,
|
| 91 | final Direction direction
|
| 92 | ) {
|
| 93 | switch (direction) {
|
| 94 | case SOUTH:
|
| 95 | default:
|
| 96 | return new BoundingBox(footX + offX, footY + offY, footZ + offZ, footX + width - 1 + offX, footY + height - 1 + offY, footZ + depth - 1 + offZ);
|
| 97 | case NORTH:
|
| 98 | return new BoundingBox(footX + offX, footY + offY, footZ - depth + 1 + offZ, footX + width - 1 + offX, footY + height - 1 + offY, footZ + offZ);
|
| 99 | case WEST:
|
| 100 | return new BoundingBox(footX - depth + 1 + offZ, footY + offY, footZ + offX, footX + offZ, footY + height - 1 + offY, footZ + width - 1 + offX);
|
| 101 | case EAST:
|
| 102 | return new BoundingBox(footX + offZ, footY + offY, footZ + offX, footX + depth - 1 + offZ, footY + height - 1 + offY, footZ + width - 1 + offX);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | public Stream<ChunkPos> intersectingChunks() {
|
| 107 | int minChunkX = SectionPos.blockToSectionCoord(this.minX());
|
| 108 | int minChunkZ = SectionPos.blockToSectionCoord(this.minZ());
|
| 109 | int maxChunkX = SectionPos.blockToSectionCoord(this.maxX());
|
| 110 | int maxChunkZ = SectionPos.blockToSectionCoord(this.maxZ());
|
| 111 | return ChunkPos.rangeClosed(new ChunkPos(minChunkX, minChunkZ), new ChunkPos(maxChunkX, maxChunkZ));
|
| 112 | }
|
| 113 |
|
| 114 | public boolean intersects(final BoundingBox other) {
|
| 115 | return this.intersects(other.minX, other.minY, other.minZ, other.maxX, other.maxY, other.maxZ);
|
| 116 | }
|
| 117 |
|
| 118 | public boolean intersects(final int minX, final int minY, final int minZ, final int maxX, final int maxY, final int maxZ) {
|
| 119 | return this.maxX >= minX && this.minX <= maxX && this.maxZ >= minZ && this.minZ <= maxZ && this.maxY >= minY && this.minY <= maxY;
|
| 120 | }
|
| 121 |
|
| 122 | public boolean intersects(final int minX, final int minZ, final int maxX, final int maxZ) {
|
| 123 | return this.maxX >= minX && this.minX <= maxX && this.maxZ >= minZ && this.minZ <= maxZ;
|
| 124 | }
|
| 125 |
|
| 126 | public static Optional<BoundingBox> encapsulatingPositions(final Iterable<BlockPos> iterable) {
|
| 127 | Iterator<BlockPos> iterator = iterable.iterator();
|
| 128 | if (!iterator.hasNext()) {
|
| 129 | return Optional.empty();
|
| 130 | }
|
| 131 |
|
| 132 | BoundingBox result = new BoundingBox(iterator.next());
|
| 133 | iterator.forEachRemaining(result::encapsulate);
|
| 134 | return Optional.of(result);
|
| 135 | }
|
| 136 |
|
| 137 | public static Optional<BoundingBox> encapsulatingBoxes(final Iterable<BoundingBox> iterable) {
|
| 138 | Iterator<BoundingBox> iterator = iterable.iterator();
|
| 139 | if (!iterator.hasNext()) {
|
| 140 | return Optional.empty();
|
| 141 | }
|
| 142 |
|
| 143 | BoundingBox first = iterator.next();
|
| 144 | BoundingBox result = new BoundingBox(first.minX, first.minY, first.minZ, first.maxX, first.maxY, first.maxZ);
|
| 145 | iterator.forEachRemaining(result::encapsulate);
|
| 146 | return Optional.of(result);
|
| 147 | }
|
| 148 |
|
| 149 | @Deprecated
|
| 150 | public BoundingBox encapsulate(final BoundingBox other) {
|
| 151 | this.minX = Math.min(this.minX, other.minX);
|
| 152 | this.minY = Math.min(this.minY, other.minY);
|
| 153 | this.minZ = Math.min(this.minZ, other.minZ);
|
| 154 | this.maxX = Math.max(this.maxX, other.maxX);
|
| 155 | this.maxY = Math.max(this.maxY, other.maxY);
|
| 156 | this.maxZ = Math.max(this.maxZ, other.maxZ);
|
| 157 | return this;
|
| 158 | }
|
| 159 |
|
| 160 | public static BoundingBox encapsulating(final BoundingBox a, final BoundingBox b) {
|
| 161 | return new BoundingBox(
|
| 162 | Math.min(a.minX, b.minX), Math.min(a.minY, b.minY), Math.min(a.minZ, b.minZ), Math.max(a.maxX, b.maxX), Math.max(a.maxY, b.maxY), Math.max(a.maxZ, b.maxZ)
|
| 163 | );
|
| 164 | }
|
| 165 |
|
| 166 | @Deprecated
|
| 167 | public BoundingBox encapsulate(final BlockPos pos) {
|
| 168 | this.minX = Math.min(this.minX, pos.getX());
|
| 169 | this.minY = Math.min(this.minY, pos.getY());
|
| 170 | this.minZ = Math.min(this.minZ, pos.getZ());
|
| 171 | this.maxX = Math.max(this.maxX, pos.getX());
|
| 172 | this.maxY = Math.max(this.maxY, pos.getY());
|
| 173 | this.maxZ = Math.max(this.maxZ, pos.getZ());
|
| 174 | return this;
|
| 175 | }
|
| 176 |
|
| 177 | @Deprecated
|
| 178 | public BoundingBox move(final int dx, final int dy, final int dz) {
|
| 179 | this.minX += dx;
|
| 180 | this.minY += dy;
|
| 181 | this.minZ += dz;
|
| 182 | this.maxX += dx;
|
| 183 | this.maxY += dy;
|
| 184 | this.maxZ += dz;
|
| 185 | return this;
|
| 186 | }
|
| 187 |
|
| 188 | @Deprecated
|
| 189 | public BoundingBox move(final Vec3i amount) {
|
| 190 | return this.move(amount.getX(), amount.getY(), amount.getZ());
|
| 191 | }
|
| 192 |
|
| 193 | public BoundingBox moved(final int dx, final int dy, final int dz) {
|
| 194 | return new BoundingBox(this.minX + dx, this.minY + dy, this.minZ + dz, this.maxX + dx, this.maxY + dy, this.maxZ + dz);
|
| 195 | }
|
| 196 |
|
| 197 | public BoundingBox inflatedBy(final int amountToAddAllDirections) {
|
| 198 | return this.inflatedBy(amountToAddAllDirections, amountToAddAllDirections, amountToAddAllDirections);
|
| 199 | }
|
| 200 |
|
| 201 | public BoundingBox inflatedBy(final int inflateX, final int inflateY, final int inflateZ) {
|
| 202 | return new BoundingBox(
|
| 203 | this.minX() - inflateX, this.minY() - inflateY, this.minZ() - inflateZ, this.maxX() + inflateX, this.maxY() + inflateY, this.maxZ() + inflateZ
|
| 204 | );
|
| 205 | }
|
| 206 |
|
| 207 | public boolean isInside(final Vec3i pos) {
|
| 208 | return this.isInside(pos.getX(), pos.getY(), pos.getZ());
|
| 209 | }
|
| 210 |
|
| 211 | public boolean isInside(final int x, final int y, final int z) {
|
| 212 | return x >= this.minX && x <= this.maxX && z >= this.minZ && z <= this.maxZ && y >= this.minY && y <= this.maxY;
|
| 213 | }
|
| 214 |
|
| 215 | public Vec3i getLength() {
|
| 216 | return new Vec3i(this.maxX - this.minX, this.maxY - this.minY, this.maxZ - this.minZ);
|
| 217 | }
|
| 218 |
|
| 219 | public int getXSpan() {
|
| 220 | return this.maxX - this.minX + 1;
|
| 221 | }
|
| 222 |
|
| 223 | public int getYSpan() {
|
| 224 | return this.maxY - this.minY + 1;
|
| 225 | }
|
| 226 |
|
| 227 | public int getZSpan() {
|
| 228 | return this.maxZ - this.minZ + 1;
|
| 229 | }
|
| 230 |
|
| 231 | public BlockPos getCenter() {
|
| 232 | return new BlockPos(this.minX + (this.maxX - this.minX + 1) / 2, this.minY + (this.maxY - this.minY + 1) / 2, this.minZ + (this.maxZ - this.minZ + 1) / 2);
|
| 233 | }
|
| 234 |
|
| 235 | public void forAllCorners(final Consumer<BlockPos> consumer) {
|
| 236 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 237 | consumer.accept(pos.set(this.maxX, this.maxY, this.maxZ));
|
| 238 | consumer.accept(pos.set(this.minX, this.maxY, this.maxZ));
|
| 239 | consumer.accept(pos.set(this.maxX, this.minY, this.maxZ));
|
| 240 | consumer.accept(pos.set(this.minX, this.minY, this.maxZ));
|
| 241 | consumer.accept(pos.set(this.maxX, this.maxY, this.minZ));
|
| 242 | consumer.accept(pos.set(this.minX, this.maxY, this.minZ));
|
| 243 | consumer.accept(pos.set(this.maxX, this.minY, this.minZ));
|
| 244 | consumer.accept(pos.set(this.minX, this.minY, this.minZ));
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public String toString() {
|
| 249 | return MoreObjects.toStringHelper(this)
|
| 250 | .add("minX", this.minX)
|
| 251 | .add("minY", this.minY)
|
| 252 | .add("minZ", this.minZ)
|
| 253 | .add("maxX", this.maxX)
|
| 254 | .add("maxY", this.maxY)
|
| 255 | .add("maxZ", this.maxZ)
|
| 256 | .toString();
|
| 257 | }
|
| 258 |
|
| 259 | @Override
|
| 260 | public boolean equals(final Object o) {
|
| 261 | if (this == o) {
|
| 262 | return true;
|
| 263 | } else {
|
| 264 | return !(o instanceof BoundingBox that)
|
| 265 | ? false
|
| 266 | : this.minX == that.minX && this.minY == that.minY && this.minZ == that.minZ && this.maxX == that.maxX && this.maxY == that.maxY && this.maxZ == that.maxZ;
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | @Override
|
| 271 | public int hashCode() {
|
| 272 | return Objects.hash(this.minX, this.minY, this.minZ, this.maxX, this.maxY, this.maxZ);
|
| 273 | }
|
| 274 |
|
| 275 | public int minX() {
|
| 276 | return this.minX;
|
| 277 | }
|
| 278 |
|
| 279 | public int minY() {
|
| 280 | return this.minY;
|
| 281 | }
|
| 282 |
|
| 283 | public int minZ() {
|
| 284 | return this.minZ;
|
| 285 | }
|
| 286 |
|
| 287 | public int maxX() {
|
| 288 | return this.maxX;
|
| 289 | }
|
| 290 |
|
| 291 | public int maxY() {
|
| 292 | return this.maxY;
|
| 293 | }
|
| 294 |
|
| 295 | public int maxZ() {
|
| 296 | return this.maxZ;
|
| 297 | }
|
| 298 | }
|
| 299 |
|