diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 08b2fcb8b..fb21f6f24 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -132,8 +132,8 @@ public class DynamicChunk extends Chunk { final FramedPacket chunkPacket = chunkCache.retrieveFramedPacket(lastChange); if (connection instanceof PlayerSocketConnection) { PlayerSocketConnection socketConnection = (PlayerSocketConnection) connection; - socketConnection.write(lightPacket.body()); - socketConnection.write(chunkPacket.body()); + socketConnection.write(lightPacket); + socketConnection.write(chunkPacket); } else { connection.sendPacket(lightPacket.packet()); connection.sendPacket(chunkPacket.packet()); 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 b03f4d502..a2c1caf8e 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -230,15 +230,14 @@ public class PlayerSocketConnection extends PlayerConnection { } } + @ApiStatus.Internal public void write(@NotNull ByteBuffer buffer) { synchronized (bufferLock) { - final int size = buffer.position(); + final int size = buffer.remaining(); if (size <= BUFFER_SIZE) { if (!tickBuffer.canWrite(size)) flush(); - this.tickBuffer.write(buffer.flip()); + this.tickBuffer.write(buffer); } else { - final int positionCache = buffer.position(); - final int limitCache = buffer.limit(); final int bufferCount = size / BUFFER_SIZE + 1; for (int i = 0; i < bufferCount; i++) { buffer.position(i * BUFFER_SIZE); @@ -246,17 +245,16 @@ public class PlayerSocketConnection extends PlayerConnection { if (!tickBuffer.canWrite(buffer.remaining())) flush(); this.tickBuffer.write(buffer); } - buffer.position(positionCache).limit(limitCache); } } } public void write(@NotNull FramedPacket framedPacket) { - write(framedPacket.body()); + write(framedPacket.body().flip()); } public void write(@NotNull ServerPacket packet) { - write(PacketUtils.createFramedPacket(packet, compressed)); + write(PacketUtils.createFramedPacket(packet, compressed).flip()); } public void writeAndFlush(@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 72aeae50e..42763a0d0 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -308,7 +308,6 @@ public final class PacketUtils { final int start = pair.leftInt(); if (start != lastWrite) { ByteBuffer slice = buffer.asByteBuffer(lastWrite, start); - slice.position(slice.limit()); writer.accept(slice); } lastWrite = pair.rightInt(); @@ -318,7 +317,6 @@ public final class PacketUtils { final int remaining = buffer.writerOffset() - lastWrite; if (remaining > 0) { ByteBuffer remainSlice = buffer.asByteBuffer(lastWrite, remaining); - remainSlice.position(remainSlice.limit()); writer.accept(remainSlice); } }