Class ClientPlayNetworking

java.lang.Object
net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking

@Environment(CLIENT) public final class ClientPlayNetworking extends Object
Offers access to play stage client-side networking functionalities.

Client-side networking functionalities include receiving clientbound packets, sending serverbound packets, and events related to client-side network handlers.

This class should be only used on the physical client and for the logical client.

See Also:
ClientLoginNetworking, ServerPlayNetworking
  • Method Details

    • registerGlobalReceiver

      public static boolean registerGlobalReceiver(net.minecraft.util.Identifier channelName, ClientPlayNetworking.PlayChannelHandler channelHandler)
      Registers a handler to a channel. A global receiver is registered to all connections, in the present and future.

      If a handler is already registered to the channel, this method will return false, and no change will be made. Use unregisterGlobalReceiver(Identifier) to unregister the existing handler.

      Parameters:
      channelName - the id of the channel
      channelHandler - the handler
      Returns:
      false if a handler is already registered to the channel
      See Also:
      unregisterGlobalReceiver(Identifier), registerReceiver(Identifier, PlayChannelHandler)
    • unregisterGlobalReceiver

      @Nullable public static @Nullable ClientPlayNetworking.PlayChannelHandler unregisterGlobalReceiver(net.minecraft.util.Identifier channelName)
      Removes the handler of a channel. A global receiver is registered to all connections, in the present and future.

      The channel is guaranteed not to have a handler after this call.

      Parameters:
      channelName - the id of the channel
      Returns:
      the previous handler, or null if no handler was bound to the channel
      See Also:
      registerGlobalReceiver(Identifier, PlayChannelHandler), unregisterReceiver(Identifier)
    • getGlobalReceivers

      public static Set<net.minecraft.util.Identifier> getGlobalReceivers()
      Gets all channel names which global receivers are registered for. A global receiver is registered to all connections, in the present and future.
      Returns:
      all channel names which global receivers are registered for.
    • registerReceiver

      public static boolean registerReceiver(net.minecraft.util.Identifier channelName, ClientPlayNetworking.PlayChannelHandler channelHandler)
      Registers a handler to a channel.

      If a handler is already registered to the channel, this method will return false, and no change will be made. Use unregisterReceiver(Identifier) to unregister the existing handler.

      For example, if you only register a receiver using this method when a ClientLoginNetworking.registerGlobalReceiver(Identifier, ClientLoginNetworking.LoginQueryRequestHandler) login query has been received, you should use ClientPlayConnectionEvents.INIT to register the channel handler.

      Parameters:
      channelName - the id of the channel
      Returns:
      false if a handler is already registered to the channel
      Throws:
      IllegalStateException - if the client is not connected to a server
      See Also:
      ClientPlayConnectionEvents.INIT
    • unregisterReceiver

      @Nullable public static @Nullable ClientPlayNetworking.PlayChannelHandler unregisterReceiver(net.minecraft.util.Identifier channelName) throws IllegalStateException
      Removes the handler of a channel.

      The channelName is guaranteed not to have a handler after this call.

      Parameters:
      channelName - the id of the channel
      Returns:
      the previous handler, or null if no handler was bound to the channel
      Throws:
      IllegalStateException - if the client is not connected to a server
    • getReceived

      public static Set<net.minecraft.util.Identifier> getReceived() throws IllegalStateException
      Gets all the channel names that the client can receive packets on.
      Returns:
      All the channel names that the client can receive packets on
      Throws:
      IllegalStateException - if the client is not connected to a server
    • getSendable

      public static Set<net.minecraft.util.Identifier> getSendable() throws IllegalStateException
      Gets all channel names that the connected server declared the ability to receive a packets on.
      Returns:
      All the channel names the connected server declared the ability to receive a packets on
      Throws:
      IllegalStateException - if the client is not connected to a server
    • canSend

      public static boolean canSend(net.minecraft.util.Identifier channelName) throws IllegalArgumentException
      Checks if the connected server declared the ability to receive a packet on a specified channel name.
      Parameters:
      channelName - the channel name
      Returns:
      True if the connected server has declared the ability to receive a packet on the specified channel. False if the client is not in game.
      Throws:
      IllegalArgumentException
    • createC2SPacket

      public static net.minecraft.network.Packet<?> createC2SPacket(net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf)
      Creates a packet which may be sent to a the connected server.
      Parameters:
      channelName - the channel name
      buf - the packet byte buf which represents the payload of the packet
      Returns:
      a new packet
    • getSender

      public static PacketSender getSender() throws IllegalStateException
      Gets the packet sender which sends packets to the connected server.
      Returns:
      the client's packet sender
      Throws:
      IllegalStateException - if the client is not connected to a server
    • send

      public static void send(net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf) throws IllegalStateException
      Sends a packet to the connected server.
      Parameters:
      channelName - the channel of the packet
      buf - the payload of the packet
      Throws:
      IllegalStateException - if the client is not connected to a server