package net.minecraft.network.protocol.game;

import net.minecraft.network.PacketListener;
import java.util.Iterator;
import java.io.IOException;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.chunk.DataLayer;
import net.minecraft.core.SectionPos;
import net.minecraft.world.level.LightLayer;
import com.google.common.collect.Lists;
import net.minecraft.world.level.lighting.LevelLightEngine;
import net.minecraft.world.level.ChunkPos;
import java.util.List;
import net.minecraft.network.protocol.Packet;

public class ClientboundLightUpdatePacket implements Packet<ClientGamePacketListener> {
    private int x;
    private int z;
    private int skyYMask;
    private int blockYMask;
    private int emptySkyYMask;
    private int emptyBlockYMask;
    private List<byte[]> skyUpdates;
    private List<byte[]> blockUpdates;
    
    public ClientboundLightUpdatePacket() {
    }
    
    public ClientboundLightUpdatePacket(final ChunkPos bjh, final LevelLightEngine coa) {
        this.x = bjh.x;
        this.z = bjh.z;
        this.skyUpdates = Lists.newArrayList();
        this.blockUpdates = Lists.newArrayList();
        for (int integer4 = 0; integer4 < 18; ++integer4) {
            final DataLayer caf5 = coa.getLayerListener(LightLayer.SKY).getDataLayerData(SectionPos.of(bjh, -1 + integer4));
            final DataLayer caf6 = coa.getLayerListener(LightLayer.BLOCK).getDataLayerData(SectionPos.of(bjh, -1 + integer4));
            if (caf5 != null) {
                if (caf5.isEmpty()) {
                    this.emptySkyYMask |= 1 << integer4;
                }
                else {
                    this.skyYMask |= 1 << integer4;
                    this.skyUpdates.add(caf5.getData().clone());
                }
            }
            if (caf6 != null) {
                if (caf6.isEmpty()) {
                    this.emptyBlockYMask |= 1 << integer4;
                }
                else {
                    this.blockYMask |= 1 << integer4;
                    this.blockUpdates.add(caf6.getData().clone());
                }
            }
        }
    }
    
    public ClientboundLightUpdatePacket(final ChunkPos bjh, final LevelLightEngine coa, final int integer3, final int integer4) {
        this.x = bjh.x;
        this.z = bjh.z;
        this.skyYMask = integer3;
        this.blockYMask = integer4;
        this.skyUpdates = Lists.newArrayList();
        this.blockUpdates = Lists.newArrayList();
        for (int integer5 = 0; integer5 < 18; ++integer5) {
            if ((this.skyYMask & 1 << integer5) != 0x0) {
                final DataLayer caf7 = coa.getLayerListener(LightLayer.SKY).getDataLayerData(SectionPos.of(bjh, -1 + integer5));
                if (caf7 == null || caf7.isEmpty()) {
                    this.skyYMask &= ~(1 << integer5);
                    if (caf7 != null) {
                        this.emptySkyYMask |= 1 << integer5;
                    }
                }
                else {
                    this.skyUpdates.add(caf7.getData().clone());
                }
            }
            if ((this.blockYMask & 1 << integer5) != 0x0) {
                final DataLayer caf7 = coa.getLayerListener(LightLayer.BLOCK).getDataLayerData(SectionPos.of(bjh, -1 + integer5));
                if (caf7 == null || caf7.isEmpty()) {
                    this.blockYMask &= ~(1 << integer5);
                    if (caf7 != null) {
                        this.emptyBlockYMask |= 1 << integer5;
                    }
                }
                else {
                    this.blockUpdates.add(caf7.getData().clone());
                }
            }
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf kv) throws IOException {
        this.x = kv.readVarInt();
        this.z = kv.readVarInt();
        this.skyYMask = kv.readVarInt();
        this.blockYMask = kv.readVarInt();
        this.emptySkyYMask = kv.readVarInt();
        this.emptyBlockYMask = kv.readVarInt();
        this.skyUpdates = Lists.newArrayList();
        for (int integer3 = 0; integer3 < 18; ++integer3) {
            if ((this.skyYMask & 1 << integer3) != 0x0) {
                this.skyUpdates.add(kv.readByteArray(2048));
            }
        }
        this.blockUpdates = Lists.newArrayList();
        for (int integer3 = 0; integer3 < 18; ++integer3) {
            if ((this.blockYMask & 1 << integer3) != 0x0) {
                this.blockUpdates.add(kv.readByteArray(2048));
            }
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf kv) throws IOException {
        kv.writeVarInt(this.x);
        kv.writeVarInt(this.z);
        kv.writeVarInt(this.skyYMask);
        kv.writeVarInt(this.blockYMask);
        kv.writeVarInt(this.emptySkyYMask);
        kv.writeVarInt(this.emptyBlockYMask);
        for (final byte[] arr4 : this.skyUpdates) {
            kv.writeByteArray(arr4);
        }
        for (final byte[] arr4 : this.blockUpdates) {
            kv.writeByteArray(arr4);
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener lw) {
        lw.handleLightUpdatePacked(this);
    }
    
    public int getX() {
        return this.x;
    }
    
    public int getZ() {
        return this.z;
    }
    
    public int getSkyYMask() {
        return this.skyYMask;
    }
    
    public int getEmptySkyYMask() {
        return this.emptySkyYMask;
    }
    
    public List<byte[]> getSkyUpdates() {
        return this.skyUpdates;
    }
    
    public int getBlockYMask() {
        return this.blockYMask;
    }
    
    public int getEmptyBlockYMask() {
        return this.emptyBlockYMask;
    }
    
    public List<byte[]> getBlockUpdates() {
        return this.blockUpdates;
    }
}
