diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 81ffa7ec7..ea67b3d57 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -130,8 +130,8 @@ public class DynamicChunk extends Chunk { final FramedPacket chunkPacket = chunkCache.retrieveFramedPacket(lastChange); if (connection instanceof PlayerSocketConnection) { PlayerSocketConnection socketConnection = (PlayerSocketConnection) connection; - socketConnection.write(lightPacket); - socketConnection.write(chunkPacket); + socketConnection.sendFramedPacket(lightPacket); + socketConnection.sendFramedPacket(chunkPacket); } else { connection.sendPacket(lightPacket.packet()); connection.sendPacket(chunkPacket.packet()); diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java index 4f1548a89..351e05933 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java @@ -8,7 +8,9 @@ import net.minestom.server.listener.manager.PacketListenerManager; import net.minestom.server.listener.manager.ServerPacketConsumer; import net.minestom.server.network.ConnectionManager; import net.minestom.server.network.ConnectionState; +import net.minestom.server.network.packet.FramedPacket; import net.minestom.server.network.packet.server.ServerPacket; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -100,6 +102,11 @@ public abstract class PlayerConnection { */ public abstract void sendPacket(@NotNull ServerPacket serverPacket, boolean skipTranslating); + @ApiStatus.Experimental + public void sendFramedPacket(@NotNull FramedPacket framedPacket) { + this.sendPacket(framedPacket.packet()); + } + /** * Flush waiting data to the connection. *
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 a7ef2b2f4..fb23992ae 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -210,7 +210,7 @@ public class PlayerSocketConnection extends PlayerConnection { serverPacket = ((ComponentHoldingServerPacket) serverPacket).copyWithOperator(component -> GlobalTranslator.render(component, Objects.requireNonNullElseGet(player.getLocale(), MinestomAdventure::getDefaultLocale))); } - write(serverPacket); + writePacket(serverPacket); } else { // Player is probably not logged yet writeAndFlush(serverPacket); @@ -218,6 +218,11 @@ public class PlayerSocketConnection extends PlayerConnection { } } + @Override + public void sendFramedPacket(@NotNull FramedPacket framedPacket) { + write(framedPacket.body().duplicate().position(0)); + } + @ApiStatus.Internal public void write(@NotNull ByteBuffer buffer) { synchronized (bufferLock) { @@ -245,17 +250,13 @@ public class PlayerSocketConnection extends PlayerConnection { } } - public void write(@NotNull FramedPacket framedPacket) { - write(framedPacket.body().duplicate().position(0)); - } - - public void write(@NotNull ServerPacket packet) { + private void writePacket(@NotNull ServerPacket packet) { write(PacketUtils.createFramedPacket(packet, compressed).flip()); } public void writeAndFlush(@NotNull ServerPacket packet) { synchronized (bufferLock) { - write(packet); + writePacket(packet); flush(); } } diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index c95a4b0a9..8b877cfbb 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -120,12 +120,7 @@ public final class PacketUtils { for (Player player : players) { if (!player.isOnline() || !playerValidator.isValid(player)) continue; - final PlayerConnection connection = player.getPlayerConnection(); - if (connection instanceof PlayerSocketConnection) { - ((PlayerSocketConnection) connection).write(framedPacket); - } else { - connection.sendPacket(packet); - } + player.getPlayerConnection().sendFramedPacket(framedPacket); } } else { // Write the same packet for each individual players @@ -218,12 +213,14 @@ public final class PacketUtils { Utils.writeVarIntHeader(buffer, uncompressedIndex, compressed ? packetSize : 0); } + @ApiStatus.Internal public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet, boolean compression) { ByteBuffer buffer = PACKET_BUFFER.get(); writeFramedPacket(buffer, packet, compression); return buffer; } + @ApiStatus.Internal public static ByteBuffer createFramedPacket(@NotNull ServerPacket packet) { return createFramedPacket(packet, MinecraftServer.getCompressionThreshold() > 0); }