diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index fdd322054..d391d2e7b 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -133,8 +133,10 @@ public class DynamicChunk extends Chunk { if (connection instanceof NettyPlayerConnection) { final long lastChange = getLastChangeTime(); if (lastChange > cachedPacketTime || (cachedChunkBuffer == null || cachedLightBuffer == null)) { - this.cachedChunkBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createChunkPacket()); - this.cachedLightBuffer = PacketUtils.createFramedPacket(ByteBuffer.allocate(65000), createLightPacket()); + final var tempChunk = PacketUtils.createFramedPacket(createChunkPacket()); + this.cachedChunkBuffer = ByteBuffer.allocate(tempChunk.position()).put(tempChunk.flip()); + final var tempLight = PacketUtils.createFramedPacket(createLightPacket()); + this.cachedLightBuffer = ByteBuffer.allocate(tempLight.position()).put(tempLight.flip()); this.cachedPacketTime = lastChange; } NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) connection; 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 9be69b392..5ca363d0b 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -178,8 +178,7 @@ public class NettyPlayerConnection extends PlayerConnection { */ @Override public void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating) { - if (!channel.isConnected()) - return; + if (!channel.isConnected()) return; if (shouldSendPacket(serverPacket)) { final Player player = getPlayer(); if (player != null) { @@ -207,7 +206,6 @@ public class NettyPlayerConnection extends PlayerConnection { this.channel.write(tickBuffer.flip()); this.tickBuffer.clear().put(buffer); } catch (IOException ex) { - disconnect(); MinecraftServer.getExceptionManager().handleException(ex); } } @@ -229,7 +227,6 @@ public class NettyPlayerConnection extends PlayerConnection { this.tickBuffer.clear(); PacketUtils.writeFramedPacket(tickBuffer, packet, compressed); } catch (IOException ex) { - disconnect(); MinecraftServer.getExceptionManager().handleException(ex); } } @@ -255,7 +252,6 @@ public class NettyPlayerConnection extends PlayerConnection { this.channel.write(tickBuffer.flip()); this.tickBuffer.clear(); } catch (IOException e) { - disconnect(); MinecraftServer.getExceptionManager().handleException(e); } } diff --git a/src/main/java/net/minestom/server/network/socket/Worker.java b/src/main/java/net/minestom/server/network/socket/Worker.java index c4998af63..9764d2b00 100644 --- a/src/main/java/net/minestom/server/network/socket/Worker.java +++ b/src/main/java/net/minestom/server/network/socket/Worker.java @@ -85,11 +85,9 @@ public final class Worker { } public void disconnect(NettyPlayerConnection connection, SocketChannel channel) throws IOException { + if (connectionMap.remove(channel) == null) return; // Client close - connection.refreshOnline(false); channel.close(); - this.connectionMap.remove(channel); - // Remove the connection connection.refreshOnline(false); Player player = connection.getPlayer(); if (player != null) { diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index b45c03211..4ee1b9c71 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -102,7 +102,6 @@ public final class PacketUtils { connection.sendPacket(packet); } } - finalBuffer.clear(); // Clear packet to be reused } else { // Write the same packet for each individual players for (Player player : players) { @@ -179,6 +178,6 @@ public final class PacketUtils { } public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) { - return createFramedPacket(BUFFER.get(), packet); + return createFramedPacket(BUFFER.get().clear(), packet); } }