Archived
0

Merge remote-tracking branch 'origin/world' into proto-125

# Conflicts:
#	proto125_netty/src/main/java/mc/core/network/proto_125/netty/PacketHandler.java
This commit is contained in:
2018-05-01 22:53:37 +03:00
8 changed files with 219 additions and 46 deletions

View File

@@ -8,55 +8,109 @@ import lombok.Setter;
import lombok.ToString;
import mc.core.network.SCPacket;
import mc.core.network.proto_125.ByteArrayOutputNetStream;
import mc.core.world.Chunk;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;
import java.util.zip.Deflater;
@Setter
@ToString
public class ChunkDataPacket implements SCPacket {
private static byte[] compressData;
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;
private int yMin,yMax;
private byte[] compressData;
static {
byte[] blocktype = new byte[4096];
Arrays.fill(blocktype, 0, 256, (byte)7);
Arrays.fill(blocktype, 256, 768, (byte)3);
Arrays.fill(blocktype, 768, 1024, (byte)2);
Arrays.fill(blocktype, 1024, 4096, (byte)0);
public void setChunk(Chunk chunk) {
ByteBuffer chunkData = ByteBuffer.allocate(dataSize);
byte[] blockmeta = new byte[2048];
Arrays.fill(blockmeta, (byte)0);
/*
* 0 - blocktype
* 1 - metadata
* 2 - blocklight
* 3 - skylight
* 4 - addition
* 5 - biome
*/
int[] idx = new int[6];
byte[] blocklight = new byte[2048];
Arrays.fill(blocklight, (byte)0);
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));
byte[] skylight = new byte[2048];
Arrays.fill(skylight, 0, 512, (byte) 0);
Arrays.fill(skylight, 512, 2048, (byte)-1);
// 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)((chunk.getBlockMetadata(x, y, z) << 4) | b);
chunkData.put(offset+i, b);
} else {
int i = (int) ((((idx[1]++) + 1) / 2d) - .5d);
chunkData.put(offset+i, (byte) chunk.getBlockMetadata(x, y, z));
}
byte[] addition = new byte[2048];
Arrays.fill(addition, 0, 256, (byte)1);
Arrays.fill(addition, 256, 2048, (byte)0);
// 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));
}
byte[] biometype = new byte[256];
Arrays.fill(biometype, (byte)0);
// 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));
}
byte[] chunkData = new byte[blocktype.length + blockmeta.length + blocklight.length + skylight.length + addition.length + biometype.length];
System.arraycopy(blocktype, 0, chunkData, 0, blocktype.length);
System.arraycopy(blockmeta, 0, chunkData, blocktype.length, blockmeta.length);
System.arraycopy(blocklight, 0, chunkData, blockmeta.length, blocklight.length);
System.arraycopy(skylight, 0, chunkData, blocklight.length, skylight.length);
System.arraycopy(addition, 0, chunkData, skylight.length, addition.length);
System.arraycopy(biometype, 0, chunkData, addition.length, biometype.length);
// 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);
zlib.setInput(chunkData.array());
zlib.finish();
byte[] preCompileData = new byte[chunkData.length];
byte[] preCompileData = new byte[dataSize];
int compressSize = zlib.deflate(preCompileData);
compressData = new byte[compressSize];