Add properties for grouped, cached & viewable packets (#524)

This commit is contained in:
TheMode 2021-11-10 22:34:21 +01:00 committed by GitHub
parent e75b9f238d
commit dc6a520ab5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 30 deletions

View File

@ -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.
* <p>
* 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.
* <p>
* 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.
*

View File

@ -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<FramedPacket> ref;
FramedPacket cache;
if (updated == 0 ||

View File

@ -47,6 +47,10 @@ public final class PacketUtils {
private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager();
private static final LocalCache<Deflater> 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<ByteBuffer> PACKET_BUFFER = LocalCache.ofBuffer(Server.MAX_PACKET_SIZE);
private static final LocalCache<ByteBuffer> 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;
}
}