diff --git a/core/src/main/java/mc/core/world/Block.java b/core/src/main/java/mc/core/world/Block.java new file mode 100644 index 0000000..ba2865a --- /dev/null +++ b/core/src/main/java/mc/core/world/Block.java @@ -0,0 +1,15 @@ +/* + * DmitriyMX + * 2018-08-02 + */ +package mc.core.world; + +import mc.core.Location; + +public interface Block { + int getId(); + int getState(); + int getMetadata(); + int getLight(); + Location getLocation(); +} diff --git a/core/src/main/java/mc/core/world/Chunk.java b/core/src/main/java/mc/core/world/Chunk.java index 15ee0fa..db8f289 100644 --- a/core/src/main/java/mc/core/world/Chunk.java +++ b/core/src/main/java/mc/core/world/Chunk.java @@ -4,16 +4,15 @@ */ package mc.core.world; +import mc.core.Location; + /* 16x16x16 */ public interface Chunk { - int getBlockType(int x, int y, int z); - void setBlockType(int x, int y, int z, int type); - - int getBlockMetadata(int x, int y, int z); - void setBlockMetadata(int x, int y, int z, int metadata); - - int getBlockLight(int x, int y, int z); - void setBlockLight(int x, int y, int z, int lightLevel); + Block getBlock(int x, int y, int z); + default Block getBlock(Location location) { + return getBlock(location.getBlockX(), location.getBlockY(), location.getBlockZ()); + } + void setBlock(Block block); int getSkyLight(int x, int y, int z); void setSkyLight(int x, int y, int z, int lightLevel); diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/ByteArrayOutputNetStream.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/ByteArrayOutputNetStream.java index 15810b7..d496e05 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/ByteArrayOutputNetStream.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/ByteArrayOutputNetStream.java @@ -65,6 +65,10 @@ public class ByteArrayOutputNetStream extends NetOutputStream_p340 { writeLong(Double.doubleToLongBits(value)); } + public int size() { + return baos.size(); + } + public byte[] toByteArray() { return baos.toByteArray(); } diff --git a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/ChunkDataPacket.java b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/ChunkDataPacket.java index c05c479..f1e9f98 100644 --- a/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/ChunkDataPacket.java +++ b/proto_1.12.2/src/main/java/mc/core/network/proto_1_12_2/packets/ChunkDataPacket.java @@ -10,6 +10,7 @@ import lombok.Setter; import lombok.extern.slf4j.Slf4j; import mc.core.network.NetOutputStream; import mc.core.network.SCPacket; +import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream; import mc.core.world.Chunk; import java.io.ByteArrayOutputStream; @@ -17,6 +18,55 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; +/* +Packet structure + +- https://wiki.vg/Chunk_Format#Packet_structure + ++------------------------------------------------------+ +| Field | Type | +|--------------------------|---------------------------| +| Chunk X | int | +|--------------------------|---------------------------| +| Chunk Y | int | +|--------------------------|---------------------------| +| Init Chunk | boolean | ("Ground-Up Continuous") +|--------------------------|---------------------------| +| Primary Bit Mask | VarInt | +|--------------------------|---------------------------| +| Size of Data | VarInt | +|--------------------------|---------------------------| +| Data | Byte array | - https://wiki.vg/Chunk_Format#Data_structure +| +------------------------------------------------+ | +| | Chunk Section | Byte array | | - https://wiki.vg/Chunk_Format#Chunk_Section_structure +| | +------------------------------------------+ | | +| | | Bits Per Block | Unsigned Byte | | | +| | |--------------------|---------------------| | | +| | | Palette | Byte array | | | - https://wiki.vg/Chunk_Format#Palettes +| | | +------------------------------------+ | | | (we use Indirect type palette) +| | | | Size of palette | VarInt | | | | +| | | |-----------------|------------------| | | | +| | | | Palette | Array of VarInt | | | | +| | | +------------------------------------+ | | | +| | |--------------------|---------------------| | | +| | | Size of Data Array | VarInt | | | +| | |--------------------|---------------------| | | +| | | Data Array | Array of Long | | | +| | |--------------------|---------------------| | | +| | | Block Light | Byte Array | | | +| | |--------------------|---------------------| | | +| | | Sky Light | Optional Byte Array | | | +| | +------------------------------------------+ | | +| |-----------------------|------------------------| | +| | Biomes | Optional Byte array | | +| +------------------------------------------------+ | +|--------------------------|---------------------------| +| Number of block entities | VarInt | +|--------------------------|---------------------------| +| Block entities | Array of NBT | ++------------------------------------------------------+ + */ + @Slf4j @NoArgsConstructor public class ChunkDataPacket implements SCPacket { @@ -36,19 +86,14 @@ public class ChunkDataPacket implements SCPacket { netStream.writeBoolean(initChunk); netStream.writeVarInt(0b11111111); // Primary Bit Mask - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - try { - for (Chunk chunk : chunks) { - netStream.writeByte(4); // Bits Per Block - // - // - // - // - // -// baos.write(ChunkSerializer.serializeBiomes(chunk)); - } - } catch (IOException e) { - log.error("Error serialize chunk", e); // what? is it possible?? + ByteArrayOutputNetStream baos = new ByteArrayOutputNetStream(); + for (Chunk chunk : chunks) { + // + // + // + // + // + // } netStream.writeVarInt(baos.size()); // Size of Data in bytes netStream.writeBytes(baos.toByteArray()); // Data chunks