diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 72812c589..fdd322054 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -132,8 +132,7 @@ public class DynamicChunk extends Chunk { final PlayerConnection connection = player.getPlayerConnection(); if (connection instanceof NettyPlayerConnection) { final long lastChange = getLastChangeTime(); - if (lastChange > cachedPacketTime || - (cachedChunkBuffer == null || cachedLightBuffer == null)) { + if (lastChange > cachedPacketTime || (cachedChunkBuffer == null || cachedLightBuffer == null)) { this.cachedChunkBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createChunkPacket()); this.cachedLightBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createLightPacket()); this.cachedPacketTime = lastChange; diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index f5c117b22..9be69b392 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -188,7 +188,7 @@ public class NettyPlayerConnection extends PlayerConnection { serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component -> GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale))); } - attemptWrite(serverPacket); + write(serverPacket); } else { // Player is probably not logged yet writeAndFlush(serverPacket); @@ -196,22 +196,29 @@ public class NettyPlayerConnection extends PlayerConnection { } } - public void write(@NotNull FramedPacket framedPacket) { - attemptWrite(framedPacket.body()); - } - public void write(@NotNull ByteBuffer buffer) { - attemptWrite(buffer); - } - - public void writeAndFlush(@NotNull ServerPacket packet) { synchronized (tickBuffer) { - attemptWrite(packet); - flush(); + buffer.flip(); + if (tickBuffer.remaining() >= buffer.remaining()) { + // Enough buffer space + this.tickBuffer.put(buffer); + } else { + try { + this.channel.write(tickBuffer.flip()); + this.tickBuffer.clear().put(buffer); + } catch (IOException ex) { + disconnect(); + MinecraftServer.getExceptionManager().handleException(ex); + } + } } } - public void attemptWrite(ServerPacket packet) { + public void write(@NotNull FramedPacket framedPacket) { + write(framedPacket.body()); + } + + public void write(@NotNull ServerPacket packet) { synchronized (tickBuffer) { final int position = tickBuffer.position(); try { @@ -229,21 +236,10 @@ public class NettyPlayerConnection extends PlayerConnection { } } - public void attemptWrite(ByteBuffer buffer) { + public void writeAndFlush(@NotNull ServerPacket packet) { synchronized (tickBuffer) { - try { - this.tickBuffer.put(buffer.flip()); - } catch (BufferOverflowException e) { - try { - this.channel.write(tickBuffer.flip()); - this.channel.write(buffer); - } catch (IOException ex) { - disconnect(); - MinecraftServer.getExceptionManager().handleException(ex); - } finally { - this.tickBuffer.clear(); - } - } + write(packet); + flush(); } } @@ -257,10 +253,11 @@ public class NettyPlayerConnection extends PlayerConnection { if (tickBuffer.position() == 0) return; try { this.channel.write(tickBuffer.flip()); + this.tickBuffer.clear(); } catch (IOException e) { + disconnect(); MinecraftServer.getExceptionManager().handleException(e); } - this.tickBuffer.clear(); } }