Class ClientPlayNetworking

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

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 packet listeners. Packets received by this class must be registered to PayloadTypeRegistry.clientboundPlay() on both ends. Packets sent by this class must be registered to PayloadTypeRegistry.serverboundPlay() on both ends. Packets must be registered before registering any receivers.

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

See ServerPlayNetworking for information on how to use the payload object-based API.

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
     
    static interface 
    ClientPlayNetworking.PlayPayloadHandler<T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    A thread-safe payload handler utilizing CustomPacketPayload.
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    canSend(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<?> type)
    Checks if the connected server declared the ability to receive a payload on a specified channel name.
    static boolean
    canSend(net.minecraft.resources.Identifier channelName)
    Checks if the connected server declared the ability to receive a payload on a specified channel name.
    static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    net.minecraft.network.protocol.Packet<net.minecraft.network.protocol.common.ServerCommonPacketListener>
    Creates a payload which may be sent to the connected server.
    static Set<net.minecraft.resources.Identifier>
    Gets all channel names which global receivers are registered for.
    static Set<net.minecraft.resources.Identifier>
    Gets all the channel names that the client can receive packets on.
    static Set<net.minecraft.resources.Identifier>
    Gets all channel names that the connected server declared the ability to receive a packets on.
    Gets the payload sender which sends packets to the connected server.
    static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    boolean
    registerGlobalReceiver(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, ClientPlayNetworking.PlayPayloadHandler<T> handler)
    Registers a handler for a payload type.
    static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
    boolean
    registerReceiver(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, ClientPlayNetworking.PlayPayloadHandler<T> handler)
    Registers a handler for a payload type.
    static void
    send(net.minecraft.network.protocol.common.custom.CustomPacketPayload payload)
    Sends a payload to the connected server.
    unregisterGlobalReceiver(net.minecraft.resources.Identifier id)
    Removes the handler for a payload type.
    unregisterReceiver(net.minecraft.resources.Identifier id)
    Removes the handler for a payload id.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • registerGlobalReceiver

      public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> boolean registerGlobalReceiver(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, ClientPlayNetworking.PlayPayloadHandler<T> handler)
      Registers a handler for a payload type. A global receiver is registered to all connections, in the present and future.

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

      Parameters:
      type - the payload type
      handler - the handler
      Returns:
      false if a handler is already registered to the channel
      Throws:
      IllegalArgumentException - if the codec for type has not been registered yet
      See Also:
    • unregisterGlobalReceiver

      public static @Nullable ClientPlayNetworking.PlayPayloadHandler<?> unregisterGlobalReceiver(net.minecraft.resources.Identifier id)
      Removes the handler for a payload type. A global receiver is registered to all connections, in the present and future.

      The type is guaranteed not to have an associated handler after this call.

      Parameters:
      id - the payload id
      Returns:
      the previous handler, or null if no handler was bound to the channel, or it was not registered using registerGlobalReceiver(CustomPacketPayload.Type, PlayPayloadHandler)
      See Also:
    • getGlobalReceivers

      public static Set<net.minecraft.resources.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 <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> boolean registerReceiver(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, ClientPlayNetworking.PlayPayloadHandler<T> handler)
      Registers a handler for a payload type.

      If a handler is already registered for the type, 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:
      type - the payload type
      handler - the handler
      Returns:
      false if a handler is already registered for the type
      Throws:
      IllegalArgumentException - if the codec for type has not been registered yet
      IllegalStateException - if the client is not connected to a server
      See Also:
    • unregisterReceiver

      public static @Nullable ClientPlayNetworking.PlayPayloadHandler<?> unregisterReceiver(net.minecraft.resources.Identifier id)
      Removes the handler for a payload id.

      The type is guaranteed not to have an associated handler after this call.

      Parameters:
      id - the payload id
      Returns:
      the previous handler, or null if no handler was bound to the channel, or it was not registered using registerReceiver(CustomPacketPayload.Type, PlayPayloadHandler)
      Throws:
      IllegalStateException - if the client is not connected to a server
    • getReceived

      public static Set<net.minecraft.resources.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.resources.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.resources.Identifier channelName) throws IllegalArgumentException
      Checks if the connected server declared the ability to receive a payload on a specified channel name.
      Parameters:
      channelName - the channel name
      Returns:
      true if the connected server has declared the ability to receive a payload on the specified channel. False if the client is not in game.
      Throws:
      IllegalArgumentException
    • canSend

      public static boolean canSend(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<?> type)
      Checks if the connected server declared the ability to receive a payload on a specified channel name. This returns false if the client is not in game.
      Parameters:
      type - the payload type
      Returns:
      true if the connected server has declared the ability to receive a payload on the specified channel
    • createServerboundPacket

      public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> net.minecraft.network.protocol.Packet<net.minecraft.network.protocol.common.ServerCommonPacketListener> createServerboundPacket(T packet)
      Creates a payload which may be sent to the connected server.
      Parameters:
      packet - the fabric payload
      Returns:
      a new payload
    • getSender

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

      public static void send(net.minecraft.network.protocol.common.custom.CustomPacketPayload payload)
      Sends a payload to the connected server.

      Any packets sent must be registered.

      Parameters:
      payload - the payload
      Throws:
      IllegalStateException - if the client is not connected to a server