package protocolsupport.protocol.packet.middle.impl.clientbound.play.v_4_5_6_7_8_9r1_9r2_10_11_12r1_12r2_13;

import java.util.BitSet;

import protocolsupport.protocol.packet.middle.MiddlePacketCancelException;
import protocolsupport.protocol.packet.middle.impl.clientbound.play.v_4_5_6_7_8_9r1_9r2_10_11_12r1_12r2_13_14r1_14r2_15_16r1_16r2.AbstractLimitedHeightChunkLight;
import protocolsupport.protocol.storage.netcache.chunk.LimitedHeightCachedChunk;
import protocolsupport.protocol.storage.netcache.chunk.LimitedHeightChunkCache;
import protocolsupport.protocol.types.chunk.ChunkConstants;

public abstract class AbstractChunkCacheChunkLight extends AbstractLimitedHeightChunkLight {

	protected AbstractChunkCacheChunkLight(IMiddlePacketInit init) {
		super(init);
	}

	protected final LimitedHeightChunkCache chunkCache = cache.getChunkCache();

	protected LimitedHeightCachedChunk cachedChunk;
	protected BitSet blockMask;

	@Override
	protected void handle() {
		super.handle();

		boolean chunkLoaded = false;
		cachedChunk = chunkCache.get(coord);
		if (cachedChunk != null) {
			if (cachedChunk.isFull()) {
				chunkLoaded = true;
			}
		} else {
			cachedChunk = chunkCache.add(coord);
		}

		for (int sectionIndex = 1; sectionIndex < (ChunkConstants.LEGACY_LIMITED_HEIGHT_CHUNK_LIGHT_SECTIONS - 1); sectionIndex++) {
			if (setSkyLightMask.get(sectionIndex)) {
				cachedChunk.setSkyLightSection(sectionIndex - 1, skyLight[sectionIndex]);
			} else if (emptySkyLightMask.get(sectionIndex)) {
				cachedChunk.setSkyLightSection(sectionIndex - 1, null);
			}

			if (setBlockLightMask.get(sectionIndex)) {
				cachedChunk.setBlockLightSection(sectionIndex - 1, blockLight[sectionIndex]);
			} else if (emptyBlockLightMask.get(sectionIndex)) {
				cachedChunk.setBlockLightSection(sectionIndex - 1, null);
			}
		}

		if (!chunkLoaded) {
			throw MiddlePacketCancelException.INSTANCE;
		}

		BitSet fullLightMask = new BitSet();
		fullLightMask.or(setSkyLightMask);
		fullLightMask.or(emptySkyLightMask);
		fullLightMask.or(setBlockLightMask);
		fullLightMask.or(emptyBlockLightMask);
		blockMask = fullLightMask.get(1, 1 + ChunkConstants.LEGACY_LIMITED_HEIGHT_CHUNK_BLOCK_SECTIONS);
	}

}

@@ -1197,16 +1197,392 @@
 );%0A%09%09%7D%0A%0A
+%09%09BitSet fullLightMask = new BitSet();%0A%09%09fullLightMask.or(setSkyLightMask);%0A%09%09fullLightMask.or(emptySkyLightMask);%0A%09%09fullLightMask.or(setBlockLightMask);%0A%09%09fullLightMask.or(emptyBlockLightMask);%0A%09%09blockMask = fullLightMask.get(1, 1 + ChunkConstants.LEGACY_LIMITED_HEIGHT_CHUNK_BLOCK_SECTIONS);%0A%0A%09%09if (blockMask.isEmpty()) %7B%0A%09%09%09throw MiddlePacketCancelException.INSTANCE;%0A%09%09%7D%0A%0A
 %09%09for (i
@@ -2272,303 +2272,8 @@
 %0A%09%09%7D
-%0A%0A%09%09BitSet fullLightMask = new BitSet();%0A%09%09fullLightMask.or(setSkyLightMask);%0A%09%09fullLightMask.or(emptySkyLightMask);%0A%09%09fullLightMask.or(setBlockLightMask);%0A%09%09fullLightMask.or(emptyBlockLightMask);%0A%09%09blockMask = fullLightMask.get(1, 1 + ChunkConstants.LEGACY_LIMITED_HEIGHT_CHUNK_BLOCK_SECTIONS);
 %0A%09%7D%0A

Improve chunk light updates for < 1.14