ClientboundLevelChunkPacketData .java

net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData (class, 142 lines): members: HEIGHTMAPS_STREAM_CODEC, forEachBlockEntityTag, buffer.

net.minecraft.network.protocol.gameclass142 linescommon5.6 KB

API Surface (4)

void extractChunkData(final FriendlyByteBuf buffer, final LevelChunk chunk)
void forEachBlockEntityTag(final int chunkX, final int chunkZ, final ClientboundLevelChunkPacketData.BlockEntityTagOutput output)
FriendlyByteBuf getReadBuffer()
Map<Heightmap.Types, long[]> getHeightmaps()

Fields & Constants (6)

HEIGHTMAPS_STREAM_CODEC
TWO_MEGABYTES
STREAM_CODEC
heightmaps
buffer
blockEntitiesData

String literals (2)

"Didn't fill chunk buffer: expected "
" bytes, got "

Same Package (197)

Full Source

ClientboundLevelChunkPacketData.java142 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.protocol.game;
2
3
import com.google.common.collect.Lists;
4
import io.netty.buffer.ByteBuf;
5
import io.netty.buffer.Unpooled;
6
import java.util.EnumMap;
7
import java.util.List;
8
import java.util.Map;
9
import java.util.Optional;
10
import java.util.Map.Entry;
11
import java.util.stream.Collectors;
12
import net.minecraft.core.BlockPos;
13
import net.minecraft.core.SectionPos;
14
import net.minecraft.core.registries.Registries;
15
import net.minecraft.nbt.CompoundTag;
16
import net.minecraft.network.FriendlyByteBuf;
17
import net.minecraft.network.RegistryFriendlyByteBuf;
18
import net.minecraft.network.codec.ByteBufCodecs;
19
import net.minecraft.network.codec.StreamCodec;
20
import net.minecraft.world.level.block.entity.BlockEntity;
21
import net.minecraft.world.level.block.entity.BlockEntityType;
22
import net.minecraft.world.level.chunk.LevelChunk;
23
import net.minecraft.world.level.chunk.LevelChunkSection;
24
import net.minecraft.world.level.levelgen.Heightmap;
25
import org.jspecify.annotations.Nullable;
26
27
public class ClientboundLevelChunkPacketData {
28
	private static final StreamCodec<ByteBuf, Map<Heightmap.Types, long[]>> HEIGHTMAPS_STREAM_CODEC = ByteBufCodecs.map(
29
		size -> new EnumMap<>(Heightmap.Types.class), Heightmap.Types.STREAM_CODEC, ByteBufCodecs.LONG_ARRAY
30
	);
31
	private static final int TWO_MEGABYTES = 2097152;
32
	public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundLevelChunkPacketData> STREAM_CODEC = StreamCodec.composite(
33
		HEIGHTMAPS_STREAM_CODEC,
34
		p -> p.heightmaps,
35
		ByteBufCodecs.byteArray(2097152),
36
		p -> p.buffer,
37
		ClientboundLevelChunkPacketData.BlockEntityInfo.STREAM_CODEC.apply(ByteBufCodecs.list()),
38
		p -> p.blockEntitiesData,
39
		ClientboundLevelChunkPacketData::new
40
	);
41
	private final Map<Heightmap.Types, long[]> heightmaps;
42
	private final byte[] buffer;
43
	private final List<ClientboundLevelChunkPacketData.BlockEntityInfo> blockEntitiesData;
44
45
	public ClientboundLevelChunkPacketData(final LevelChunk levelChunk) {
46
		this.heightmaps = levelChunk.getHeightmaps()
47
			.stream()
48
			.filter(entryx -> ((Heightmap.Types)entryx.getKey()).sendToClient())
49
			.collect(Collectors.toMap(Entry::getKey, entryx -> (long[])((Heightmap)entryx.getValue()).getRawData().clone()));
50
		this.buffer = new byte[calculateChunkSize(levelChunk)];
51
		extractChunkData(new FriendlyByteBuf(this.getWriteBuffer()), levelChunk);
52
		this.blockEntitiesData = Lists.newArrayList();
53
54
		for (Entry<BlockPos, BlockEntity> entry : levelChunk.getBlockEntities().entrySet()) {
55
			this.blockEntitiesData.add(ClientboundLevelChunkPacketData.BlockEntityInfo.create(entry.getValue()));
56
		}
57
	}
58
59
	private ClientboundLevelChunkPacketData(
60
		final Map<Heightmap.Types, long[]> heightmaps, final byte[] buffer, final List<ClientboundLevelChunkPacketData.BlockEntityInfo> blockEntitiesData
61
	) {
62
		this.heightmaps = heightmaps;
63
		this.buffer = buffer;
64
		this.blockEntitiesData = blockEntitiesData;
65
	}
66
67
	private static int calculateChunkSize(final LevelChunk chunk) {
68
		int total = 0;
69
70
		for (LevelChunkSection section : chunk.getSections()) {
71
			total += section.getSerializedSize();
72
		}
73
74
		return total;
75
	}
76
77
	private ByteBuf getWriteBuffer() {
78
		ByteBuf buffer = Unpooled.wrappedBuffer(this.buffer);
79
		buffer.writerIndex(0);
80
		return buffer;
81
	}
82
83
	public static void extractChunkData(final FriendlyByteBuf buffer, final LevelChunk chunk) {
84
		for (LevelChunkSection section : chunk.getSections()) {
85
			section.write(buffer);
86
		}
87
88
		if (buffer.writerIndex() != buffer.capacity()) {
89
			throw new IllegalStateException("Didn't fill chunk buffer: expected " + buffer.capacity() + " bytes, got " + buffer.writerIndex());
90
		}
91
	}
92
93
	public void forEachBlockEntityTag(final int chunkX, final int chunkZ, final ClientboundLevelChunkPacketData.BlockEntityTagOutput output) {
94
		int baseX = 16 * chunkX;
95
		int baseZ = 16 * chunkZ;
96
		BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
97
98
		for (ClientboundLevelChunkPacketData.BlockEntityInfo data : this.blockEntitiesData) {
99
			int unpackedX = baseX + SectionPos.sectionRelative(data.packedXZ >> 4);
100
			int unpackedZ = baseZ + SectionPos.sectionRelative(data.packedXZ);
101
			pos.set(unpackedX, data.y, unpackedZ);
102
			output.accept(pos, data.type, data.tag.orElse(null));
103
		}
104
	}
105
106
	public FriendlyByteBuf getReadBuffer() {
107
		return new FriendlyByteBuf(Unpooled.wrappedBuffer(this.buffer));
108
	}
109
110
	public Map<Heightmap.Types, long[]> getHeightmaps() {
111
		return this.heightmaps;
112
	}
113
114
	private record BlockEntityInfo(byte packedXZ, short y, BlockEntityType<?> type, Optional<CompoundTag> tag) {
115
		public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundLevelChunkPacketData.BlockEntityInfo> STREAM_CODEC = StreamCodec.composite(
116
			ByteBufCodecs.BYTE,
117
			ClientboundLevelChunkPacketData.BlockEntityInfo::packedXZ,
118
			ByteBufCodecs.SHORT,
119
			ClientboundLevelChunkPacketData.BlockEntityInfo::y,
120
			ByteBufCodecs.registry(Registries.BLOCK_ENTITY_TYPE),
121
			ClientboundLevelChunkPacketData.BlockEntityInfo::type,
122
			ByteBufCodecs.OPTIONAL_COMPOUND_TAG,
123
			ClientboundLevelChunkPacketData.BlockEntityInfo::tag,
124
			ClientboundLevelChunkPacketData.BlockEntityInfo::new
125
		);
126
127
		private static ClientboundLevelChunkPacketData.BlockEntityInfo create(final BlockEntity blockEntity) {
128
			CompoundTag tag = blockEntity.getUpdateTag(blockEntity.getLevel().registryAccess());
129
			BlockPos pos = blockEntity.getBlockPos();
130
			int xz = SectionPos.sectionRelative(pos.getX()) << 4 | SectionPos.sectionRelative(pos.getZ());
131
			return new ClientboundLevelChunkPacketData.BlockEntityInfo(
132
				(byte)xz, (short)pos.getY(), blockEntity.getType(), tag.isEmpty() ? Optional.empty() : Optional.of(tag)
133
			);
134
		}
135
	}
136
137
	@FunctionalInterface
138
	public interface BlockEntityTagOutput {
139
		void accept(BlockPos pos, BlockEntityType<?> type, @Nullable CompoundTag tag);
140
	}
141
}
142

Decompiled Minecraft 26.3 source of ClientboundLevelChunkPacketData (net/minecraft/network/protocol/game/ClientboundLevelChunkPacketData.java) with Mojang official mappings, stored verbatim on the site.