Minestom/src/main/java/net/minestom/server/utils/PacketUtils.java

200 lines
9.3 KiB
Java
Raw Normal View History

2020-04-24 03:25:58 +02:00
package net.minestom.server.utils;
2019-08-22 14:52:32 +02:00
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.minestom.server.MinecraftServer;
2021-06-11 17:19:11 +02:00
import net.minestom.server.adventure.MinestomAdventure;
import net.minestom.server.adventure.audience.PacketGroupingAudience;
import net.minestom.server.entity.Player;
import net.minestom.server.listener.manager.PacketListenerManager;
import net.minestom.server.network.netty.packet.FramedPacket;
2021-03-12 16:33:19 +01:00
import net.minestom.server.network.packet.server.ComponentHoldingServerPacket;
2020-04-24 03:25:58 +02:00
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.player.NettyPlayerConnection;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.binary.BinaryWriter;
import net.minestom.server.utils.callback.validator.PlayerValidator;
2020-11-05 22:20:51 +01:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
2019-08-22 14:52:32 +02:00
2021-06-20 20:59:53 +02:00
import java.nio.ByteBuffer;
import java.util.Collection;
2021-06-20 20:59:53 +02:00
import java.util.zip.Deflater;
2020-08-15 04:05:15 +02:00
/**
* Utils class for packets. Including writing a {@link ServerPacket} into a {@link ByteBuffer}
2020-11-13 09:17:53 +01:00
* for network processing.
2020-08-15 04:05:15 +02:00
*/
public final class PacketUtils {
private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager();
2021-06-20 20:59:53 +02:00
private static final ThreadLocal<Deflater> COMPRESSOR = ThreadLocal.withInitial(Deflater::new);
private PacketUtils() {
}
2019-08-22 14:52:32 +02:00
/**
* Sends a packet to an audience. This method performs the following steps in the
* following order:
* <ol>
* <li>If {@code audience} is a {@link Player}, send the packet to them.</li>
* <li>Otherwise, if {@code audience} is a {@link PacketGroupingAudience}, call
* {@link #sendGroupedPacket(Collection, ServerPacket)} on the players that the
* grouping audience contains.</li>
* <li>Otherwise, if {@code audience} is a {@link ForwardingAudience.Single},
* call this method on the single audience inside the forwarding audience.</li>
* <li>Otherwise, if {@code audience} is a {@link ForwardingAudience}, call this
* method for each audience member of the forwarding audience.</li>
* <li>Otherwise, do nothing.</li>
* </ol>
*
* @param audience the audience
* @param packet the packet
*/
@SuppressWarnings("OverrideOnly") // we need to access the audiences inside ForwardingAudience
public static void sendPacket(@NotNull Audience audience, @NotNull ServerPacket packet) {
if (audience instanceof Player) {
((Player) audience).getPlayerConnection().sendPacket(packet);
} else if (audience instanceof PacketGroupingAudience) {
PacketUtils.sendGroupedPacket(((PacketGroupingAudience) audience).getPlayers(), packet);
} else if (audience instanceof ForwardingAudience.Single) {
PacketUtils.sendPacket(((ForwardingAudience.Single) audience).audience(), packet);
} else if (audience instanceof ForwardingAudience) {
for (Audience member : ((ForwardingAudience) audience).audiences()) {
PacketUtils.sendPacket(member, packet);
}
}
}
2020-11-13 09:17:53 +01:00
/**
* Sends a {@link ServerPacket} to multiple players.
2020-11-13 09:17:53 +01:00
* <p>
* Can drastically improve performance since the packet will not have to be processed as much.
2020-11-13 09:17:53 +01:00
*
* @param players the players to send the packet to
* @param packet the packet to send to the players
* @param playerValidator optional callback to check if a specify player of {@code players} should receive the packet
2020-11-13 09:17:53 +01:00
*/
public static void sendGroupedPacket(@NotNull Collection<Player> players, @NotNull ServerPacket packet,
@Nullable PlayerValidator playerValidator) {
2020-11-20 14:14:55 +01:00
if (players.isEmpty())
return;
2021-03-12 16:33:19 +01:00
// work out if the packet needs to be sent individually due to server-side translating
boolean needsTranslating = false;
2021-06-11 17:19:11 +02:00
if (MinestomAdventure.AUTOMATIC_COMPONENT_TRANSLATION && packet instanceof ComponentHoldingServerPacket) {
needsTranslating = ComponentUtils.areAnyTranslatable(((ComponentHoldingServerPacket) packet).components());
2021-03-12 16:33:19 +01:00
}
if (MinecraftServer.hasGroupedPacket() && !needsTranslating) {
// Send grouped packet...
final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players);
if (success) {
final ByteBuffer finalBuffer = createFramedPacket(packet);
final FramedPacket framedPacket = new FramedPacket(finalBuffer);
// Send packet to all players
for (Player player : players) {
if (!player.isOnline())
continue;
// 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
if (playerValidator != null && !playerValidator.isValid(player))
continue;
final PlayerConnection playerConnection = player.getPlayerConnection();
2021-03-15 15:19:43 +01:00
playerConnection.sendPacket(packet, false);
}
}
}
/**
* Same as {@link #sendGroupedPacket(Collection, ServerPacket, PlayerValidator)}
* but with the player validator sets to null.
*
* @see #sendGroupedPacket(Collection, ServerPacket, PlayerValidator)
*/
public static void sendGroupedPacket(@NotNull Collection<Player> players, @NotNull ServerPacket packet) {
sendGroupedPacket(players, packet, null);
}
2020-08-15 04:05:15 +02:00
/**
* Writes a {@link ServerPacket} into a {@link ByteBuffer}.
2020-08-15 04:05:15 +02:00
*
* @param buf the recipient of {@code packet}
* @param packet the packet to write into {@code buf}
*/
public static void writePacket(@NotNull ByteBuffer buf, @NotNull ServerPacket packet) {
Utils.writeVarInt(buf, packet.getId());
BinaryWriter writer = new BinaryWriter(buf);
2020-11-19 02:28:56 +01:00
try {
packet.write(writer);
} catch (Exception e) {
MinecraftServer.getExceptionManager().handleException(e);
2020-11-19 02:28:56 +01:00
}
}
public static void writeFramedPacket(@NotNull ByteBuffer buffer,
@NotNull ServerPacket serverPacket) {
2021-03-22 14:31:38 +01:00
final int compressionThreshold = MinecraftServer.getCompressionThreshold();
2021-05-10 05:47:14 +02:00
// Index of the var-int containing the complete packet length
final int packetLengthIndex = Utils.writeEmptyVarIntHeader(buffer);
final int startIndex = buffer.position(); // Index where the content starts (after length)
2021-05-10 11:02:12 +02:00
if (compressionThreshold > 0) {
2021-05-10 05:47:14 +02:00
// Index of the uncompressed payload length
final int dataLengthIndex = Utils.writeEmptyVarIntHeader(buffer);
// Write packet
final int contentIndex = buffer.position();
writePacket(buffer, serverPacket);
final int packetSize = buffer.position() - contentIndex;
2021-05-10 11:02:12 +02:00
final int uncompressedLength = packetSize >= compressionThreshold ? packetSize : 0;
Utils.writeVarIntHeader(buffer, dataLengthIndex, uncompressedLength);
2021-05-10 11:02:12 +02:00
if (uncompressedLength > 0) {
2021-05-10 07:00:16 +02:00
// Packet large enough, compress
ByteBuffer uncompressedCopy = buffer.duplicate().position(contentIndex).limit(contentIndex + packetSize);
buffer.position(contentIndex);
var deflater = COMPRESSOR.get();
deflater.setInput(uncompressedCopy);
deflater.finish();
deflater.deflate(buffer);
deflater.reset();
2021-03-26 13:08:05 +01:00
}
} else {
2021-05-10 05:47:14 +02:00
// No compression, write packet id + payload
writePacket(buffer, serverPacket);
2021-03-26 13:08:05 +01:00
}
2021-05-10 11:02:12 +02:00
// Total length
final int totalPacketLength = buffer.position() - startIndex;
Utils.writeVarIntHeader(buffer, packetLengthIndex, totalPacketLength);
2021-03-26 13:08:05 +01:00
}
2021-03-22 14:31:38 +01:00
2021-03-26 13:08:05 +01:00
/**
* Creates a "framed packet" (packet which can be send and understood by a Minecraft client)
* from a server packet, directly into an output buffer.
* <p>
* Can be used if you want to store a raw buffer and send it later without the additional writing cost.
* Compression is applied if {@link MinecraftServer#getCompressionThreshold()} is greater than 0.
*/
public static @NotNull ByteBuffer createFramedPacket(@NotNull ServerPacket serverPacket) {
ByteBuffer packetBuf = ByteBuffer.allocate(2_000_000);
writeFramedPacket(packetBuf, serverPacket);
2021-03-26 13:08:05 +01:00
return packetBuf;
}
2019-08-22 14:52:32 +02:00
}