Class ClientPlayNetworking
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 ClassesModifier and TypeClassDescriptionstatic interfacestatic interfaceClientPlayNetworking.PlayPayloadHandler<T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>A thread-safe payload handler utilizingCustomPacketPayload. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancanSend(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 booleancanSend(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> createServerboundPacket(T packet) 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.static PacketSenderGets the payload sender which sends packets to the connected server.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>
booleanregisterGlobalReceiver(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>
booleanregisterReceiver(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, ClientPlayNetworking.PlayPayloadHandler<T> handler) Registers a handler for a payload type.static voidsend(net.minecraft.network.protocol.common.custom.CustomPacketPayload payload) Sends a payload to the connected server.static @Nullable ClientPlayNetworking.PlayPayloadHandler<?> unregisterGlobalReceiver(net.minecraft.resources.Identifier id) Removes the handler for a payload type.static @Nullable ClientPlayNetworking.PlayPayloadHandler<?> unregisterReceiver(net.minecraft.resources.Identifier id) Removes the handler for a payload id.
-
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 returnfalse, and no change will be made. UseunregisterGlobalReceiver(Identifier)to unregister the existing handler.- Parameters:
type- the payload typehandler- the handler- Returns:
- false if a handler is already registered to the channel
- Throws:
IllegalArgumentException- if the codec fortypehas 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
typeis 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.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 returnfalse, and no change will be made. UseunregisterReceiver(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.INITto register the channel handler.- Parameters:
type- the payload typehandler- the handler- Returns:
falseif a handler is already registered for the type- Throws:
IllegalArgumentException- if the codec fortypehas not been registered yetIllegalStateException- 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
typeis 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 usingregisterReceiver(CustomPacketPayload.Type, PlayPayloadHandler) - Throws:
IllegalStateException- if the client is not connected to a server
-
getReceived
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
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:
trueif 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 returnsfalseif the client is not in game.- Parameters:
type- the payload type- Returns:
trueif 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
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
-