Skip to content

Commit

Permalink
Fix SubChunkPacket serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
SupremeMortal committed Dec 23, 2021
1 parent 9e1991c commit a90c6db
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@

@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class SubChunkSerializer_v471 implements BedrockPacketSerializer<SubChunkPacket> {

public static final SubChunkSerializer_v471 INSTANCE = new SubChunkSerializer_v471();

protected static final int HEIGHT_MAP_LENGTH = 256;

@Override
public void serialize(ByteBuf buffer, BedrockPacketHelper helper, SubChunkPacket packet) {
VarInts.writeInt(buffer, packet.getDimension());
helper.writeVector3i(buffer, packet.getSubChunkPosition());
helper.writeByteArray(buffer, packet.getData());
VarInts.writeInt(buffer, packet.getResult().ordinal());
buffer.writeByte(packet.getHeightMapType().ordinal());
buffer.writeBytes(packet.getHeightMapData());
buffer.writeBytes(packet.getHeightMapData(), 0, HEIGHT_MAP_LENGTH);
}

@Override
Expand All @@ -32,9 +35,8 @@ public void deserialize(ByteBuf buffer, BedrockPacketHelper helper, SubChunkPack
packet.setResult(SubChunkRequestResult.values()[VarInts.readInt(buffer)]);
packet.setHeightMapType(HeightMapDataType.values()[buffer.readByte()]);

ByteBuf heightMapBuffer = buffer.readBytes(buffer.readableBytes());
byte[] heightMap = new byte[heightMapBuffer.readableBytes()];
heightMapBuffer.readBytes(heightMap);
byte[] heightMap = new byte[HEIGHT_MAP_LENGTH];
buffer.readBytes(heightMap);

packet.setHeightMapData(heightMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void serialize(ByteBuf buffer, BedrockPacketHelper helper, SubChunkPacket
buffer.writeByte(packet.getHeightMapType().ordinal());
if (packet.getHeightMapType() == HeightMapDataType.HAS_DATA) {
byte[] heightMapBuf = packet.getHeightMapData();
buffer.writeBytes(heightMapBuf);
buffer.writeBytes(heightMapBuf, 0, HEIGHT_MAP_LENGTH);
}
buffer.writeBoolean(packet.isCacheEnabled());
if (packet.isCacheEnabled()) {
Expand All @@ -37,9 +37,8 @@ public void deserialize(ByteBuf buffer, BedrockPacketHelper helper, SubChunkPack
packet.setHeightMapType(HeightMapDataType.values()[buffer.readByte()]);

if (packet.getHeightMapType() == HeightMapDataType.HAS_DATA) {
ByteBuf heightMapBuffer = buffer.readBytes(buffer.readableBytes());
byte[] heightMap = new byte[heightMapBuffer.readableBytes()];
heightMapBuffer.readBytes(heightMap);
byte[] heightMap = new byte[HEIGHT_MAP_LENGTH];
buffer.readBytes(heightMap);
}
packet.setCacheEnabled(buffer.readBoolean());
if (packet.isCacheEnabled()) {
Expand Down

0 comments on commit a90c6db

Please sign in to comment.