From 27414f65abdcfa9d51ae766838c817ab00a08468 Mon Sep 17 00:00:00 2001 From: themode Date: Sat, 21 Nov 2020 10:26:25 +0100 Subject: [PATCH] Made packet caching optional (enabled by default) --- .../net/minestom/server/MinecraftServer.java | 28 +++++++++++++++++++ .../network/player/NettyPlayerConnection.java | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index 03f536c03..7e9b54f61 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -128,6 +128,7 @@ public final class MinecraftServer { private static int chunkViewDistance = 8; private static int entityViewDistance = 5; private static int compressionThreshold = 256; + private static boolean packetCaching = true; private static ResponseDataConsumer responseDataConsumer; private static String brandName = "Minestom"; private static Difficulty difficulty = Difficulty.NORMAL; @@ -520,6 +521,33 @@ public final class MinecraftServer { MinecraftServer.compressionThreshold = compressionThreshold; } + /** + * Gets if the packet caching feature is enabled. + *

+ * This feature allows some packets (implementing the {@link net.minestom.server.utils.cache.CacheablePacket} to be cached + * in order to do not have to be written and compressed over and over gain), this is especially useful for chunk and light packets. + *

+ * It is enabled by default and it is our recommendation, you should only disable it if you want to focus on low memory usage + * at the cost of many packet writing and compression. + * + * @return true if the packet caching feature is enabled, false otherwise + */ + public static boolean hasPacketCaching() { + return packetCaching; + } + + /** + * Enables or disable packet caching. + * + * @param packetCaching true to enable packet caching + * @throws IllegalStateException if this is called after the server started + * @see #hasPacketCaching() + */ + public static void setPacketCaching(boolean packetCaching) { + Check.stateCondition(started, "You cannot change the packet caching value after the server has been started."); + MinecraftServer.packetCaching = packetCaching; + } + /** * Gets the consumer executed to show server-list data. * 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 13f28e391..c2e22cc0a 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -114,7 +114,7 @@ public class NettyPlayerConnection extends PlayerConnection { if (shouldSendPacket(serverPacket)) { if (getPlayer() != null) { // Flush happen during #update() - if (serverPacket instanceof CacheablePacket) { + if (serverPacket instanceof CacheablePacket && MinecraftServer.hasPacketCaching()) { CacheablePacket cacheablePacket = (CacheablePacket) serverPacket; final UUID identifier = cacheablePacket.getIdentifier();