Block serialization
This commit is contained in:
@@ -7,7 +7,7 @@ import mc.core.Location;
|
||||
public abstract class AbstractBlock implements Block {
|
||||
@Getter@Setter
|
||||
private Location location;
|
||||
@Getter@Setter
|
||||
@Getter
|
||||
private int meta;
|
||||
@Getter
|
||||
private final BlockType blockType;
|
||||
@@ -16,6 +16,11 @@ public abstract class AbstractBlock implements Block {
|
||||
this.blockType = type;
|
||||
}
|
||||
|
||||
protected AbstractBlock(BlockType type, int meta) {
|
||||
this.blockType = type;
|
||||
this.meta = meta;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return blockType.getId();
|
||||
|
||||
@@ -3,20 +3,20 @@ package mc.core.block;
|
||||
import mc.core.Location;
|
||||
|
||||
/**
|
||||
* Serialize info about block
|
||||
* Serialization block info
|
||||
*
|
||||
* +------------+--------+------------+
|
||||
* | param | range | bits |
|
||||
* +------------+--------+------------+
|
||||
* | id | 0-255 | 8 |
|
||||
* | id | 0:255 | 8 |
|
||||
* +------------+--------+------------+
|
||||
* | meta | 0-15 | 4 |
|
||||
* | meta | 0:15 | 4 |
|
||||
* +------------+--------+------------+
|
||||
* | x | 0-15 | 4 |
|
||||
* | x | 0:15 | 4 |
|
||||
* +------------+--------+------------+
|
||||
* | y | 0-15 | 4 |
|
||||
* | y | 0:15 | 4 |
|
||||
* +------------+--------+------------+
|
||||
* | z | 0-15 | 4 |
|
||||
* | z | 0:15 | 4 |
|
||||
* +------------+--------+------------+
|
||||
*
|
||||
* Total: 24 bits per block (3 bytes)
|
||||
|
||||
17
core/src/main/java/mc/core/block/BlockFactory.java
Normal file
17
core/src/main/java/mc/core/block/BlockFactory.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package mc.core.block;
|
||||
|
||||
public class BlockFactory {
|
||||
|
||||
public Block create(BlockType blockType, int meta) {
|
||||
return new EmbeddedBlock(blockType, meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* For first-time generation
|
||||
*/
|
||||
private class EmbeddedBlock extends AbstractBlock {
|
||||
EmbeddedBlock(BlockType type, int meta) {
|
||||
super(type, meta);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package mc.core.serialization;
|
||||
|
||||
import mc.core.block.Block;
|
||||
|
||||
public interface BlockDeserializer extends Deserializer<Block> {
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package mc.core.serialization;
|
||||
|
||||
import mc.core.block.Block;
|
||||
|
||||
public interface BlockSerializer extends Serializer<Block> {
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package mc.core.serialization;
|
||||
|
||||
public interface Deserializer<T> {
|
||||
T deserialize (byte[] bytes);
|
||||
}
|
||||
5
core/src/main/java/mc/core/serialization/Serializer.java
Normal file
5
core/src/main/java/mc/core/serialization/Serializer.java
Normal file
@@ -0,0 +1,5 @@
|
||||
package mc.core.serialization;
|
||||
|
||||
public interface Serializer<T> {
|
||||
byte[] serialize (T t);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package mc.world.generated_world;
|
||||
|
||||
import mc.core.block.Block;
|
||||
import mc.core.block.BlockFactory;
|
||||
import mc.core.block.BlockType;
|
||||
import mc.core.serialization.BlockDeserializer;
|
||||
import mc.core.serialization.BlockSerializer;
|
||||
import mc.core.world.Chunk;
|
||||
|
||||
/**
|
||||
* Prototype
|
||||
*/
|
||||
public class BlockSerializerDeserializer implements BlockSerializer, BlockDeserializer {
|
||||
|
||||
private BlockFactory blockFactory;
|
||||
private Chunk chunk;
|
||||
|
||||
public BlockSerializerDeserializer(BlockFactory blockFactory, Chunk chunk) {
|
||||
this.blockFactory = blockFactory;
|
||||
this.chunk = chunk;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block deserialize(byte[] bytes) {
|
||||
int id = bytes[0] + 128;
|
||||
int meta = bytes[1] >> 4;
|
||||
int x = (bytes[1] & 0xf) + chunk.getX() * 16;
|
||||
int y = bytes[2] >> 4 + chunk.getY() * 16;
|
||||
int z = (bytes[2] & 0xf) + chunk.getZ() * 16;
|
||||
BlockType type = BlockType.values()[id];
|
||||
Block block = blockFactory.create(type, meta);
|
||||
block.getLocation().setX(x);
|
||||
block.getLocation().setY(y);
|
||||
block.getLocation().setZ(z);
|
||||
return block;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Block block) {
|
||||
byte[] bytes = new byte[3];
|
||||
bytes[0] = (byte) ((block.getId() - 128) & 0xff);
|
||||
bytes[1] = (byte) ((block.getMeta() << 4) | (block.getLocation().getBlockX() % 16));
|
||||
bytes[2] = (byte) (((block.getLocation().getBlockZ() % 16) << 4) | (block.getLocation().getBlockZ() % 16));
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user