Archived
0
This commit is contained in:
2018-08-02 15:59:11 +03:00
parent 0152377289
commit 55ef6eec66

View File

@@ -11,6 +11,7 @@ import lombok.extern.slf4j.Slf4j;
import mc.core.network.NetOutputStream; import mc.core.network.NetOutputStream;
import mc.core.network.SCPacket; import mc.core.network.SCPacket;
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream; import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
import mc.core.world.Block;
import mc.core.world.Chunk; import mc.core.world.Chunk;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@@ -40,7 +41,7 @@ Packet structure
| +------------------------------------------------+ | | +------------------------------------------------+ |
| | Chunk Section | Byte array | | - https://wiki.vg/Chunk_Format#Chunk_Section_structure | | Chunk Section | Byte array | | - https://wiki.vg/Chunk_Format#Chunk_Section_structure
| | +------------------------------------------+ | | | | +------------------------------------------+ | |
| | | Bits Per Block | Unsigned Byte | | | | | | Bits Per Block | Unsigned Byte | | | (we use 4 bits per block)
| | |--------------------|---------------------| | | | | |--------------------|---------------------| | |
| | | Palette | Byte array | | | - https://wiki.vg/Chunk_Format#Palettes | | | Palette | Byte array | | | - https://wiki.vg/Chunk_Format#Palettes
| | | +------------------------------------+ | | | (we use Indirect type palette) | | | +------------------------------------+ | | | (we use Indirect type palette)
@@ -53,9 +54,9 @@ Packet structure
| | |--------------------|---------------------| | | | | |--------------------|---------------------| | |
| | | Data Array | Array of Long | | | | | | Data Array | Array of Long | | |
| | |--------------------|---------------------| | | | | |--------------------|---------------------| | |
| | | Block Light | Byte Array | | | | | | Block Light | Byte Array | | | (Half byte per block)
| | |--------------------|---------------------| | | | | |--------------------|---------------------| | |
| | | Sky Light | Optional Byte Array | | | | | | Sky Light | Optional Byte Array | | | (Only if in the Overworld; half byte per block)
| | +------------------------------------------+ | | | | +------------------------------------------+ | |
| |-----------------------|------------------------| | | |-----------------------|------------------------| |
| | Biomes | Optional Byte array | | | | Biomes | Optional Byte array | |
@@ -79,25 +80,86 @@ public class ChunkDataPacket implements SCPacket {
@Getter @Getter
private List<Chunk> chunks = new ArrayList<>(); private List<Chunk> chunks = new ArrayList<>();
private long serializeBlockState(Block block) {
return (block.getId() << 4) | block.getState();
}
@Override @Override
public void writeSelf(NetOutputStream netStream) { public void writeSelf(NetOutputStream netStream) {
netStream.writeInt(x); netStream.writeInt(x); // Chunk X
netStream.writeInt(z); netStream.writeInt(z); // Chunk Y
netStream.writeBoolean(initChunk); netStream.writeBoolean(initChunk); // Init Chunk
netStream.writeVarInt(0b11111111); // Primary Bit Mask netStream.writeVarInt(0b11111111); // Primary Bit Mask
ByteArrayOutputNetStream baos = new ByteArrayOutputNetStream(); final ByteArrayOutputNetStream data = new ByteArrayOutputNetStream();
for (Chunk chunk : chunks) { for (Chunk chunk : chunks) {
// <Bits Per Block /> final List<Long> palette = new ArrayList<>();
// <Palette /> final ByteArrayOutputNetStream dataArray = new ByteArrayOutputNetStream();
// <Data Array Length /> final ByteArrayOutputNetStream blockLight = new ByteArrayOutputNetStream();
// <Data Array /> final ByteArrayOutputNetStream skyLight = new ByteArrayOutputNetStream();
// <Block Light /> final ByteArrayOutputNetStream biomes = new ByteArrayOutputNetStream();
// <Sky Light />
int blockLightCompacted = 0;
boolean flagFirstHalf = true;
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
Block block = chunk.getBlock(x, y, z);
long blockState = serializeBlockState(block);
int currentIndexPaletteBlock;
if (!palette.contains(blockState)) {
palette.add(blockState);
currentIndexPaletteBlock = palette.size()-1;
} else {
currentIndexPaletteBlock = palette.indexOf(blockState);
} }
netStream.writeVarInt(baos.size()); // Size of Data in bytes
netStream.writeBytes(baos.toByteArray()); // Data chunks dataArray.writeLong(currentIndexPaletteBlock);
netStream.writeVarInt(0); // size NBT if (flagFirstHalf) {
blockLightCompacted = block.getLight();
flagFirstHalf = false;
} else {
blockLightCompacted = (blockLightCompacted << 4) | block.getLight();
blockLight.writeByte(blockLightCompacted);
flagFirstHalf = true;
skyLight.writeByte(0b11111111); //FIXME
}
biomes.writeByte(chunk.getBiome(x, z));
}
}
}
// <Chunk Section>
// <Palette>
data.writeUnsignedByte(4); // Bits Per Block
data.writeVarInt(palette.size()); // Size of palette
palette.stream()
.mapToInt(Long::intValue)
.forEach(data::writeVarInt); // Palette
// </Palette>
// <Data Array>
data.writeVarInt(dataArray.size()); // Size of Data Array
data.writeBytes(dataArray.toByteArray()); // Data Array
// </Data Array>
// <Block Light>
data.writeBytes(blockLight.toByteArray());
// </Block Light>
// <Sky Light>
data.writeBytes(skyLight.toByteArray());
// </Sky Light>
// </Chunk Section>
// <Biomes>
data.writeBytes(biomes.toByteArray());
// </Biomes>
}
netStream.writeVarInt(data.size()); // Size of Data
netStream.writeBytes(data.toByteArray()); // Data
netStream.writeVarInt(0); // Number of block entities
/* writeNBT */ /* writeNBT */
} }
} }