| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import io.netty.buffer.Unpooled;
|
| 5 | import java.util.List;
|
| 6 | import net.minecraft.network.FriendlyByteBuf;
|
| 7 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 | import net.minecraft.network.protocol.Packet;
|
| 10 | import net.minecraft.network.protocol.PacketType;
|
| 11 | import net.minecraft.world.level.ChunkPos;
|
| 12 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 13 | import net.minecraft.world.level.chunk.LevelChunkSection;
|
| 14 |
|
| 15 | public record ClientboundChunksBiomesPacket(List<ClientboundChunksBiomesPacket.ChunkBiomeData> chunkBiomeData) implements Packet<ClientGamePacketListener> {
|
| 16 | public static final StreamCodec<ByteBuf, ClientboundChunksBiomesPacket> STREAM_CODEC = StreamCodec.composite(
|
| 17 | ClientboundChunksBiomesPacket.ChunkBiomeData.STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 18 | ClientboundChunksBiomesPacket::chunkBiomeData,
|
| 19 | ClientboundChunksBiomesPacket::new
|
| 20 | );
|
| 21 | private static final int TWO_MEGABYTES = 2097152;
|
| 22 |
|
| 23 | public static ClientboundChunksBiomesPacket forChunks(final List<LevelChunk> chunks) {
|
| 24 | return new ClientboundChunksBiomesPacket(chunks.stream().map(ClientboundChunksBiomesPacket.ChunkBiomeData::new).toList());
|
| 25 | }
|
| 26 |
|
| 27 | @Override
|
| 28 | public PacketType<ClientboundChunksBiomesPacket> type() {
|
| 29 | return GamePacketTypes.CLIENTBOUND_CHUNKS_BIOMES;
|
| 30 | }
|
| 31 |
|
| 32 | public void handle(final ClientGamePacketListener listener) {
|
| 33 | listener.handleChunksBiomes(this);
|
| 34 | }
|
| 35 |
|
| 36 | public record ChunkBiomeData(ChunkPos pos, byte[] buffer) {
|
| 37 | public static final StreamCodec<ByteBuf, ClientboundChunksBiomesPacket.ChunkBiomeData> STREAM_CODEC = StreamCodec.composite(
|
| 38 | ChunkPos.STREAM_CODEC,
|
| 39 | ClientboundChunksBiomesPacket.ChunkBiomeData::pos,
|
| 40 | ByteBufCodecs.byteArray(2097152),
|
| 41 | ClientboundChunksBiomesPacket.ChunkBiomeData::buffer,
|
| 42 | ClientboundChunksBiomesPacket.ChunkBiomeData::new
|
| 43 | );
|
| 44 |
|
| 45 | public ChunkBiomeData(final LevelChunk chunk) {
|
| 46 | this(chunk.getPos(), new byte[calculateChunkSize(chunk)]);
|
| 47 | extractChunkData(new FriendlyByteBuf(this.getWriteBuffer()), chunk);
|
| 48 | }
|
| 49 |
|
| 50 | private static int calculateChunkSize(final LevelChunk chunk) {
|
| 51 | int total = 0;
|
| 52 |
|
| 53 | for (LevelChunkSection section : chunk.getSections()) {
|
| 54 | total += section.getBiomes().getSerializedSize();
|
| 55 | }
|
| 56 |
|
| 57 | return total;
|
| 58 | }
|
| 59 |
|
| 60 | public FriendlyByteBuf getReadBuffer() {
|
| 61 | return new FriendlyByteBuf(Unpooled.wrappedBuffer(this.buffer));
|
| 62 | }
|
| 63 |
|
| 64 | private ByteBuf getWriteBuffer() {
|
| 65 | ByteBuf buffer = Unpooled.wrappedBuffer(this.buffer);
|
| 66 | buffer.writerIndex(0);
|
| 67 | return buffer;
|
| 68 | }
|
| 69 |
|
| 70 | public static void extractChunkData(final FriendlyByteBuf buffer, final LevelChunk chunk) {
|
| 71 | for (LevelChunkSection section : chunk.getSections()) {
|
| 72 | section.getBiomes().write(buffer);
|
| 73 | }
|
| 74 |
|
| 75 | if (buffer.writerIndex() != buffer.capacity()) {
|
| 76 | throw new IllegalStateException("Didn't fill biome buffer: expected " + buffer.capacity() + " bytes, got " + buffer.writerIndex());
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|