Class ServerPlayNetworking

java.lang.Object
net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking

public final class ServerPlayNetworking extends Object
Offers access to play stage server-side networking functionalities.

Server-side networking functionalities include receiving serverbound packets, sending clientbound packets, and events related to server-side network handlers. Packets received by this class must be registered to PayloadTypeRegistry.playC2S() on both ends. Packets sent by this class must be registered to PayloadTypeRegistry.playS2C() on both ends. Packets must be registered before registering any receivers.

This class should be only used for the logical server.

Packet object-based API Link icon

This class provides a registration method, utilizing packet objects, registerGlobalReceiver(CustomPayload.Id, PlayPayloadHandler). This handler executes the callback in the server thread, ensuring thread safety.

This payload object-based API involves three classes:

See the documentation on each class for more information.

See Also:
  • Method Details Link icon

    • registerGlobalReceiver Link icon

      public static <T extends CustomPayload> boolean registerGlobalReceiver(CustomPayload.Id<T> type, ServerPlayNetworking.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 packet 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 Link icon

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

      The id 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(CustomPayload.Id, PlayPayloadHandler)
      See Also:
    • getGlobalReceivers Link icon

      public static Set<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 Link icon

      public static <T extends CustomPayload> boolean registerReceiver(ServerPlayNetworkHandler networkHandler, CustomPayload.Id<T> type, ServerPlayNetworking.PlayPayloadHandler<T> handler)
      Registers a handler for a payload type. This method differs from registerGlobalReceiver(CustomPayload.Id, PlayPayloadHandler) since the channel handler will only be applied to the player represented by the ServerPlayNetworkHandler.

      For example, if you only register a receiver using this method when a ServerLoginNetworking.registerGlobalReceiver(Identifier, ServerLoginNetworking.LoginQueryResponseHandler) login response has been received, you should use ServerPlayConnectionEvents.INIT to register the channel handler.

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

      Parameters:
      networkHandler - the network handler
      type - the packet type
      handler - the handler
      Returns:
      false if a handler is already registered to the channel name
      Throws:
      IllegalArgumentException - if the codec for type has not been registered yet
      See Also:
    • unregisterReceiver Link icon

      @Nullable public static ServerPlayNetworking.PlayPayloadHandler<?> unregisterReceiver(ServerPlayNetworkHandler networkHandler, Identifier id)
      Removes the handler for a packet type.

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

      Parameters:
      id - the id of the payload
      Returns:
      the previous handler, or null if no handler was bound to the channel, or it was not registered using registerReceiver(ServerPlayNetworkHandler, CustomPayload.Id, PlayPayloadHandler)
    • getReceived Link icon

      public static Set<Identifier> getReceived(ServerPlayerEntity player)
      Gets all the channel names that the server can receive packets on.
      Parameters:
      player - the player
      Returns:
      All the channel names that the server can receive packets on
    • getReceived Link icon

      public static Set<Identifier> getReceived(ServerPlayNetworkHandler handler)
      Gets all the channel names that the server can receive packets on.
      Parameters:
      handler - the network handler
      Returns:
      All the channel names that the server can receive packets on
    • getSendable Link icon

      public static Set<Identifier> getSendable(ServerPlayerEntity player)
      Gets all channel names that the connected client declared the ability to receive a packets on.
      Parameters:
      player - the player
      Returns:
      All the channel names the connected client declared the ability to receive a packets on
    • getSendable Link icon

      public static Set<Identifier> getSendable(ServerPlayNetworkHandler handler)
      Gets all channel names that a connected client declared the ability to receive a packets on.
      Parameters:
      handler - the network handler
      Returns:
      true if the connected client has declared the ability to receive a packet on the specified channel
    • canSend Link icon

      public static boolean canSend(ServerPlayerEntity player, Identifier channelName)
      Checks if the connected client declared the ability to receive a packet on a specified channel name.
      Parameters:
      player - the player
      channelName - the channel name
      Returns:
      true if the connected client has declared the ability to receive a packet on the specified channel
    • canSend Link icon

      public static boolean canSend(ServerPlayerEntity player, CustomPayload.Id<?> type)
      Checks if the connected client declared the ability to receive a specific type of packet.
      Parameters:
      player - the player
      type - the packet type
      Returns:
      true if the connected client has declared the ability to receive a specific type of packet
    • canSend Link icon

      public static boolean canSend(ServerPlayNetworkHandler handler, Identifier channelName)
      Checks if the connected client declared the ability to receive a packet on a specified channel name.
      Parameters:
      handler - the network handler
      channelName - the channel name
      Returns:
      true if the connected client has declared the ability to receive a packet on the specified channel
    • canSend Link icon

      public static boolean canSend(ServerPlayNetworkHandler handler, CustomPayload.Id<?> type)
      Checks if the connected client declared the ability to receive a specific type of packet.
      Parameters:
      handler - the network handler
      type - the packet type
      Returns:
      true if the connected client has declared the ability to receive a specific type of packet
    • createS2CPacket Link icon

      public static <T extends CustomPayload> Packet<ClientCommonPacketListener> createS2CPacket(T packet)
      Creates a packet which may be sent to a connected client.
      Parameters:
      packet - the packet
      Returns:
      a new packet
    • getSender Link icon

      public static PacketSender getSender(ServerPlayerEntity player)
      Gets the packet sender which sends packets to the connected client.
      Parameters:
      player - the player
      Returns:
      the packet sender
    • getSender Link icon

      public static PacketSender getSender(ServerPlayNetworkHandler handler)
      Gets the packet sender which sends packets to the connected client.
      Parameters:
      handler - the network handler, representing the connection to the player/client
      Returns:
      the packet sender
    • send Link icon

      public static void send(ServerPlayerEntity player, CustomPayload payload)
      Sends a packet to a player.

      Any packets sent must be registered.

      Parameters:
      player - the player to send the packet to
      payload - the payload to send
    • reconfigure Link icon

      public static void reconfigure(ServerPlayerEntity player)
      Put the player back into configuration phase and re-run all of the configuration tasks.
      Parameters:
      player - the player
    • reconfigure Link icon

      public static void reconfigure(ServerPlayNetworkHandler handler)
      Put the player back into configuration phase and re-run all of the configuration tasks.
      Parameters:
      handler - the network handler