diff --git a/src/main/java/net/minestom/server/map/Framebuffer.java b/src/main/java/net/minestom/server/map/Framebuffer.java index d0ed8c058..ff163503e 100644 --- a/src/main/java/net/minestom/server/map/Framebuffer.java +++ b/src/main/java/net/minestom/server/map/Framebuffer.java @@ -34,9 +34,9 @@ public interface Framebuffer { } return new MapDataPacket(mapId, (byte) 0, false, false, List.of(), - (byte) width, (byte) height, - (byte) minX, (byte) minY, - colors); + new MapDataPacket.ColorContent((byte) width, (byte) height, + (byte) minX, (byte) minY, + colors)); } static int index(int x, int z) { diff --git a/src/main/java/net/minestom/server/map/LargeFramebuffer.java b/src/main/java/net/minestom/server/map/LargeFramebuffer.java index d9f3dca85..cd4f26f43 100644 --- a/src/main/java/net/minestom/server/map/LargeFramebuffer.java +++ b/src/main/java/net/minestom/server/map/LargeFramebuffer.java @@ -41,8 +41,8 @@ public interface LargeFramebuffer { } return new MapDataPacket(mapId, (byte) 0, false, false, List.of(), - (byte) width, (byte) height, - (byte) 0, (byte) 0, - colors); + new MapDataPacket.ColorContent((byte) width, (byte) height, + (byte) 0, (byte) 0, + colors)); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java index c9705de5a..cd26e340e 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java @@ -13,8 +13,7 @@ import java.util.List; public record MapDataPacket(int mapId, byte scale, boolean locked, boolean trackingPosition, @NotNull List icons, - byte columns, byte rows, byte x, byte z, - byte @Nullable [] data) implements ServerPacket { + @Nullable MapDataPacket.ColorContent colorContent) implements ServerPacket { public MapDataPacket { icons = List.copyOf(icons); } @@ -26,8 +25,7 @@ public record MapDataPacket(int mapId, byte scale, boolean locked, private MapDataPacket(MapDataPacket packet) { this(packet.mapId, packet.scale, packet.locked, packet.trackingPosition, packet.icons, - packet.columns, packet.rows, packet.x, packet.z, - packet.data); + packet.colorContent); } private static MapDataPacket read(BinaryReader reader) { @@ -38,19 +36,14 @@ public record MapDataPacket(int mapId, byte scale, boolean locked, List icons = trackingPosition ? reader.readVarIntList(Icon::new) : List.of(); var columns = reader.readByte(); - byte rows = 0; - byte x = 0; - byte z = 0; - byte[] data = null; - if (columns > 0) { - rows = reader.readByte(); - x = reader.readByte(); - z = reader.readByte(); - data = reader.readBytes(reader.readVarInt()); - } + if (columns <= 0) return new MapDataPacket(mapId, scale, locked, trackingPosition, icons, null); + byte rows = reader.readByte(); + byte x = reader.readByte(); + byte z = reader.readByte(); + byte[] data = reader.readBytes(reader.readVarInt()); return new MapDataPacket(mapId, scale, locked, - trackingPosition, icons, columns, rows, x, z, - data); + trackingPosition, icons, new ColorContent(columns, rows, x, z, + data)); } @Override @@ -60,17 +53,10 @@ public record MapDataPacket(int mapId, byte scale, boolean locked, writer.writeBoolean(locked); writer.writeBoolean(trackingPosition); if (trackingPosition) writer.writeVarIntList(icons, BinaryWriter::write); - - writer.writeByte(columns); - if (columns <= 0) return; - writer.writeByte(rows); - writer.writeByte(x); - writer.writeByte(z); - if (data != null && data.length > 0) { - writer.writeVarInt(data.length); - writer.writeBytes(data); + if (colorContent != null) { + writer.write(colorContent); } else { - writer.writeVarInt(0); + writer.writeByte((byte) 0); } } @@ -95,4 +81,20 @@ public record MapDataPacket(int mapId, byte scale, boolean locked, if (displayName != null) writer.writeComponent(displayName); } } + + public record ColorContent(byte columns, byte rows, byte x, byte z, + byte @NotNull [] data) implements Writeable { + public ColorContent(BinaryReader reader) { + this(reader.readByte(), reader.readByte(), reader.readByte(), reader.readByte(), + reader.readBytes(reader.readVarInt())); + } + + public void write(BinaryWriter writer) { + writer.writeByte(columns); + writer.writeByte(rows); + writer.writeByte(x); + writer.writeByte(z); + writer.writeByteArray(data); + } + } }