Class ServerPlayNetworking
Server-side networking functionalities include receiving serverbound packets, sending clientbound packets, and events related to server-side packet listeners.
Packets received by this class must be registered to PayloadTypeRegistry.serverboundPlay() on both ends.
Packets sent by this class must be registered to PayloadTypeRegistry.clientboundPlay() 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
This class provides a registration method, utilizing packet objects, registerGlobalReceiver(CustomPacketPayload.Type, PlayPayloadHandler).
This handler executes the callback in the server thread, ensuring thread safety.
This payload object-based API involves three classes:
- A class implementing
CustomPacketPayloadthat is "sent" over the network CustomPacketPayload.TypeAndCodecinstance, which represents the packet's type (and its codec)ServerPlayNetworking.PlayPayloadHandler, which handles the packet (usually implemented as a functional interface)
See the documentation on each class for more information.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfacestatic interfaceServerPlayNetworking.PlayPayloadHandler<T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>A thread-safe packet handler utilizingCustomPacketPayload. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanSend(net.minecraft.server.level.ServerPlayer player, net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<?> type) Checks if the connected client declared the ability to receive a specific type of packet.static booleancanSend(net.minecraft.server.level.ServerPlayer player, net.minecraft.resources.Identifier channelName) Checks if the connected client declared the ability to receive a packet on a specified channel name.static booleancanSend(net.minecraft.server.network.ServerGamePacketListenerImpl listener, net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<?> type) Checks if the connected client declared the ability to receive a specific type of packet.static booleancanSend(net.minecraft.server.network.ServerGamePacketListenerImpl listener, net.minecraft.resources.Identifier channelName) Checks if the connected client declared the ability to receive a packet 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.ClientCommonPacketListener> createClientboundPacket(T packet) Creates a packet which may be sent to a connected client.static Set<net.minecraft.resources.Identifier> Gets all channel names which global receivers are registered for.static Set<net.minecraft.resources.Identifier> getReceived(net.minecraft.server.level.ServerPlayer player) Gets all the channel names that the server can receive packets on.static Set<net.minecraft.resources.Identifier> getReceived(net.minecraft.server.network.ServerGamePacketListenerImpl listener) Gets all the channel names that the server can receive packets on.static Set<net.minecraft.resources.Identifier> getSendable(net.minecraft.server.level.ServerPlayer player) Gets all channel names that the connected client declared the ability to receive a packets on.static Set<net.minecraft.resources.Identifier> getSendable(net.minecraft.server.network.ServerGamePacketListenerImpl listener) Gets all channel names that a connected client declared the ability to receive a packets on.static PacketSendergetSender(net.minecraft.server.level.ServerPlayer player) Gets the packet sender which sends packets to the connected client.static PacketSendergetSender(net.minecraft.server.network.ServerGamePacketListenerImpl listener) Gets the packet sender which sends packets to the connected client.static voidreconfigure(net.minecraft.server.level.ServerPlayer player) Put the player back into configuration phase and re-run all of the configuration tasks.static voidreconfigure(net.minecraft.server.network.ServerGamePacketListenerImpl listener) Put the player back into configuration phase and re-run all of the configuration tasks.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
booleanregisterGlobalReceiver(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, ServerPlayNetworking.PlayPayloadHandler<T> handler) Registers a handler for a payload type.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
booleanregisterReceiver(net.minecraft.server.network.ServerGamePacketListenerImpl packetListener, net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, ServerPlayNetworking.PlayPayloadHandler<T> handler) Registers a handler for a payload type.static voidsend(net.minecraft.server.level.ServerPlayer player, net.minecraft.network.protocol.common.custom.CustomPacketPayload payload) Sends a packet to a player.static @Nullable ServerPlayNetworking.PlayPayloadHandler<?> unregisterGlobalReceiver(net.minecraft.resources.Identifier id) Removes the handler for a payload type.static @Nullable ServerPlayNetworking.PlayPayloadHandler<?> unregisterReceiver(net.minecraft.server.network.ServerGamePacketListenerImpl packetListener, net.minecraft.resources.Identifier id) Removes the handler for a packet type.
-
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, 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 returnfalse, and no change will be made. UseunregisterGlobalReceiver(Identifier)to unregister the existing handler.- Parameters:
type- the packet typehandler- the handler- Returns:
falseif a handler is already registered to the channel- Throws:
IllegalArgumentException- if the codec fortypehas not been registered yet- See Also:
-
unregisterGlobalReceiver
public static @Nullable ServerPlayNetworking.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
idis guaranteed not to have an associated handler after this call.- Parameters:
id- the payload id- Returns:
- the previous handler, or
nullif no handler was bound to the channel, or it was not registered usingregisterGlobalReceiver(CustomPacketPayload.Type, PlayPayloadHandler) - See Also:
-
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.server.network.ServerGamePacketListenerImpl packetListener, net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, ServerPlayNetworking.PlayPayloadHandler<T> handler) Registers a handler for a payload type. This method differs fromregisterGlobalReceiver(CustomPacketPayload.Type, PlayPayloadHandler)since the channel handler will only be applied to the player represented by theServerGamePacketListenerImpl.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.INITto register the channel handler.If a handler is already registered for the
type, this method will returnfalse, and no change will be made. UseunregisterReceiver(ServerGamePacketListenerImpl, Identifier)to unregister the existing handler.- Parameters:
packetListener- the packet listenertype- the packet typehandler- the handler- Returns:
falseif a handler is already registered to the channel name- Throws:
IllegalArgumentException- if the codec fortypehas not been registered yet- See Also:
-
unregisterReceiver
public static @Nullable ServerPlayNetworking.PlayPayloadHandler<?> unregisterReceiver(net.minecraft.server.network.ServerGamePacketListenerImpl packetListener, net.minecraft.resources.Identifier id) Removes the handler for a packet type.The
idis guaranteed not to have an associated handler after this call.- Parameters:
id- the id of the payload- Returns:
- the previous handler, or
nullif no handler was bound to the channel, or it was not registered usingregisterReceiver(ServerGamePacketListenerImpl, CustomPacketPayload.Type, PlayPayloadHandler)
-
getReceived
public static Set<net.minecraft.resources.Identifier> getReceived(net.minecraft.server.level.ServerPlayer 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.resources.Identifier> getReceived(net.minecraft.server.network.ServerGamePacketListenerImpl listener) Gets all the channel names that the server can receive packets on.- Parameters:
listener- the packet listener- Returns:
- All the channel names that the server can receive packets on
-
getSendable
public static Set<net.minecraft.resources.Identifier> getSendable(net.minecraft.server.level.ServerPlayer 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.resources.Identifier> getSendable(net.minecraft.server.network.ServerGamePacketListenerImpl listener) Gets all channel names that a connected client declared the ability to receive a packets on.- Parameters:
listener- the packet listener- Returns:
trueif the connected client has declared the ability to receive a packet on the specified channel
-
canSend
public static boolean canSend(net.minecraft.server.level.ServerPlayer player, net.minecraft.resources.Identifier channelName) Checks if the connected client declared the ability to receive a packet on a specified channel name.- Parameters:
player- the playerchannelName- the channel name- Returns:
trueif the connected client has declared the ability to receive a packet on the specified channel
-
canSend
public static boolean canSend(net.minecraft.server.level.ServerPlayer player, net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<?> type) Checks if the connected client declared the ability to receive a specific type of packet.- Parameters:
player- the playertype- the packet type- Returns:
trueif the connected client has declared the ability to receive a specific type of packet
-
canSend
public static boolean canSend(net.minecraft.server.network.ServerGamePacketListenerImpl listener, net.minecraft.resources.Identifier channelName) Checks if the connected client declared the ability to receive a packet on a specified channel name.- Parameters:
listener- the packet listenerchannelName- the channel name- Returns:
trueif the connected client has declared the ability to receive a packet on the specified channel
-
canSend
public static boolean canSend(net.minecraft.server.network.ServerGamePacketListenerImpl listener, net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<?> type) Checks if the connected client declared the ability to receive a specific type of packet.- Parameters:
listener- the packet listenertype- the packet type- Returns:
trueif the connected client has declared the ability to receive a specific type of packet
-
createClientboundPacket
public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> net.minecraft.network.protocol.Packet<net.minecraft.network.protocol.common.ClientCommonPacketListener> createClientboundPacket(T packet) Creates a packet which may be sent to a connected client.- Parameters:
packet- the packet- Returns:
- a new packet
-
getSender
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.ServerGamePacketListenerImpl listener) Gets the packet sender which sends packets to the connected client.- Parameters:
listener- the packet listener, representing the connection to the player/client- Returns:
- the packet sender
-
send
public static void send(net.minecraft.server.level.ServerPlayer player, net.minecraft.network.protocol.common.custom.CustomPacketPayload payload) Sends a packet to a player.Any packets sent must be registered.
- Parameters:
player- the player to send the packet topayload- the payload to send
-
reconfigure
public static void reconfigure(net.minecraft.server.level.ServerPlayer player) Put the player back into configuration phase and re-run all of the configuration tasks.- Parameters:
player- the player
-
reconfigure
public static void reconfigure(net.minecraft.server.network.ServerGamePacketListenerImpl listener) Put the player back into configuration phase and re-run all of the configuration tasks.- Parameters:
listener- the packet listener
-