From 1ceffd854026dd3d0323891524855f0870cad77d Mon Sep 17 00:00:00 2001 From: themode Date: Fri, 26 Mar 2021 11:09:30 +0100 Subject: [PATCH] Do not write empty buffer --- .../network/player/NettyPlayerConnection.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) 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 e3c0d28d2..ebe1d9981 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -71,12 +71,15 @@ public class NettyPlayerConnection extends PlayerConnection { @Override public void update() { // Flush - this.channel.eventLoop().submit(() -> { - if (channel.isActive()) { - writeWaitingPackets(); - this.channel.flush(); - } - }); + final int bufferSize = tickBuffer.writerIndex(); + if (bufferSize > 0) { + this.channel.eventLoop().submit(() -> { + if (channel.isActive()) { + writeWaitingPackets(); + channel.flush(); + } + }); + } // Network stats super.update(); } @@ -208,6 +211,11 @@ public class NettyPlayerConnection extends PlayerConnection { private void writeWaitingPackets() { synchronized (tickBuffer) { final ByteBuf copy = tickBuffer.copy(); + if (copy.writerIndex() == 0) { + // Nothing to write + copy.release(); + return; + } ChannelFuture channelFuture = channel.write(new FramedPacket(copy)); channelFuture.addListener(future -> copy.release());