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 SummaryModifier and TypeMethodDescriptionstatic FramedPacketallocateTrimmedPacket(@NotNull ConnectionState state, @NotNull ServerPacket packet) static voidbroadcastPlayPacket(@NotNull ServerPacket packet) static ByteBuffercreateFramedPacket(@NotNull ConnectionState state, @NotNull ByteBuffer buffer, @NotNull ServerPacket packet) static ByteBuffercreateFramedPacket(@NotNull ConnectionState state, @NotNull ByteBuffer buffer, @NotNull ServerPacket packet, boolean compression) static voidflush()static intinvalidPacketState(@NotNull Class<?> packetClass, @NotNull ConnectionState state, @NotNull ConnectionState... expected) static voidprepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket) static voidprepareViewablePacket(@NotNull Viewable viewable, @NotNull ServerPacket serverPacket, @Nullable Entity entity) static @Nullable BinaryBufferreadPackets(@NotNull BinaryBuffer readBuffer, boolean compressed, BiConsumer<Integer, ByteBuffer> payloadConsumer) static voidsendGroupedPacket(@NotNull Collection<Player> players, @NotNull ServerPacket packet) Same assendGroupedPacket(Collection, ServerPacket, Predicate)but with the player validator sets to null.static voidsendGroupedPacket(@NotNull Collection<Player> players, @NotNull ServerPacket packet, @NotNull Predicate<Player> predicate) Sends aServerPacketto multiple players.static voidsendPacket(@NotNull Audience audience, @NotNull ServerPacket packet) Sends a packet to an audience.static voidwriteFramedPacket(@NotNull ByteBuffer buffer, int id, NetworkBuffer.Writer writer, int compressionThreshold) static voidwriteFramedPacket(@NotNull ConnectionState state, @NotNull ByteBuffer buffer, @NotNull ServerPacket packet, boolean compression) 
- 
Method Details- 
sendPacketpublic 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 audienceis aPlayer, send the packet to them.
- Otherwise, if audienceis aPacketGroupingAudience, callsendGroupedPacket(Collection, ServerPacket)on the players that the grouping audience contains.
- Otherwise, if audienceis aForwardingAudience.Single, call this method on the single audience inside the forwarding audience.
- Otherwise, if audienceis aForwardingAudience, call this method for each audience member of the forwarding audience.
- Otherwise, do nothing.
 - Parameters:
- audience- the audience
- packet- the packet
 
- If 
- 
sendGroupedPacketpublic static void sendGroupedPacket(@NotNull @NotNull Collection<Player> players, @NotNull @NotNull ServerPacket packet, @NotNull @NotNull Predicate<Player> predicate) Sends aServerPacketto 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 to
- packet- the packet to send to the players
- predicate- predicate to ignore specific players
 
- 
sendGroupedPacketpublic 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
 
- 
writeFramedPacketpublic static void writeFramedPacket(@NotNull @NotNull ConnectionState state, @NotNull @NotNull ByteBuffer buffer, @NotNull @NotNull ServerPacket packet, boolean compression) 
- 
writeFramedPacketpublic 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) 
 
-