Interface NetworkChannel

All Known Subinterfaces:
PacketChannel, RawPacketChannel

public interface NetworkChannel
Represents a channel used for network communication.

A network channel can either be an external channel or a packet channel. External channels are identified by a unique key and are used for custom payloads over the network. Packet channels will represent data from packets, identified by a packet ID and type.

For constructing an external NetworkChannel, the following can be done:

 
     private final NetworkChannel myChannel = NetworkChannel.of("example", "my_channel", MyMessage.class);
 
 
Or when inside an extension, with 'this' being the extension instance:
 
     private final NetworkChannel myChannel = NetworkChannel.of(this, "my_channel", MyMessage.class);
 
 

For packet channels keyed by an existing packet class, no ID is needed:

 
    private final NetworkChannel animateChannel = PacketChannel.bedrock(this, AnimatePacket.class);
 
 
For raw-buffer packet channels backed by a custom Message.Packet implementation, the packet ID must be supplied explicitly:
 
    private final NetworkChannel animateChannel = RawPacketChannel.bedrock(this, 44, AnimateMessage.class);
 
 

Packet channels can also be registered against packet objects from external protocol libraries, such as the ones provided in Geyser. For an example on how to do this, please see the Networking API documentation.

Since:
2.9.2
  • Method Details

    • identifier

      Identifier identifier()
      Gets the identifier that owns this channel.
      Returns:
      the identifier that owns this channel
    • isPacket

      boolean isPacket()
      Checks if this channel is a packet channel.
      Returns:
      true if this channel is a packet channel, false otherwise
    • of

      static NetworkChannel of(Extension extension, String channel, Class<?> messageType)
      Creates a new external NetworkChannel instance.

      Extensions should use this method to register their own channels for more robust identification.

      Parameters:
      extension - the extension that registered this channel
      channel - the name of the channel
      messageType - the type of the message sent over this channel
      Returns:
      a new external NetworkChannel instance
    • of

      static NetworkChannel of(String id, String channel, Class<?> messageType)
      Creates a new external NetworkChannel instance.

      This method is used for external channels provided by third parties, such as plugins or mods.

      Parameters:
      id - the channel id
      channel - the name of the channel
      messageType - the type of the message sent over this channel
      Returns:
      a new external NetworkChannel instance
    • of

      static NetworkChannel of(Identifier identifier, Class<?> messageType)
      Creates a new external NetworkChannel instance.

      This method is used for external channels provided by third parties, such as plugins or mods.

      Parameters:
      identifier - the Identifier of the channel
      messageType - the type of the message sent over this channel
      Returns:
      a new external NetworkChannel instance