ChunkPos .java

net.minecraft.world.level.ChunkPos (record): members: getMiddleBlockPosition, getChessboardDistance, INVALID_CHUNK_POS, REGION_MAX_INDEX, getWorldPosition.

net.minecraft.world.levelrecord239 linescommon6.6 KB

API Surface (43)

record ChunkPos(int x, int z)
ChunkPos decode(final ByteBuf input)
void encode(final ByteBuf output, final ChunkPos value)
ChunkPos containing(final BlockPos pos)
ChunkPos unpack(final long key)
ChunkPos minFromRegion(final int regionX, final int regionZ)
ChunkPos maxFromRegion(final int regionX, final int regionZ)
boolean isValid()
boolean isValid(final int x, final int z)
long pack()
long pack(final int x, final int z)
long fromSectionNode(final long sectionNode)
long pack(final BlockPos pos)
int getX(final long pos)
int getZ(final long pos)
int hashCode()
int hash(final int x, final int z)
int getMiddleBlockX()
int getMiddleBlockZ()
int getMinBlockX()
int getMinBlockZ()
int getMaxBlockX()
int getMaxBlockZ()
int getRegionX()
int getRegionZ()
int getRegionX(final long pos)
int getRegionZ(final long pos)
int getRegionLocalX()
int getRegionLocalZ()
BlockPos getBlockAt(final int x, final int y, final int z)
int getBlockX(final int offset)
int getBlockZ(final int offset)
BlockPos getMiddleBlockPosition(final int y)
boolean contains(final BlockPos pos)
String toString()
BlockPos getWorldPosition()
int getChessboardDistance(final ChunkPos pos)
int getChessboardDistance(final int x, final int z)
int distanceSquared(final ChunkPos pos)
int distanceSquared(final long pos)
Stream<ChunkPos> rangeClosed(final ChunkPos center, final int radius)
Stream<ChunkPos> rangeClosed(final ChunkPos from, final ChunkPos to)
boolean tryAdvance(final Consumer<? super ChunkPos> action)

Fields & Constants (14)

CODEC
STREAM_CODEC
SAFETY_MARGIN
INVALID_CHUNK_POS
ZERO
COORD_BITS
COORD_MASK
REGION_BITS
REGION_SIZE
REGION_MASK
REGION_MAX_INDEX
HASH_A
HASH_C
HASH_Z_XOR

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

ChunkPos.java239 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level;
2
3
import com.mojang.serialization.Codec;
4
import io.netty.buffer.ByteBuf;
5
import java.util.Spliterators.AbstractSpliterator;
6
import java.util.function.Consumer;
7
import java.util.stream.IntStream;
8
import java.util.stream.Stream;
9
import java.util.stream.StreamSupport;
10
import net.minecraft.core.BlockPos;
11
import net.minecraft.core.SectionPos;
12
import net.minecraft.network.FriendlyByteBuf;
13
import net.minecraft.network.codec.StreamCodec;
14
import net.minecraft.util.Mth;
15
import net.minecraft.util.Util;
16
import net.minecraft.world.level.chunk.status.ChunkPyramid;
17
import org.jspecify.annotations.Nullable;
18
19
public record ChunkPos(int x, int z) {
20
	public static final Codec<ChunkPos> CODEC = Codec.INT_STREAM
21
		.<ChunkPos>comapFlatMap(input -> Util.fixedSize(input, 2).map(ints -> new ChunkPos(ints[0], ints[1])), pos -> IntStream.of(pos.x, pos.z))
22
		.stable();
23
	public static final StreamCodec<ByteBuf, ChunkPos> STREAM_CODEC = new StreamCodec<ByteBuf, ChunkPos>() {
24
		public ChunkPos decode(final ByteBuf input) {
25
			return FriendlyByteBuf.readChunkPos(input);
26
		}
27
28
		public void encode(final ByteBuf output, final ChunkPos value) {
29
			FriendlyByteBuf.writeChunkPos(output, value);
30
		}
31
	};
32
	private static final int SAFETY_MARGIN = 1056;
33
	public static final long INVALID_CHUNK_POS = pack(1875066, 1875066);
34
	public static final ChunkPos ZERO = new ChunkPos(0, 0);
35
	private static final long COORD_BITS = 32L;
36
	private static final long COORD_MASK = 4294967295L;
37
	private static final int REGION_BITS = 5;
38
	public static final int REGION_SIZE = 32;
39
	private static final int REGION_MASK = 31;
40
	public static final int REGION_MAX_INDEX = 31;
41
	private static final int HASH_A = 1664525;
42
	private static final int HASH_C = 1013904223;
43
	private static final int HASH_Z_XOR = -559038737;
44
45
	public static ChunkPos containing(final BlockPos pos) {
46
		return new ChunkPos(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ()));
47
	}
48
49
	public static ChunkPos unpack(final long key) {
50
		return new ChunkPos((int)key, (int)(key >> 32));
51
	}
52
53
	public static ChunkPos minFromRegion(final int regionX, final int regionZ) {
54
		return new ChunkPos(regionX << 5, regionZ << 5);
55
	}
56
57
	public static ChunkPos maxFromRegion(final int regionX, final int regionZ) {
58
		return new ChunkPos((regionX << 5) + 31, (regionZ << 5) + 31);
59
	}
60
61
	public boolean isValid() {
62
		return isValid(this.x, this.z);
63
	}
64
65
	public static boolean isValid(final int x, final int z) {
66
		return Mth.absMax(x, z) <= ChunkPyramid.MAX_CHUNK_COORDINATE_VALUE;
67
	}
68
69
	public long pack() {
70
		return pack(this.x, this.z);
71
	}
72
73
	public static long pack(final int x, final int z) {
74
		return x & 4294967295L | (z & 4294967295L) << 32;
75
	}
76
77
	public static long fromSectionNode(final long sectionNode) {
78
		return pack(SectionPos.x(sectionNode), SectionPos.z(sectionNode));
79
	}
80
81
	public static long pack(final BlockPos pos) {
82
		return pack(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ()));
83
	}
84
85
	public static int getX(final long pos) {
86
		return (int)(pos & 4294967295L);
87
	}
88
89
	public static int getZ(final long pos) {
90
		return (int)(pos >>> 32 & 4294967295L);
91
	}
92
93
	@Override
94
	public int hashCode() {
95
		return hash(this.x, this.z);
96
	}
97
98
	public static int hash(final int x, final int z) {
99
		int xTransform = 1664525 * x + 1013904223;
100
		int zTransform = 1664525 * (z ^ -559038737) + 1013904223;
101
		return xTransform ^ zTransform;
102
	}
103
104
	public int getMiddleBlockX() {
105
		return this.getBlockX(8);
106
	}
107
108
	public int getMiddleBlockZ() {
109
		return this.getBlockZ(8);
110
	}
111
112
	public int getMinBlockX() {
113
		return SectionPos.sectionToBlockCoord(this.x);
114
	}
115
116
	public int getMinBlockZ() {
117
		return SectionPos.sectionToBlockCoord(this.z);
118
	}
119
120
	public int getMaxBlockX() {
121
		return this.getBlockX(15);
122
	}
123
124
	public int getMaxBlockZ() {
125
		return this.getBlockZ(15);
126
	}
127
128
	public int getRegionX() {
129
		return this.x >> 5;
130
	}
131
132
	public int getRegionZ() {
133
		return this.z >> 5;
134
	}
135
136
	public static int getRegionX(final long pos) {
137
		return getX(pos) >> 5;
138
	}
139
140
	public static int getRegionZ(final long pos) {
141
		return getZ(pos) >> 5;
142
	}
143
144
	public int getRegionLocalX() {
145
		return this.x & 31;
146
	}
147
148
	public int getRegionLocalZ() {
149
		return this.z & 31;
150
	}
151
152
	public BlockPos getBlockAt(final int x, final int y, final int z) {
153
		return new BlockPos(this.getBlockX(x), y, this.getBlockZ(z));
154
	}
155
156
	public int getBlockX(final int offset) {
157
		return SectionPos.sectionToBlockCoord(this.x, offset);
158
	}
159
160
	public int getBlockZ(final int offset) {
161
		return SectionPos.sectionToBlockCoord(this.z, offset);
162
	}
163
164
	public BlockPos getMiddleBlockPosition(final int y) {
165
		return new BlockPos(this.getMiddleBlockX(), y, this.getMiddleBlockZ());
166
	}
167
168
	public boolean contains(final BlockPos pos) {
169
		return pos.getX() >= this.getMinBlockX() && pos.getZ() >= this.getMinBlockZ() && pos.getX() <= this.getMaxBlockX() && pos.getZ() <= this.getMaxBlockZ();
170
	}
171
172
	@Override
173
	public String toString() {
174
		return "[" + this.x + ", " + this.z + "]";
175
	}
176
177
	public BlockPos getWorldPosition() {
178
		return new BlockPos(this.getMinBlockX(), 0, this.getMinBlockZ());
179
	}
180
181
	public int getChessboardDistance(final ChunkPos pos) {
182
		return this.getChessboardDistance(pos.x, pos.z);
183
	}
184
185
	public int getChessboardDistance(final int x, final int z) {
186
		return Mth.chessboardDistance(x, z, this.x, this.z);
187
	}
188
189
	public int distanceSquared(final ChunkPos pos) {
190
		return this.distanceSquared(pos.x, pos.z);
191
	}
192
193
	public int distanceSquared(final long pos) {
194
		return this.distanceSquared(getX(pos), getZ(pos));
195
	}
196
197
	private int distanceSquared(final int x, final int z) {
198
		int deltaX = x - this.x;
199
		int deltaZ = z - this.z;
200
		return deltaX * deltaX + deltaZ * deltaZ;
201
	}
202
203
	public static Stream<ChunkPos> rangeClosed(final ChunkPos center, final int radius) {
204
		return rangeClosed(new ChunkPos(center.x - radius, center.z - radius), new ChunkPos(center.x + radius, center.z + radius));
205
	}
206
207
	public static Stream<ChunkPos> rangeClosed(final ChunkPos from, final ChunkPos to) {
208
		int xSize = Math.abs(from.x - to.x) + 1;
209
		int zSize = Math.abs(from.z - to.z) + 1;
210
		final int xDiff = from.x < to.x ? 1 : -1;
211
		final int zDiff = from.z < to.z ? 1 : -1;
212
		return StreamSupport.stream(new AbstractSpliterator<ChunkPos>(xSize * zSize, 64) {
213
			private @Nullable ChunkPos pos;
214
215
			@Override
216
			public boolean tryAdvance(final Consumer<? super ChunkPos> action) {
217
				if (this.pos == null) {
218
					this.pos = from;
219
				} else {
220
					int x = this.pos.x;
221
					int z = this.pos.z;
222
					if (x == to.x) {
223
						if (z == to.z) {
224
							return false;
225
						}
226
227
						this.pos = new ChunkPos(from.x, z + zDiff);
228
					} else {
229
						this.pos = new ChunkPos(x + xDiff, z);
230
					}
231
				}
232
233
				action.accept(this.pos);
234
				return true;
235
			}
236
		}, false);
237
	}
238
}
239

Decompiled Minecraft 26.3 source of ChunkPos (net/minecraft/world/level/ChunkPos.java) with Mojang official mappings, stored verbatim on the site.