Class PacketUtils

java.lang.Object
net.minestom.server.utils.PacketUtils

public final class PacketUtils extends Object
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 Details

    • sendPacket

      public static void sendPacket(@NotNull @NotNull net.kyori.adventure.audience.Audience audience, @NotNull @NotNull ServerPacket packet)
      Sends a packet to an audience. This method performs the following steps in the following order:
      1. If audience is a Player, send the packet to them.
      2. Otherwise, if audience is a PacketGroupingAudience, call sendGroupedPacket(Collection, ServerPacket) on the players that the grouping audience contains.
      3. Otherwise, if audience is a ForwardingAudience.Single, call this method on the single audience inside the forwarding audience.
      4. Otherwise, if audience is a ForwardingAudience, call this method for each audience member of the forwarding audience.
      5. Otherwise, do nothing.
      Parameters:
      audience - the audience
      packet - the packet
    • sendGroupedPacket

      public static void sendGroupedPacket(@NotNull @NotNull Collection<Player> players, @NotNull @NotNull ServerPacket packet, @NotNull @NotNull Predicate<Player> predicate)
      Sends a ServerPacket 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 to
      packet - the packet to send to the players
      predicate - predicate to ignore specific players
    • sendGroupedPacket

      public static void sendGroupedPacket(@NotNull @NotNull Collection<Player> players, @NotNull @NotNull ServerPacket packet)
      Same as sendGroupedPacket(Collection, ServerPacket, Predicate) but with the player validator sets to null.
      See Also:
    • broadcastPlayPacket

      public static void broadcastPlayPacket(@NotNull @NotNull ServerPacket packet)
    • 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)