Archived
0

нормальная сериализация чанка

This commit is contained in:
2018-04-28 20:42:44 +03:00
parent a55e340cfe
commit ed22229bd2
3 changed files with 90 additions and 78 deletions

View File

@@ -18,7 +18,13 @@ import java.util.zip.Deflater;
@Setter
@ToString
public class ChunkDataPacket implements SCPacket {
private static final int dataSize = 4096 + 2048 + 2048 + 2048 + 2048 + 256;
private static final int blocktypeSize = 4096,
metadataSize = 2048,
blocklightSize = 2048,
skylightSize = 2048,
additionSize = 2048,
biomeSize = 256;
private static final int dataSize = blocktypeSize+metadataSize+blocklightSize+skylightSize+additionSize+biomeSize;
private int x, z;
private boolean needInitChunk;
@@ -28,12 +34,78 @@ public class ChunkDataPacket implements SCPacket {
public void setChunk(Chunk chunk) {
ByteBuffer chunkData = ByteBuffer.allocate(dataSize);
Arrays.stream(chunk.getBlockTypeAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getBlockMetadataAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getBlockLightAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getSkyLightAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getAdditionAsArray()).forEach(i -> chunkData.put((byte) i));
Arrays.stream(chunk.getBiomeAsArray()).forEach(i -> chunkData.put((byte) i));
/*
* 0 - blocktype
* 1 - metadata
* 2 - blocklight
* 3 - skylight
* 4 - addition
* 5 - biome
*/
int[] idx = new int[6];
for (int y = 0; y < 16; y++) {
for (int z = 0; z < 16; z++) {
for (int x = 0; x < 16; x++) {
// Block type
int offset = 0;
chunkData.put((idx[0]++), (byte) chunk.getBlockType(x, y, z));
// Block metadata
offset = offset+blocktypeSize;
if ((idx[1] % 2) > 0) {
int i = (int) ((((idx[1]++) + 1) / 2d) - 1d);
byte b = chunkData.get(offset+i);
b = (byte)((b << 4) | (byte)chunk.getBlockMetadata(x, y, z));
chunkData.put(offset+i, b);
} else {
int i = (int) ((((idx[1]++) + 1) / 2d) - .5d);
chunkData.put(offset+i, (byte) chunk.getBlockMetadata(x, y, z));
}
// Block light
offset = offset+metadataSize;
if ((idx[2] % 2) > 0) {
int i = (int) ((((idx[2]++) + 1) / 2d) - 1d);
byte b = chunkData.get(offset+i);
b = (byte)((b << 4) | (byte)chunk.getBlockLight(x, y, z));
chunkData.put(offset+i, b);
} else {
int i = (int) ((((idx[2]++) + 1) / 2d) - .5d);
chunkData.put(offset+i, (byte) chunk.getBlockLight(x, y, z));
}
// Sky light
offset = offset+blocklightSize;
if ((idx[3] % 2) > 0) {
int i = (int) ((((idx[3]++) + 1) / 2d) - 1d);
byte b = chunkData.get(offset+i);
b = (byte)((b << 4) | (byte)chunk.getSkyLight(x, y, z));
chunkData.put(offset+i, b);
} else {
int i = (int) ((((idx[3]++) + 1) / 2d) - .5d);
chunkData.put(offset+i, (byte) chunk.getSkyLight(x, y, z));
}
// Addition
offset = offset+skylightSize;
if ((idx[4] % 2) > 0) {
int i = (int) ((((idx[4]++) + 1) / 2d) - 1d);
byte b = chunkData.get(offset+i);
b = (byte)((b << 4) | (byte)chunk.getAddition(x, y, z));
chunkData.put(offset+i, b);
} else {
int i = (int) ((((idx[4]++) + 1) / 2d) - .5d);
chunkData.put(offset+i, (byte) chunk.getAddition(x, y, z));
}
// Biome
if (idx[5] == 256) continue;
offset = offset+additionSize;
chunkData.put(offset+(idx[5]++), (byte) chunk.getBiome(x, z));
}
}
}
Deflater zlib = new Deflater(Deflater.DEFAULT_COMPRESSION);
zlib.setInput(chunkData.array());