нормальная сериализация чанка
This commit is contained in:
@@ -4,29 +4,23 @@
|
||||
*/
|
||||
package mc.core.world;
|
||||
|
||||
/* 16x256x16 */
|
||||
/* 16x16x16 */
|
||||
public interface Chunk {
|
||||
int getBlockType(int x, int y, int z);
|
||||
int[] getBlockTypeAsArray();
|
||||
void setBlockType(int x, int y, int z, int type);
|
||||
|
||||
int getBlockMetadata(int x, int y, int z);
|
||||
int[] getBlockMetadataAsArray();
|
||||
void setBlockMetadata(int x, int y, int z, int metadata);
|
||||
|
||||
int getBlockLight(int x, int y, int z);
|
||||
int[] getBlockLightAsArray();
|
||||
void setBlockLight(int x, int y, int z, int lightLevel);
|
||||
|
||||
int getSkyLight(int x, int y, int z);
|
||||
int[] getSkyLightAsArray();
|
||||
void setSkyLight(int x, int y, int z, int lightLevel);
|
||||
|
||||
int getAddition(int x, int y, int z);
|
||||
int[] getAdditionAsArray();
|
||||
void setAddition(int x, int y, int z, int value);
|
||||
|
||||
int getBiome(int x, int y, int z);
|
||||
int[] getBiomeAsArray();
|
||||
void setBiome(int x, int y, int z, int value);
|
||||
int getBiome(int x, int z);
|
||||
void setBiome(int x, int z, int value);
|
||||
}
|
||||
|
||||
@@ -6,43 +6,13 @@ package mc.world.flat;
|
||||
|
||||
import mc.core.world.Chunk;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class SimpleChunk implements Chunk {
|
||||
private int[] blocktype = new int[4096];
|
||||
private int[] blockmeta = new int[2048];
|
||||
private int[] blocklight = new int[2048];
|
||||
private int[] skylight = new int[2048];
|
||||
private int[] addition = new int[2048];
|
||||
private int[] biometype = new int[256];
|
||||
|
||||
SimpleChunk() {
|
||||
Arrays.fill(blocktype, 0, 256, 7);
|
||||
Arrays.fill(blocktype, 256, 768, 3);
|
||||
Arrays.fill(blocktype, 768, 1024, 2);
|
||||
Arrays.fill(blocktype, 1024, 4096, 0);
|
||||
|
||||
Arrays.fill(blockmeta, 0);
|
||||
|
||||
Arrays.fill(blocklight, 0);
|
||||
|
||||
Arrays.fill(skylight, 0, 512, 0);
|
||||
Arrays.fill(skylight, 512, 2048, -1);
|
||||
|
||||
Arrays.fill(addition, 0, 256, 1);
|
||||
Arrays.fill(addition, 256, 2048, 0);
|
||||
|
||||
Arrays.fill(biometype, 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBlockType(int x, int y, int z) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getBlockTypeAsArray() {
|
||||
return blocktype;
|
||||
if (y == 0) return 7;
|
||||
else if (y >= 1 && y <= 2) return 3;
|
||||
else if (y == 3) return 2;
|
||||
else return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,11 +25,6 @@ public class SimpleChunk implements Chunk {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getBlockMetadataAsArray() {
|
||||
return blockmeta;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlockMetadata(int x, int y, int z, int metadata) {
|
||||
|
||||
@@ -70,11 +35,6 @@ public class SimpleChunk implements Chunk {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getBlockLightAsArray() {
|
||||
return blocklight;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBlockLight(int x, int y, int z, int lightLevel) {
|
||||
|
||||
@@ -82,12 +42,8 @@ public class SimpleChunk implements Chunk {
|
||||
|
||||
@Override
|
||||
public int getSkyLight(int x, int y, int z) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getSkyLightAsArray() {
|
||||
return skylight;
|
||||
if (y <= 3) return 0;
|
||||
else return 15;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -100,28 +56,18 @@ public class SimpleChunk implements Chunk {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getAdditionAsArray() {
|
||||
return addition;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAddition(int x, int y, int z, int value) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBiome(int x, int y, int z) {
|
||||
public int getBiome(int x, int z) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[] getBiomeAsArray() {
|
||||
return biometype;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBiome(int x, int y, int z, int value) {
|
||||
public void setBiome(int x, int z, int value) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user