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;
    private boolean trustEdges;
    
    public ClientboundLightUpdatePacket() {
    }
    
    public ClientboundLightUpdatePacket(final ChunkPos bph, final LevelLightEngine cwr, final boolean boolean3) {
        this.x = bph.x;
        this.z = bph.z;
        this.trustEdges = boolean3;
        this.skyUpdates = Lists.newArrayList();
        this.blockUpdates = Lists.newArrayList();
        for (int integer5 = 0; integer5 < 18; ++integer5) {
            final DataLayer chd6 = cwr.getLayerListener(LightLayer.SKY).getDataLayerData(SectionPos.of(bph, -1 + integer5));
            final DataLayer chd7 = cwr.getLayerListener(LightLayer.BLOCK).getDataLayerData(SectionPos.of(bph, -1 + integer5));
            if (chd6 != null) {
                if (chd6.isEmpty()) {
                    this.emptySkyYMask |= 1 << integer5;
                }
                else {
                    this.skyYMask |= 1 << integer5;
                    this.skyUpdates.add(chd6.getData().clone());
                }
            }
            if (chd7 != null) {
                if (chd7.isEmpty()) {
                    this.emptyBlockYMask |= 1 << integer5;
                }
                else {
                    this.blockYMask |= 1 << integer5;
                    this.blockUpdates.add(chd7.getData().clone());
                }
            }
        }
    }
    
    public ClientboundLightUpdatePacket(final ChunkPos bph, final LevelLightEngine cwr, final int integer3, final int integer4, final boolean boolean5) {
        this.x = bph.x;
        this.z = bph.z;
        this.trustEdges = boolean5;
        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 chd8 = cwr.getLayerListener(LightLayer.SKY).getDataLayerData(SectionPos.of(bph, -1 + integer5));
                if (chd8 == null || chd8.isEmpty()) {
                    this.skyYMask &= ~(1 << integer5);
                    if (chd8 != null) {
                        this.emptySkyYMask |= 1 << integer5;
                    }
                }
                else {
                    this.skyUpdates.add(chd8.getData().clone());
                }
            }
            if ((this.blockYMask & 1 << integer5) != 0x0) {
                final DataLayer chd8 = cwr.getLayerListener(LightLayer.BLOCK).getDataLayerData(SectionPos.of(bph, -1 + integer5));
                if (chd8 == null || chd8.isEmpty()) {
                    this.blockYMask &= ~(1 << integer5);
                    if (chd8 != null) {
                        this.emptyBlockYMask |= 1 << integer5;
                    }
                }
                else {
                    this.blockUpdates.add(chd8.getData().clone());
                }
            }
        }
    }
    
    @Override
    public void read(final FriendlyByteBuf mg) throws IOException {
        this.x = mg.readVarInt();
        this.z = mg.readVarInt();
        this.trustEdges = mg.readBoolean();
        this.skyYMask = mg.readVarInt();
        this.blockYMask = mg.readVarInt();
        this.emptySkyYMask = mg.readVarInt();
        this.emptyBlockYMask = mg.readVarInt();
        this.skyUpdates = Lists.newArrayList();
        for (int integer3 = 0; integer3 < 18; ++integer3) {
            if ((this.skyYMask & 1 << integer3) != 0x0) {
                this.skyUpdates.add(mg.readByteArray(2048));
            }
        }
        this.blockUpdates = Lists.newArrayList();
        for (int integer3 = 0; integer3 < 18; ++integer3) {
            if ((this.blockYMask & 1 << integer3) != 0x0) {
                this.blockUpdates.add(mg.readByteArray(2048));
            }
        }
    }
    
    @Override
    public void write(final FriendlyByteBuf mg) throws IOException {
        mg.writeVarInt(this.x);
        mg.writeVarInt(this.z);
        mg.writeBoolean(this.trustEdges);
        mg.writeVarInt(this.skyYMask);
        mg.writeVarInt(this.blockYMask);
        mg.writeVarInt(this.emptySkyYMask);
        mg.writeVarInt(this.emptyBlockYMask);
        for (final byte[] arr4 : this.skyUpdates) {
            mg.writeByteArray(arr4);
        }
        for (final byte[] arr4 : this.blockUpdates) {
            mg.writeByteArray(arr4);
        }
    }
    
    @Override
    public void handle(final ClientGamePacketListener nl) {
        nl.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;
    }
    
    public boolean getTrustEdges() {
        return this.trustEdges;
    }
}
