-
-
Save lenis0012/962afbb10cf5ffdbf88e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.myles.ViaVersion.chunks; | |
import io.netty.buffer.ByteBuf; | |
import org.bukkit.Bukkit; | |
import us.myles.ViaVersion.api.ViaVersion; | |
import us.myles.ViaVersion.util.PacketUtil; | |
import java.util.BitSet; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
public class ChunkManager { | |
/** | |
* Amount of sections in a chunk. | |
*/ | |
private static final int SECTION_COUNT = 16; | |
/** | |
* size of each chunk section (16x16x16). | |
*/ | |
private static final int SECTION_SIZE = 16; | |
/** | |
* Length of biome data. | |
*/ | |
private static final int BIOME_DATA_LENGTH = 256; | |
/** | |
* Read chunk from 1.8 chunk data. | |
* | |
* @param input data | |
* @return Chunk | |
*/ | |
public Chunk readChunk(ByteBuf input) { | |
// Primary data | |
int chunkX = PacketUtil.readVarInt(input); | |
int chunkZ = PacketUtil.readVarInt(input); | |
boolean groundUp = input.readBoolean(); | |
int bitmap = input.readShort(); | |
// Data to be read | |
BitSet usedSections = new BitSet(16); | |
ChunkSection[] sections = new ChunkSection[16]; | |
byte[] biomeData = null; | |
// Calculate section count from bitmap | |
for(int i = 0; i < 16; i++) { | |
if(((bitmap >> i) & 0x1) == 1) { | |
usedSections.set(i); | |
} | |
} | |
int sectionCount = usedSections.cardinality(); // the amount of sections set | |
// Predict the size of the remaining data | |
boolean hasSkyLight; | |
boolean hasBiomeData; | |
int bytesLeft = input.readableBytes(); | |
bytesLeft -= sectionCount * ChunkSection.BLOCK_LENGTH; // Subtract block data | |
bytesLeft -= sectionCount * ChunkSection.LIGHT_LENGTH; // Block light | |
bytesLeft -= (hasSkyLight = bytesLeft >= sectionCount * ChunkSection.LIGHT_LENGTH) ? sectionCount * ChunkSection.LIGHT_LENGTH : 0; | |
bytesLeft -= (hasBiomeData = bytesLeft >= BIOME_DATA_LENGTH) ? BIOME_DATA_LENGTH : 0; | |
if(bytesLeft > 0) { | |
Bukkit.getLogger().log(Level.WARNING, bytesLeft + " Bytes left after reading chunk!"); | |
} | |
// Read block ids | |
for(int i = 0; i < SECTION_COUNT; i++) { | |
if(!usedSections.get(i)) continue; // Section not set | |
sections[i] = new ChunkSection(); | |
for(int y = 0; y < SECTION_SIZE; y++) { | |
for(int z = 0; z < SECTION_SIZE; z++) { | |
for(int x = 0; x < SECTION_SIZE; x++) { | |
byte data = input.readByte(); | |
byte id = input.readByte(); | |
// TODO: Save to chunk section | |
} | |
} | |
} | |
} | |
// Read block light | |
for(int i = 0; i < SECTION_COUNT; i++) { | |
if(!usedSections.get(i)) continue; // Section not set, has no light | |
byte[] blockLightArray = new byte[ChunkSection.LIGHT_LENGTH]; | |
input.readBytes(blockLightArray); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment