Class ClientPlayNetworking
Client-side networking functionalities include receiving clientbound packets, sending serverbound packets, and events related to client-side network handlers.
This class should be only used on the physical client and for the logical client.
- See Also:
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canSend
(Identifier channelName) Checks if the connected server declared the ability to receive a packet on a specified channel name.static Packet<ServerPlayPacketListener>
createC2SPacket
(Identifier channelName, PacketByteBuf buf) Creates a packet which may be sent to the connected server.static Set<Identifier>
Gets all channel names which global receivers are registered for.static Set<Identifier>
Gets all the channel names that the client can receive packets on.static Set<Identifier>
Gets all channel names that the connected server declared the ability to receive a packets on.static PacketSender
Gets the packet sender which sends packets to the connected server.static boolean
registerGlobalReceiver
(Identifier channelName, ClientPlayNetworking.PlayChannelHandler channelHandler) Registers a handler to a channel.static boolean
registerReceiver
(Identifier channelName, ClientPlayNetworking.PlayChannelHandler channelHandler) Registers a handler to a channel.static void
send
(Identifier channelName, PacketByteBuf buf) Sends a packet to the connected server.unregisterGlobalReceiver
(Identifier channelName) Removes the handler of a channel.unregisterReceiver
(Identifier channelName) Removes the handler of a channel.
-
Method Details
-
registerGlobalReceiver
public static boolean registerGlobalReceiver(Identifier channelName, ClientPlayNetworking.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 returnfalse
, and no change will be made. UseunregisterGlobalReceiver(Identifier)
to unregister the existing handler.- Parameters:
channelName
- the id of the channelchannelHandler
- the handler- Returns:
- false if a handler is already registered to the channel
- See Also:
-
unregisterGlobalReceiver
@Nullable public static @Nullable ClientPlayNetworking.PlayChannelHandler unregisterGlobalReceiver(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
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(Identifier channelName, ClientPlayNetworking.PlayChannelHandler channelHandler) Registers a handler to a channel.If a handler is already registered to the
channel
, 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.INIT
to register the channel handler.- Parameters:
channelName
- the id of the channel- Returns:
- false if a handler is already registered to the channel
- Throws:
IllegalStateException
- if the client is not connected to a server- See Also:
-
unregisterReceiver
@Nullable public static @Nullable ClientPlayNetworking.PlayChannelHandler unregisterReceiver(Identifier channelName) throws IllegalStateException 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 - 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
Checks if the connected server declared the ability to receive a packet on a specified channel name.- Parameters:
channelName
- the channel name- Returns:
- True if the connected server has declared the ability to receive a packet on the specified channel. False if the client is not in game.
- Throws:
IllegalArgumentException
-
createC2SPacket
public static Packet<ServerPlayPacketListener> createC2SPacket(Identifier channelName, PacketByteBuf buf) Creates a packet which may be sent to the connected server.- Parameters:
channelName
- the channel namebuf
- the packet byte buf which represents the payload of the packet- Returns:
- a new packet
-
getSender
Gets the packet sender which sends packets to the connected server.- Returns:
- the client's packet sender
- Throws:
IllegalStateException
- if the client is not connected to a server
-
send
Sends a packet to the connected server.- Parameters:
channelName
- the channel of the packetbuf
- the payload of the packet- Throws:
IllegalStateException
- if the client is not connected to a server
-