Package net.minestom.server.utils
Class PacketUtils
java.lang.Object
net.minestom.server.utils.PacketUtils
Utils class for packets. Including writing a
ServerPacket
into a ByteBuffer
for network processing.
Note that all methods are mostly internal and can change at any moment. This is due to their very unsafe nature (use of local buffers as cache) and their potential performance impact. Be sure to check the implementation code.
-
Method Summary
Modifier and TypeMethodDescriptionstatic FramedPacket
allocateTrimmedPacket
(@NotNull ConnectionState state, @NotNull ServerPacket packet) static void
broadcastPlayPacket
(@NotNull ServerPacket packet) static ByteBuffer
createFramedPacket
(@NotNull ConnectionState state, @NotNull ByteBuffer buffer, @NotNull ServerPacket packet) static ByteBuffer
createFramedPacket
(@NotNull ConnectionState state, @NotNull ByteBuffer buffer, @NotNull ServerPacket packet, boolean compression) static void
flush()
static int
invalidPacketState
(@NotNull Class<?> packetClass, @NotNull ConnectionState state, @NotNull ConnectionState... expected) static void
prepareViewablePacket
(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket) static void
prepareViewablePacket
(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket, @Nullable Entity entity) static @Nullable BinaryBuffer
readPackets
(@NotNull BinaryBuffer readBuffer, boolean compressed, BiConsumer<Integer, ByteBuffer> payloadConsumer) static void
sendGroupedPacket
(@NotNull Collection<Player> players, @NotNull ServerPacket packet) Same assendGroupedPacket(Collection, ServerPacket, Predicate)
but with the player validator sets to null.static void
sendGroupedPacket
(@NotNull Collection<Player> players, @NotNull ServerPacket packet, @NotNull Predicate<Player> predicate) Sends aServerPacket
to multiple players.static void
sendPacket
(@NotNull Audience audience, @NotNull ServerPacket packet) Sends a packet to an audience.static void
writeFramedPacket
(@NotNull ByteBuffer buffer, int id, NetworkBuffer.Writer writer, int compressionThreshold) static void
writeFramedPacket
(@NotNull ConnectionState state, @NotNull ByteBuffer buffer, @NotNull ServerPacket packet, boolean compression)
-
Method Details
-
sendPacket
public static void sendPacket(@NotNull @NotNull Audience audience, @NotNull @NotNull ServerPacket packet) Sends a packet to an audience. This method performs the following steps in the following order:- If
audience
is aPlayer
, send the packet to them. - Otherwise, if
audience
is aPacketGroupingAudience
, callsendGroupedPacket(Collection, ServerPacket)
on the players that the grouping audience contains. - Otherwise, if
audience
is aForwardingAudience.Single
, call this method on the single audience inside the forwarding audience. - Otherwise, if
audience
is aForwardingAudience
, call this method for each audience member of the forwarding audience. - Otherwise, do nothing.
- Parameters:
audience
- the audiencepacket
- the packet
- If
-
sendGroupedPacket
public static void sendGroupedPacket(@NotNull @NotNull Collection<Player> players, @NotNull @NotNull ServerPacket packet, @NotNull @NotNull Predicate<Player> predicate) Sends aServerPacket
to multiple players.Can drastically improve performance since the packet will not have to be processed as much.
- Parameters:
players
- the players to send the packet topacket
- the packet to send to the playerspredicate
- predicate to ignore specific players
-
sendGroupedPacket
public static void sendGroupedPacket(@NotNull @NotNull Collection<Player> players, @NotNull @NotNull ServerPacket packet) Same assendGroupedPacket(Collection, ServerPacket, Predicate)
but with the player validator sets to null.- See Also:
-
broadcastPlayPacket
-
prepareViewablePacket
@Experimental public static void prepareViewablePacket(@NotNull @NotNull Viewable viewable, @NotNull @NotNull ServerPacket serverPacket, @Nullable @Nullable Entity entity) -
prepareViewablePacket
@Experimental public static void prepareViewablePacket(@NotNull @NotNull Viewable viewable, @NotNull @NotNull ServerPacket serverPacket) -
flush
@Internal public static void flush() -
readPackets
@Internal @Nullable public static @Nullable BinaryBuffer readPackets(@NotNull @NotNull BinaryBuffer readBuffer, boolean compressed, BiConsumer<Integer, ByteBuffer> payloadConsumer) throws DataFormatException- Throws:
DataFormatException
-
writeFramedPacket
public static void writeFramedPacket(@NotNull @NotNull ConnectionState state, @NotNull @NotNull ByteBuffer buffer, @NotNull @NotNull ServerPacket packet, boolean compression) -
writeFramedPacket
public static void writeFramedPacket(@NotNull @NotNull ByteBuffer buffer, int id, @NotNull NetworkBuffer.Writer writer, int compressionThreshold) -
createFramedPacket
@Internal public static ByteBuffer createFramedPacket(@NotNull @NotNull ConnectionState state, @NotNull @NotNull ByteBuffer buffer, @NotNull @NotNull ServerPacket packet, boolean compression) -
createFramedPacket
@Internal public static ByteBuffer createFramedPacket(@NotNull @NotNull ConnectionState state, @NotNull @NotNull ByteBuffer buffer, @NotNull @NotNull ServerPacket packet) -
allocateTrimmedPacket
@Internal public static FramedPacket allocateTrimmedPacket(@NotNull @NotNull ConnectionState state, @NotNull @NotNull ServerPacket packet) -
invalidPacketState
@Internal public static int invalidPacketState(@NotNull @NotNull Class<?> packetClass, @NotNull @NotNull ConnectionState state, @NotNull @NotNull ConnectionState... expected)
-