| 1 | package net.minecraft.world.level;
|
| 2 |
|
| 3 | import com.google.common.base.Suppliers;
|
| 4 | import java.util.List;
|
| 5 | import java.util.function.Supplier;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Holder;
|
| 8 | import net.minecraft.core.SectionPos;
|
| 9 | import net.minecraft.core.registries.Registries;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.level.biome.Biome;
|
| 12 | import net.minecraft.world.level.biome.Biomes;
|
| 13 | import net.minecraft.world.level.block.Blocks;
|
| 14 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 15 | import net.minecraft.world.level.block.state.BlockState;
|
| 16 | import net.minecraft.world.level.border.WorldBorder;
|
| 17 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 18 | import net.minecraft.world.level.chunk.ChunkSource;
|
| 19 | import net.minecraft.world.level.chunk.EmptyLevelChunk;
|
| 20 | import net.minecraft.world.level.material.FluidState;
|
| 21 | import net.minecraft.world.level.material.Fluids;
|
| 22 | import net.minecraft.world.phys.AABB;
|
| 23 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public class PathNavigationRegion implements CollisionGetter {
|
| 27 | protected final int centerX;
|
| 28 | protected final int centerZ;
|
| 29 | protected final ChunkAccess[][] chunks;
|
| 30 | protected boolean allEmpty;
|
| 31 | protected final Level level;
|
| 32 | private final Supplier<Holder<Biome>> plains;
|
| 33 |
|
| 34 | public PathNavigationRegion(final Level level, final BlockPos start, final BlockPos end) {
|
| 35 | this.level = level;
|
| 36 | this.plains = Suppliers.memoize(() -> level.registryAccess().lookupOrThrow(Registries.BIOME).getOrThrow(Biomes.PLAINS));
|
| 37 | this.centerX = SectionPos.blockToSectionCoord(start.getX());
|
| 38 | this.centerZ = SectionPos.blockToSectionCoord(start.getZ());
|
| 39 | int xc2 = SectionPos.blockToSectionCoord(end.getX());
|
| 40 | int zc2 = SectionPos.blockToSectionCoord(end.getZ());
|
| 41 | this.chunks = new ChunkAccess[xc2 - this.centerX + 1][zc2 - this.centerZ + 1];
|
| 42 | ChunkSource chunkSource = level.getChunkSource();
|
| 43 | this.allEmpty = true;
|
| 44 |
|
| 45 | for (int xc = this.centerX; xc <= xc2; xc++) {
|
| 46 | for (int zc = this.centerZ; zc <= zc2; zc++) {
|
| 47 | this.chunks[xc - this.centerX][zc - this.centerZ] = chunkSource.getChunkNow(xc, zc);
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | for (int xc = SectionPos.blockToSectionCoord(start.getX()); xc <= SectionPos.blockToSectionCoord(end.getX()); xc++) {
|
| 52 | for (int zc = SectionPos.blockToSectionCoord(start.getZ()); zc <= SectionPos.blockToSectionCoord(end.getZ()); zc++) {
|
| 53 | ChunkAccess chunk = this.chunks[xc - this.centerX][zc - this.centerZ];
|
| 54 | if (chunk != null && !chunk.isYSpaceEmpty(start.getY(), end.getY())) {
|
| 55 | this.allEmpty = false;
|
| 56 | return;
|
| 57 | }
|
| 58 | }
|
| 59 | }
|
| 60 | }
|
| 61 |
|
| 62 | private ChunkAccess getChunk(final BlockPos pos) {
|
| 63 | return this.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ()));
|
| 64 | }
|
| 65 |
|
| 66 | private ChunkAccess getChunk(final int chunkX, final int chunkZ) {
|
| 67 | int xc = chunkX - this.centerX;
|
| 68 | int zc = chunkZ - this.centerZ;
|
| 69 | if (xc >= 0 && xc < this.chunks.length && zc >= 0 && zc < this.chunks[xc].length) {
|
| 70 | ChunkAccess chunk = this.chunks[xc][zc];
|
| 71 | return chunk != null ? chunk : new EmptyLevelChunk(this.level, new ChunkPos(chunkX, chunkZ), this.plains.get());
|
| 72 | } else {
|
| 73 | return new EmptyLevelChunk(this.level, new ChunkPos(chunkX, chunkZ), this.plains.get());
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public WorldBorder getWorldBorder() {
|
| 79 | return this.level.getWorldBorder();
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public BlockGetter getChunkForCollisions(final int chunkX, final int chunkZ) {
|
| 84 | return this.getChunk(chunkX, chunkZ);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public List<VoxelShape> getEntityCollisions(final @Nullable Entity source, final AABB testArea) {
|
| 89 | return List.of();
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public @Nullable BlockEntity getBlockEntity(final BlockPos pos) {
|
| 94 | ChunkAccess chunk = this.getChunk(pos);
|
| 95 | return chunk.getBlockEntity(pos);
|
| 96 | }
|
| 97 |
|
| 98 | @Override
|
| 99 | public BlockState getBlockState(final BlockPos pos) {
|
| 100 | if (this.isOutsideBuildHeight(pos)) {
|
| 101 | return Blocks.AIR.defaultBlockState();
|
| 102 | }
|
| 103 |
|
| 104 | ChunkAccess chunk = this.getChunk(pos);
|
| 105 | return chunk.getBlockState(pos);
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public FluidState getFluidState(final BlockPos pos) {
|
| 110 | if (this.isOutsideBuildHeight(pos)) {
|
| 111 | return Fluids.EMPTY.defaultFluidState();
|
| 112 | }
|
| 113 |
|
| 114 | ChunkAccess chunk = this.getChunk(pos);
|
| 115 | return chunk.getFluidState(pos);
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public int getMinY() {
|
| 120 | return this.level.getMinY();
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public int getHeight() {
|
| 125 | return this.level.getHeight();
|
| 126 | }
|
| 127 | }
|
| 128 |
|