Added option to disable the grouped packet feature

This commit is contained in:
themode 2021-01-02 15:44:50 +01:00
parent 1c916b92fa
commit 0faaea2c1b
2 changed files with 54 additions and 11 deletions

View File

@ -133,6 +133,7 @@ public final class MinecraftServer {
private static int entityViewDistance = 5;
private static int compressionThreshold = 256;
private static boolean packetCaching = true;
private static boolean groupedPacket = true;
private static ResponseDataConsumer responseDataConsumer;
private static String brandName = "Minestom";
private static Difficulty difficulty = Difficulty.NORMAL;
@ -571,6 +572,34 @@ public final class MinecraftServer {
MinecraftServer.packetCaching = packetCaching;
}
/**
* 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 the consumer executed to show server-list data.
*

View File

@ -47,12 +47,31 @@ public final class PacketUtils {
if (players.isEmpty())
return;
final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players);
if (success) {
final ByteBuf finalBuffer = createFramedPacket(packet, false);
final FramedPacket framedPacket = new FramedPacket(finalBuffer);
if (MinecraftServer.hasGroupedPacket()) {
// Send grouped packet...
final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players);
if (success) {
final ByteBuf finalBuffer = createFramedPacket(packet, false);
final FramedPacket framedPacket = new FramedPacket(finalBuffer);
// Send packet to all players
// Send packet to all players
for (Player player : players) {
// Verify if the player should receive the packet
if (playerValidator != null && !playerValidator.isValid(player))
continue;
final PlayerConnection playerConnection = player.getPlayerConnection();
if (playerConnection instanceof NettyPlayerConnection) {
final NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) playerConnection;
nettyPlayerConnection.write(framedPacket);
} else {
playerConnection.sendPacket(packet);
}
}
}
} else {
// Write the same packet for each individual players
for (Player player : players) {
// Verify if the player should receive the packet
@ -60,12 +79,7 @@ public final class PacketUtils {
continue;
final PlayerConnection playerConnection = player.getPlayerConnection();
if (playerConnection instanceof NettyPlayerConnection) {
final NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) playerConnection;
nettyPlayerConnection.write(framedPacket);
} else {
playerConnection.sendPacket(packet);
}
playerConnection.sendPacket(packet);
}
}
}