From dc6a520ab5acecf4708b8ee2e55b0a7767d2e321 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 10 Nov 2021 22:34:21 +0100 Subject: [PATCH] Add properties for grouped, cached & viewable packets (#524) --- .../net/minestom/server/MinecraftServer.java | 29 ------------------- .../server/network/packet/CachedPacket.java | 4 +++ .../minestom/server/utils/PacketUtils.java | 19 +++++++++++- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/src/main/java/net/minestom/server/MinecraftServer.java b/src/main/java/net/minestom/server/MinecraftServer.java index a6d9378fe..4f448d8ae 100644 --- a/src/main/java/net/minestom/server/MinecraftServer.java +++ b/src/main/java/net/minestom/server/MinecraftServer.java @@ -120,7 +120,6 @@ public final class MinecraftServer { private static int chunkViewDistance = Integer.getInteger("minestom.chunk-view-distance", 8); private static int entityViewDistance = Integer.getInteger("minestom.entity-view-distance", 5); private static int compressionThreshold = 256; - private static boolean groupedPacket = true; private static boolean terminalEnabled = System.getProperty("minestom.terminal.disabled") == null; private static ResponseDataConsumer responseDataConsumer; private static String brandName = "Minestom"; @@ -500,34 +499,6 @@ public final class MinecraftServer { MinecraftServer.compressionThreshold = compressionThreshold; } - /** - * Gets if the packet caching feature is enabled. - *

- * This features allow sending the exact same packet/buffer to multiple connections. - * It does provide a great performance benefit by allocating and writing/compressing only once. - *

- * It is enabled by default and it is our recommendation, - * you should only disable it if you want to modify packet per-players instead of sharing it. - * Disabling the feature would result in performance decrease. - * - * @return true if the grouped packet feature is enabled, false otherwise - */ - public static boolean hasGroupedPacket() { - return groupedPacket; - } - - /** - * Enables or disable grouped packet. - * - * @param groupedPacket true to enable grouped packet - * @throws IllegalStateException if this is called after the server started - * @see #hasGroupedPacket() - */ - public static void setGroupedPacket(boolean groupedPacket) { - Check.stateCondition(started, "You cannot change the grouped packet value after the server has been started."); - MinecraftServer.groupedPacket = groupedPacket; - } - /** * Gets if the built in Minestom terminal is enabled. * diff --git a/src/main/java/net/minestom/server/network/packet/CachedPacket.java b/src/main/java/net/minestom/server/network/packet/CachedPacket.java index d9fb145a5..4d02a6867 100644 --- a/src/main/java/net/minestom/server/network/packet/CachedPacket.java +++ b/src/main/java/net/minestom/server/network/packet/CachedPacket.java @@ -27,6 +27,10 @@ public final class CachedPacket { } public @NotNull FramedPacket retrieve() { + if (!PacketUtils.CACHED_PACKET) { + // TODO: Using a local buffer may be possible + return PacketUtils.allocateTrimmedPacket(supplier.get()); + } SoftReference ref; FramedPacket cache; if (updated == 0 || diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 31f3d7612..384d35152 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -47,6 +47,10 @@ public final class PacketUtils { private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager(); private static final LocalCache LOCAL_DEFLATER = LocalCache.of(Deflater::new); + public static final boolean GROUPED_PACKET = getBoolean("minestom.grouped-packet", true); + public static final boolean CACHED_PACKET = getBoolean("minestom.cached-packet", true); + public static final boolean VIEWABLE_PACKET = getBoolean("minestom.viewable-packet", true); + /// Local buffers private static final LocalCache PACKET_BUFFER = LocalCache.ofBuffer(Server.MAX_PACKET_SIZE); private static final LocalCache LOCAL_BUFFER = LocalCache.ofBuffer(Server.MAX_PACKET_SIZE); @@ -114,7 +118,7 @@ public final class PacketUtils { if (MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION && packet instanceof ComponentHoldingServerPacket) { needsTranslating = ComponentUtils.areAnyTranslatable(((ComponentHoldingServerPacket) packet).components()); } - if (MinecraftServer.hasGroupedPacket() && !needsTranslating) { + if (GROUPED_PACKET && !needsTranslating) { // Send grouped packet... if (!PACKET_LISTENER_MANAGER.processServerPacket(packet, players)) return; @@ -157,6 +161,10 @@ public final class PacketUtils { entity.sendPacketToViewers(serverPacket); return; } + if (!VIEWABLE_PACKET) { + sendGroupedPacket(viewable.getViewers(), serverPacket, value -> !Objects.equals(value, entity)); + return; + } final Player exception = entity instanceof Player ? (Player) entity : null; VIEWABLE_STORAGE_MAP.compute(viewable, (v, storage) -> { if (storage == null) storage = new ViewableStorage(); @@ -304,4 +312,13 @@ public final class PacketUtils { // TODO for non-socket connection } } + + private static boolean getBoolean(String name, boolean defaultValue) { + boolean result = defaultValue; + try { + result = Boolean.parseBoolean(System.getProperty(name)); + } catch (IllegalArgumentException | NullPointerException ignored) { + } + return result; + } }