| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import io.netty.buffer.ByteBuf;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.BitSet;
|
| 6 | import java.util.List;
|
| 7 | import net.minecraft.core.SectionPos;
|
| 8 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 9 | import net.minecraft.network.codec.StreamCodec;
|
| 10 | import net.minecraft.world.level.ChunkPos;
|
| 11 | import net.minecraft.world.level.LightLayer;
|
| 12 | import net.minecraft.world.level.chunk.DataLayer;
|
| 13 | import net.minecraft.world.level.lighting.LevelLightEngine;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public record ClientboundLightUpdatePacketData(
|
| 17 | BitSet skyYMask, BitSet blockYMask, BitSet emptySkyYMask, BitSet emptyBlockYMask, List<byte[]> skyUpdates, List<byte[]> blockUpdates
|
| 18 | ) {
|
| 19 | private static final StreamCodec<ByteBuf, byte[]> DATA_LAYER_STREAM_CODEC = ByteBufCodecs.byteArray(2048);
|
| 20 | public static final StreamCodec<ByteBuf, ClientboundLightUpdatePacketData> STREAM_CODEC = StreamCodec.composite(
|
| 21 | ByteBufCodecs.BIT_SET,
|
| 22 | ClientboundLightUpdatePacketData::skyYMask,
|
| 23 | ByteBufCodecs.BIT_SET,
|
| 24 | ClientboundLightUpdatePacketData::blockYMask,
|
| 25 | ByteBufCodecs.BIT_SET,
|
| 26 | ClientboundLightUpdatePacketData::emptySkyYMask,
|
| 27 | ByteBufCodecs.BIT_SET,
|
| 28 | ClientboundLightUpdatePacketData::emptyBlockYMask,
|
| 29 | DATA_LAYER_STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 30 | ClientboundLightUpdatePacketData::skyUpdates,
|
| 31 | DATA_LAYER_STREAM_CODEC.apply(ByteBufCodecs.list()),
|
| 32 | ClientboundLightUpdatePacketData::blockUpdates,
|
| 33 | ClientboundLightUpdatePacketData::new
|
| 34 | );
|
| 35 |
|
| 36 | public ClientboundLightUpdatePacketData(
|
| 37 | final ChunkPos chunkPos,
|
| 38 | final LevelLightEngine lightEngine,
|
| 39 | final @Nullable BitSet skyChangedLightSectionFilter,
|
| 40 | final @Nullable BitSet blockChangedLightSectionFilter
|
| 41 | ) {
|
| 42 | this(new BitSet(), new BitSet(), new BitSet(), new BitSet(), new ArrayList<>(), new ArrayList<>());
|
| 43 |
|
| 44 | for (int sectionIndex = 0; sectionIndex < lightEngine.getLightSectionCount(); sectionIndex++) {
|
| 45 | if (skyChangedLightSectionFilter == null || skyChangedLightSectionFilter.get(sectionIndex)) {
|
| 46 | this.prepareSectionData(chunkPos, lightEngine, LightLayer.SKY, sectionIndex, this.skyYMask, this.emptySkyYMask, this.skyUpdates);
|
| 47 | }
|
| 48 |
|
| 49 | if (blockChangedLightSectionFilter == null || blockChangedLightSectionFilter.get(sectionIndex)) {
|
| 50 | this.prepareSectionData(chunkPos, lightEngine, LightLayer.BLOCK, sectionIndex, this.blockYMask, this.emptyBlockYMask, this.blockUpdates);
|
| 51 | }
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | private void prepareSectionData(
|
| 56 | final ChunkPos pos,
|
| 57 | final LevelLightEngine lightEngine,
|
| 58 | final LightLayer layer,
|
| 59 | final int sectionIndex,
|
| 60 | final BitSet mask,
|
| 61 | final BitSet emptyMask,
|
| 62 | final List<byte[]> updates
|
| 63 | ) {
|
| 64 | DataLayer data = lightEngine.getLayerListener(layer).getDataLayerData(SectionPos.of(pos, lightEngine.getMinLightSection() + sectionIndex));
|
| 65 | if (data != null) {
|
| 66 | if (data.isEmpty()) {
|
| 67 | emptyMask.set(sectionIndex);
|
| 68 | } else {
|
| 69 | mask.set(sectionIndex);
|
| 70 | updates.add(data.copy().getData());
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 | }
|
| 75 |
|