Interface NMSPacketHandler


public interface NMSPacketHandler
Handler interface for NMS packet operations and management.

This interface provides the primary API for working with Minecraft packets, including wrapping raw NMS packet objects, creating new packets, and sending packets to players. It abstracts away the complexity of version-specific packet handling while providing a unified interface for packet operations.

The packet handler serves as both a factory for creating packet wrappers and as a communication bridge for sending packets to clients. It handles the intricate details of packet serialization, version compatibility, and network transmission.

Example usage:


 // Wrap an existing NMS packet
 NMSPacket wrappedPacket = handler.wrapPacket(rawNMSPacket);
 
 // Create a new packet
 NMSOutEntityDestroy destroyPacket = handler.createDestroyPacket(1, 2, 3);
 
 // Send packet to player
 handler.sendPacket(player, destroyPacket);
 

  • Method Summary

    Modifier and Type
    Method
    Description
    createDestroyPacket(int... ids)
    Creates a new entity destroy packet for the specified entity IDs.
    void
    sendPacket(@NotNull Player player, @NotNull NMSPacket nmsPacket)
    Sends a packet to the specified player.
    @NotNull NMSPacket
    wrapPacket(@NotNull Object packet)
    Wraps a raw NMS packet object in a type-safe wrapper.
  • Method Details

    • wrapPacket

      @NotNull @NotNull NMSPacket wrapPacket(@NotNull @NotNull Object packet) throws IllegalStateException
      Wraps a raw NMS packet object in a type-safe wrapper.

      This method takes a raw NMS packet object and creates an appropriate wrapper that implements the NMSPacket interface. The wrapper provides type-safe access to packet data and functionality while hiding version-specific implementation details.

      The method automatically detects the packet type and creates the most appropriate wrapper implementation. If the packet type is not supported or recognized, an IllegalStateException is thrown.

      Parameters:
      packet - the raw NMS packet object to wrap
      Returns:
      a NMSPacket wrapper for the specified packet
      Throws:
      IllegalStateException - if the packet type is unsupported or invalid
    • createDestroyPacket

      @NotNull @NotNull NMSOutEntityDestroy createDestroyPacket(int... ids)
      Creates a new entity destroy packet for the specified entity IDs.

      This method creates a packet that, when sent to a client, will cause the specified entities to be removed from the client's view. This is commonly used for despawning entities, hiding entities from specific players, or cleaning up entity displays.

      The created packet can be sent to one or more players using the sendPacket(Player, NMSPacket) method.

      Parameters:
      ids - the entity IDs to include in the destroy packet
      Returns:
      a new NMSOutEntityDestroy packet containing the specified entity IDs
    • sendPacket

      void sendPacket(@NotNull @NotNull Player player, @NotNull @NotNull NMSPacket nmsPacket)
      Sends a packet to the specified player.

      This method transmits the specified packet to the given player's client, handling all the necessary serialization and network communication details. The packet is sent asynchronously and does not block the calling thread.

      The method works with any NMSPacket wrapper, automatically extracting the underlying NMS packet and handling version-specific transmission requirements.

      Parameters:
      player - the Player to send the packet to
      nmsPacket - the NMSPacket to send