From 8f1e84ffdb8826c2c8c112dd9f8647098d2cd584 Mon Sep 17 00:00:00 2001 From: TheMode Date: Tue, 7 Sep 2021 01:24:51 +0200 Subject: [PATCH] Prevent framed packets from being corrupted Signed-off-by: TheMode --- .../server/network/player/PlayerSocketConnection.java | 2 +- src/main/java/net/minestom/server/utils/PacketUtils.java | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java index 8ace2acc7..6737ada68 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -252,7 +252,7 @@ public class PlayerSocketConnection extends PlayerConnection { } public void write(@NotNull FramedPacket framedPacket) { - write(framedPacket.body().flip()); + write(framedPacket.body().position(0)); } public void write(@NotNull ServerPacket packet) { diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 83b0e2965..0cae41f01 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -109,7 +109,7 @@ public final class PacketUtils { // Send grouped packet... if (!PACKET_LISTENER_MANAGER.processServerPacket(packet, players)) return; - final ByteBuffer finalBuffer = createFramedPacket(packet); + final ByteBuffer finalBuffer = createFramedPacket(packet).flip(); final FramedPacket framedPacket = new FramedPacket(packet.getId(), finalBuffer, packet); // Send packet to all players for (Player player : players) { @@ -228,7 +228,8 @@ public final class PacketUtils { @ApiStatus.Internal public static FramedPacket allocateTrimmedPacket(@NotNull ServerPacket packet) { final ByteBuffer temp = PacketUtils.createFramedPacket(packet).flip(); - final ByteBuffer buffer = ByteBuffer.allocateDirect(temp.remaining()).put(temp).asReadOnlyBuffer(); + final ByteBuffer buffer = ByteBuffer.allocateDirect(temp.remaining()) + .put(temp).flip().asReadOnlyBuffer(); return new FramedPacket(packet.getId(), buffer, packet); }