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.

This class should be only used for the logical server.

See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    canSend(net.minecraft.server.network.ServerPlayerEntity player, net.minecraft.util.Identifier channelName)
    Checks if the connected client declared the ability to receive a packet on a specified channel name.
    static boolean
    canSend(net.minecraft.server.network.ServerPlayNetworkHandler handler, net.minecraft.util.Identifier channelName)
    Checks if the connected client declared the ability to receive a packet on a specified channel name.
    static net.minecraft.network.Packet<?>
    createS2CPacket(net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf)
    Creates a packet which may be sent to a the connected client.
    static Set<net.minecraft.util.Identifier>
    Gets all channel names which global receivers are registered for.
    static Set<net.minecraft.util.Identifier>
    getReceived(net.minecraft.server.network.ServerPlayerEntity player)
    Gets all the channel names that the server can receive packets on.
    static Set<net.minecraft.util.Identifier>
    getReceived(net.minecraft.server.network.ServerPlayNetworkHandler handler)
    Gets all the channel names that the server can receive packets on.
    static Set<net.minecraft.util.Identifier>
    getSendable(net.minecraft.server.network.ServerPlayerEntity player)
    Gets all channel names that the connected client declared the ability to receive a packets on.
    static Set<net.minecraft.util.Identifier>
    getSendable(net.minecraft.server.network.ServerPlayNetworkHandler handler)
    Gets all channel names that a the connected client declared the ability to receive a packets on.
    getSender(net.minecraft.server.network.ServerPlayerEntity player)
    Gets the packet sender which sends packets to the connected client.
    getSender(net.minecraft.server.network.ServerPlayNetworkHandler handler)
    Gets the packet sender which sends packets to the connected client.
    static net.minecraft.server.MinecraftServer
    getServer(net.minecraft.server.network.ServerPlayNetworkHandler handler)
    Returns the Minecraft Server of a server play network handler.
    static boolean
    registerGlobalReceiver(net.minecraft.util.Identifier channelName, ServerPlayNetworking.PlayChannelHandler channelHandler)
    Registers a handler to a channel.
    static boolean
    registerReceiver(net.minecraft.server.network.ServerPlayNetworkHandler networkHandler, net.minecraft.util.Identifier channelName, ServerPlayNetworking.PlayChannelHandler channelHandler)
    Registers a handler to a channel.
    static void
    send(net.minecraft.server.network.ServerPlayerEntity player, net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf)
    Sends a packet to a player.
    unregisterGlobalReceiver(net.minecraft.util.Identifier channelName)
    Removes the handler of a channel.
    unregisterReceiver(net.minecraft.server.network.ServerPlayNetworkHandler networkHandler, net.minecraft.util.Identifier channelName)
    Removes the handler of a channel.

    Methods inherited from class java.lang.Object

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

    • registerGlobalReceiver

      public static boolean registerGlobalReceiver(net.minecraft.util.Identifier channelName, ServerPlayNetworking.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 unregisterReceiver(ServerPlayNetworkHandler, 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

      @Nullable public static @Nullable ServerPlayNetworking.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:
    • 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.server.network.ServerPlayNetworkHandler networkHandler, net.minecraft.util.Identifier channelName, ServerPlayNetworking.PlayChannelHandler channelHandler)
      Registers a handler to a channel. This method differs from registerGlobalReceiver(Identifier, PlayChannelHandler) 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 to the channelName, this method will return false, and no change will be made. Use unregisterReceiver(ServerPlayNetworkHandler, Identifier) to unregister the existing handler.

      Parameters:
      networkHandler - the handler
      channelName - the id of the channel
      channelHandler - the handler
      Returns:
      false if a handler is already registered to the channel name
      See Also:
    • unregisterReceiver

      @Nullable public static @Nullable ServerPlayNetworking.PlayChannelHandler unregisterReceiver(net.minecraft.server.network.ServerPlayNetworkHandler networkHandler, net.minecraft.util.Identifier channelName)
      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 name
    • getReceived

      public static Set<net.minecraft.util.Identifier> getReceived(net.minecraft.server.network.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

      public static Set<net.minecraft.util.Identifier> getReceived(net.minecraft.server.network.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

      public static Set<net.minecraft.util.Identifier> getSendable(net.minecraft.server.network.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

      public static Set<net.minecraft.util.Identifier> getSendable(net.minecraft.server.network.ServerPlayNetworkHandler handler)
      Gets all channel names that a the 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

      public static boolean canSend(net.minecraft.server.network.ServerPlayerEntity player, net.minecraft.util.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

      public static boolean canSend(net.minecraft.server.network.ServerPlayNetworkHandler handler, net.minecraft.util.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
    • createS2CPacket

      public static net.minecraft.network.Packet<?> createS2CPacket(net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf)
      Creates a packet which may be sent to a the connected client.
      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(net.minecraft.server.network.ServerPlayerEntity player)
      Gets the packet sender which sends packets to the connected client.
      Parameters:
      player - the player
      Returns:
      the packet sender
    • getSender

      public static PacketSender getSender(net.minecraft.server.network.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

      public static void send(net.minecraft.server.network.ServerPlayerEntity player, net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf)
      Sends a packet to a player.
      Parameters:
      player - the player to send the packet to
      channelName - the channel of the packet
      buf - the payload of the packet.
    • getServer

      public static net.minecraft.server.MinecraftServer getServer(net.minecraft.server.network.ServerPlayNetworkHandler handler)
      Returns the Minecraft Server of a server play network handler.
      Parameters:
      handler - the server play network handler