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
(net.minecraft.util.Identifier channelName) Checks if the connected server declared the ability to receive a packet on a specified channel name.static net.minecraft.network.Packet<?>
createC2SPacket
(net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf) Creates a packet which may be sent to a the connected server.static Set<net.minecraft.util.Identifier>
Gets all channel names which global receivers are registered for.static Set<net.minecraft.util.Identifier>
Gets all the channel names that the client can receive packets on.static Set<net.minecraft.util.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
(net.minecraft.util.Identifier channelName, ClientPlayNetworking.PlayChannelHandler channelHandler) Registers a handler to a channel.static boolean
registerReceiver
(net.minecraft.util.Identifier channelName, ClientPlayNetworking.PlayChannelHandler channelHandler) Registers a handler to a channel.static void
send
(net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf) Sends a packet to the connected server.static @Nullable ClientPlayNetworking.PlayChannelHandler
unregisterGlobalReceiver
(net.minecraft.util.Identifier channelName) Removes the handler of a channel.static @Nullable ClientPlayNetworking.PlayChannelHandler
unregisterReceiver
(net.minecraft.util.Identifier channelName) Removes the handler of a channel.
-
Method Details
-
registerGlobalReceiver
public static boolean registerGlobalReceiver(net.minecraft.util.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(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
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.util.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(net.minecraft.util.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
public static boolean canSend(net.minecraft.util.Identifier channelName) throws IllegalArgumentException 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 net.minecraft.network.Packet<?> createC2SPacket(net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf) Creates a packet which may be sent to a 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
public static void send(net.minecraft.util.Identifier channelName, net.minecraft.network.PacketByteBuf buf) throws IllegalStateException 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
-