| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import it.unimi.dsi.fastutil.longs.LongConsumer;
|
| 5 | import java.util.Spliterators.AbstractSpliterator;
|
| 6 | import java.util.function.Consumer;
|
| 7 | import java.util.stream.Stream;
|
| 8 | import java.util.stream.StreamSupport;
|
| 9 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.level.ChunkPos;
|
| 13 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 14 | import net.minecraft.world.level.entity.EntityAccess;
|
| 15 |
|
| 16 | public class SectionPos extends Vec3i {
|
| 17 | public static final int SECTION_BITS = 4;
|
| 18 | public static final int SECTION_SIZE = 16;
|
| 19 | public static final int SECTION_BLOCK_COUNT = 4096;
|
| 20 | public static final int SECTION_MASK = 15;
|
| 21 | public static final int SECTION_HALF_SIZE = 8;
|
| 22 | public static final int SECTION_MAX_INDEX = 15;
|
| 23 | private static final int PACKED_X_LENGTH = 22;
|
| 24 | private static final int PACKED_Y_LENGTH = 20;
|
| 25 | private static final int PACKED_Z_LENGTH = 22;
|
| 26 | private static final long PACKED_X_MASK = 4194303L;
|
| 27 | private static final long PACKED_Y_MASK = 1048575L;
|
| 28 | private static final long PACKED_Z_MASK = 4194303L;
|
| 29 | private static final int Y_OFFSET = 0;
|
| 30 | private static final int Z_OFFSET = 20;
|
| 31 | private static final int X_OFFSET = 42;
|
| 32 | private static final int RELATIVE_X_SHIFT = 8;
|
| 33 | private static final int RELATIVE_Y_SHIFT = 0;
|
| 34 | private static final int RELATIVE_Z_SHIFT = 4;
|
| 35 | public static final StreamCodec<ByteBuf, SectionPos> STREAM_CODEC = ByteBufCodecs.LONG.map(SectionPos::of, SectionPos::asLong);
|
| 36 |
|
| 37 | private SectionPos(final int x, final int y, final int z) {
|
| 38 | super(x, y, z);
|
| 39 | }
|
| 40 |
|
| 41 | public static SectionPos of(final int x, final int y, final int z) {
|
| 42 | return new SectionPos(x, y, z);
|
| 43 | }
|
| 44 |
|
| 45 | public static SectionPos of(final BlockPos pos) {
|
| 46 | return new SectionPos(blockToSectionCoord(pos.getX()), blockToSectionCoord(pos.getY()), blockToSectionCoord(pos.getZ()));
|
| 47 | }
|
| 48 |
|
| 49 | public static SectionPos of(final ChunkPos pos, final int sectionY) {
|
| 50 | return new SectionPos(pos.x(), sectionY, pos.z());
|
| 51 | }
|
| 52 |
|
| 53 | public static SectionPos of(final EntityAccess entity) {
|
| 54 | return of(entity.blockPosition());
|
| 55 | }
|
| 56 |
|
| 57 | public static SectionPos of(final Position pos) {
|
| 58 | return new SectionPos(blockToSectionCoord(pos.x()), blockToSectionCoord(pos.y()), blockToSectionCoord(pos.z()));
|
| 59 | }
|
| 60 |
|
| 61 | public static SectionPos of(final long sectionNode) {
|
| 62 | return new SectionPos(x(sectionNode), y(sectionNode), z(sectionNode));
|
| 63 | }
|
| 64 |
|
| 65 | public static SectionPos bottomOf(final ChunkAccess chunk) {
|
| 66 | return of(chunk.getPos(), chunk.getMinSectionY());
|
| 67 | }
|
| 68 |
|
| 69 | public static long offset(final long sectionNode, final Direction offset) {
|
| 70 | return offset(sectionNode, offset.getStepX(), offset.getStepY(), offset.getStepZ());
|
| 71 | }
|
| 72 |
|
| 73 | public static long offset(final long sectionNode, final int stepX, final int stepY, final int stepZ) {
|
| 74 | return asLong(x(sectionNode) + stepX, y(sectionNode) + stepY, z(sectionNode) + stepZ);
|
| 75 | }
|
| 76 |
|
| 77 | public static int posToSectionCoord(final double pos) {
|
| 78 | return blockToSectionCoord(Mth.floor(pos));
|
| 79 | }
|
| 80 |
|
| 81 | public static int blockToSectionCoord(final int blockCoord) {
|
| 82 | return blockCoord >> 4;
|
| 83 | }
|
| 84 |
|
| 85 | public static int blockToSectionCoord(final double coord) {
|
| 86 | return Mth.floor(coord) >> 4;
|
| 87 | }
|
| 88 |
|
| 89 | public static int sectionRelative(final int blockCoord) {
|
| 90 | return blockCoord & 15;
|
| 91 | }
|
| 92 |
|
| 93 | public static short sectionRelativePos(final BlockPos pos) {
|
| 94 | int x = sectionRelative(pos.getX());
|
| 95 | int y = sectionRelative(pos.getY());
|
| 96 | int z = sectionRelative(pos.getZ());
|
| 97 | return (short)(x << 8 | z << 4 | y << 0);
|
| 98 | }
|
| 99 |
|
| 100 | public static int sectionRelativeX(final short relative) {
|
| 101 | return relative >>> 8 & 15;
|
| 102 | }
|
| 103 |
|
| 104 | public static int sectionRelativeY(final short relative) {
|
| 105 | return relative >>> 0 & 15;
|
| 106 | }
|
| 107 |
|
| 108 | public static int sectionRelativeZ(final short relative) {
|
| 109 | return relative >>> 4 & 15;
|
| 110 | }
|
| 111 |
|
| 112 | public int relativeToBlockX(final short relative) {
|
| 113 | return this.minBlockX() + sectionRelativeX(relative);
|
| 114 | }
|
| 115 |
|
| 116 | public int relativeToBlockY(final short relative) {
|
| 117 | return this.minBlockY() + sectionRelativeY(relative);
|
| 118 | }
|
| 119 |
|
| 120 | public int relativeToBlockZ(final short relative) {
|
| 121 | return this.minBlockZ() + sectionRelativeZ(relative);
|
| 122 | }
|
| 123 |
|
| 124 | public BlockPos relativeToBlockPos(final short relative) {
|
| 125 | return new BlockPos(this.relativeToBlockX(relative), this.relativeToBlockY(relative), this.relativeToBlockZ(relative));
|
| 126 | }
|
| 127 |
|
| 128 | public static int sectionToBlockCoord(final int sectionCoord) {
|
| 129 | return sectionCoord << 4;
|
| 130 | }
|
| 131 |
|
| 132 | public static int sectionToBlockCoord(final int sectionCoord, final int offset) {
|
| 133 | return sectionToBlockCoord(sectionCoord) + offset;
|
| 134 | }
|
| 135 |
|
| 136 | public static int x(final long sectionNode) {
|
| 137 | return (int)(sectionNode << 0 >> 42);
|
| 138 | }
|
| 139 |
|
| 140 | public static int y(final long sectionNode) {
|
| 141 | return (int)(sectionNode << 44 >> 44);
|
| 142 | }
|
| 143 |
|
| 144 | public static int z(final long sectionNode) {
|
| 145 | return (int)(sectionNode << 22 >> 42);
|
| 146 | }
|
| 147 |
|
| 148 | public int x() {
|
| 149 | return this.getX();
|
| 150 | }
|
| 151 |
|
| 152 | public int y() {
|
| 153 | return this.getY();
|
| 154 | }
|
| 155 |
|
| 156 | public int z() {
|
| 157 | return this.getZ();
|
| 158 | }
|
| 159 |
|
| 160 | public int minBlockX() {
|
| 161 | return sectionToBlockCoord(this.x());
|
| 162 | }
|
| 163 |
|
| 164 | public int minBlockY() {
|
| 165 | return sectionToBlockCoord(this.y());
|
| 166 | }
|
| 167 |
|
| 168 | public int minBlockZ() {
|
| 169 | return sectionToBlockCoord(this.z());
|
| 170 | }
|
| 171 |
|
| 172 | public int maxBlockX() {
|
| 173 | return sectionToBlockCoord(this.x(), 15);
|
| 174 | }
|
| 175 |
|
| 176 | public int maxBlockY() {
|
| 177 | return sectionToBlockCoord(this.y(), 15);
|
| 178 | }
|
| 179 |
|
| 180 | public int maxBlockZ() {
|
| 181 | return sectionToBlockCoord(this.z(), 15);
|
| 182 | }
|
| 183 |
|
| 184 | public static long blockToSection(final long blockNode) {
|
| 185 | return asLong(blockToSectionCoord(BlockPos.getX(blockNode)), blockToSectionCoord(BlockPos.getY(blockNode)), blockToSectionCoord(BlockPos.getZ(blockNode)));
|
| 186 | }
|
| 187 |
|
| 188 | public static long getZeroNode(final int x, final int z) {
|
| 189 | return getZeroNode(asLong(x, 0, z));
|
| 190 | }
|
| 191 |
|
| 192 | public static long getZeroNode(final long sectionNode) {
|
| 193 | return sectionNode & -1048576L;
|
| 194 | }
|
| 195 |
|
| 196 | public static long sectionToChunk(final long sectionNode) {
|
| 197 | return ChunkPos.pack(x(sectionNode), z(sectionNode));
|
| 198 | }
|
| 199 |
|
| 200 | public BlockPos origin() {
|
| 201 | return new BlockPos(sectionToBlockCoord(this.x()), sectionToBlockCoord(this.y()), sectionToBlockCoord(this.z()));
|
| 202 | }
|
| 203 |
|
| 204 | public BlockPos center() {
|
| 205 | int delta = 8;
|
| 206 | return this.origin().offset(8, 8, 8);
|
| 207 | }
|
| 208 |
|
| 209 | public ChunkPos chunk() {
|
| 210 | return new ChunkPos(this.x(), this.z());
|
| 211 | }
|
| 212 |
|
| 213 | public static long asLong(final BlockPos pos) {
|
| 214 | return asLong(blockToSectionCoord(pos.getX()), blockToSectionCoord(pos.getY()), blockToSectionCoord(pos.getZ()));
|
| 215 | }
|
| 216 |
|
| 217 | public static long asLong(final int x, final int y, final int z) {
|
| 218 | long node = 0L;
|
| 219 | node |= (x & 4194303L) << 42;
|
| 220 | node |= (y & 1048575L) << 0;
|
| 221 | return node | (z & 4194303L) << 20;
|
| 222 | }
|
| 223 |
|
| 224 | public long asLong() {
|
| 225 | return asLong(this.x(), this.y(), this.z());
|
| 226 | }
|
| 227 |
|
| 228 | public SectionPos offset(final int x, final int y, final int z) {
|
| 229 | return x == 0 && y == 0 && z == 0 ? this : new SectionPos(this.x() + x, this.y() + y, this.z() + z);
|
| 230 | }
|
| 231 |
|
| 232 | public Stream<BlockPos> blocksInside() {
|
| 233 | return BlockPos.betweenClosedStream(this.minBlockX(), this.minBlockY(), this.minBlockZ(), this.maxBlockX(), this.maxBlockY(), this.maxBlockZ());
|
| 234 | }
|
| 235 |
|
| 236 | public static Stream<SectionPos> cube(final SectionPos center, final int radius) {
|
| 237 | int x = center.x();
|
| 238 | int y = center.y();
|
| 239 | int z = center.z();
|
| 240 | return betweenClosedStream(x - radius, y - radius, z - radius, x + radius, y + radius, z + radius);
|
| 241 | }
|
| 242 |
|
| 243 | public static Stream<SectionPos> aroundChunk(final ChunkPos center, final int radius, final int minSection, final int maxSection) {
|
| 244 | int x = center.x();
|
| 245 | int z = center.z();
|
| 246 | return betweenClosedStream(x - radius, minSection, z - radius, x + radius, maxSection, z + radius);
|
| 247 | }
|
| 248 |
|
| 249 | public static Stream<SectionPos> betweenClosedStream(final int minX, final int minY, final int minZ, final int maxX, final int maxY, final int maxZ) {
|
| 250 | return StreamSupport.stream(new AbstractSpliterator<SectionPos>((maxX - minX + 1) * (maxY - minY + 1) * (maxZ - minZ + 1), 64) {
|
| 251 | private final Cursor3D cursor = new Cursor3D(minX, minY, minZ, maxX, maxY, maxZ);
|
| 252 |
|
| 253 | @Override
|
| 254 | public boolean tryAdvance(final Consumer<? super SectionPos> action) {
|
| 255 | if (this.cursor.advance()) {
|
| 256 | action.accept(new SectionPos(this.cursor.nextX(), this.cursor.nextY(), this.cursor.nextZ()));
|
| 257 | return true;
|
| 258 | } else {
|
| 259 | return false;
|
| 260 | }
|
| 261 | }
|
| 262 | }, false);
|
| 263 | }
|
| 264 |
|
| 265 | public static void aroundAndAtBlockPos(final BlockPos blockPos, final LongConsumer sectionConsumer) {
|
| 266 | aroundAndAtBlockPos(blockPos.getX(), blockPos.getY(), blockPos.getZ(), sectionConsumer);
|
| 267 | }
|
| 268 |
|
| 269 | public static void aroundAndAtBlockPos(final long blockPos, final LongConsumer sectionConsumer) {
|
| 270 | aroundAndAtBlockPos(BlockPos.getX(blockPos), BlockPos.getY(blockPos), BlockPos.getZ(blockPos), sectionConsumer);
|
| 271 | }
|
| 272 |
|
| 273 | public static void aroundAndAtBlockPos(final int blockX, final int blockY, final int blockZ, final LongConsumer sectionConsumer) {
|
| 274 | int minSectionX = blockToSectionCoord(blockX - 1);
|
| 275 | int maxSectionX = blockToSectionCoord(blockX + 1);
|
| 276 | int minSectionY = blockToSectionCoord(blockY - 1);
|
| 277 | int maxSectionY = blockToSectionCoord(blockY + 1);
|
| 278 | int minSectionZ = blockToSectionCoord(blockZ - 1);
|
| 279 | int maxSectionZ = blockToSectionCoord(blockZ + 1);
|
| 280 | if (minSectionX == maxSectionX && minSectionY == maxSectionY && minSectionZ == maxSectionZ) {
|
| 281 | sectionConsumer.accept(asLong(minSectionX, minSectionY, minSectionZ));
|
| 282 | } else {
|
| 283 | for (int sectionX = minSectionX; sectionX <= maxSectionX; sectionX++) {
|
| 284 | for (int sectionY = minSectionY; sectionY <= maxSectionY; sectionY++) {
|
| 285 | for (int sectionZ = minSectionZ; sectionZ <= maxSectionZ; sectionZ++) {
|
| 286 | sectionConsumer.accept(asLong(sectionX, sectionY, sectionZ));
|
| 287 | }
|
| 288 | }
|
| 289 | }
|
| 290 | }
|
| 291 | }
|
| 292 | }
|
| 293 |
|