Class ClientConfigurationNetworking
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 ServerPlayNetworking
for information on how to use the packet
object-based API.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static interface
A thread-safe packet handler utilizingFabricPacket
. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canSend
(PacketType<?> type) Checks if the connected server declared the ability to receive a packet on a specified channel name.static boolean
canSend
(Identifier channelName) Checks if the connected server declared the ability to receive a packet on a specified channel name.static Packet
<ServerCommonPacketListener> 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 <T extends FabricPacket>
booleanregisterGlobalReceiver
(PacketType<T> type, ClientConfigurationNetworking.ConfigurationPacketHandler<T> handler) Registers a handler for a packet type.static boolean
registerGlobalReceiver
(Identifier channelName, ClientConfigurationNetworking.ConfigurationChannelHandler channelHandler) Registers a handler to a channel.static <T extends FabricPacket>
booleanregisterReceiver
(PacketType<T> type, ClientConfigurationNetworking.ConfigurationPacketHandler<T> handler) Registers a handler for a packet type.static boolean
registerReceiver
(Identifier channelName, ClientConfigurationNetworking.ConfigurationChannelHandler channelHandler) Registers a handler to a channel.static void
send
(Identifier channelName, PacketByteBuf buf) Sends a packet to the connected server.static <T extends FabricPacket>
voidsend
(T packet) Sends a packet to the connected server.static <T extends FabricPacket>
ClientConfigurationNetworking.ConfigurationPacketHandler<T> unregisterGlobalReceiver
(PacketType<T> type) Removes the handler for a packet type.unregisterGlobalReceiver
(Identifier channelName) Removes the handler of a channel.static <T extends FabricPacket>
ClientConfigurationNetworking.ConfigurationPacketHandler<T> unregisterReceiver
(PacketType<T> type) Removes the handler for a packet type.unregisterReceiver
(Identifier channelName) Removes the handler of a channel.
-
Method Details
-
registerGlobalReceiver
public static boolean registerGlobalReceiver(Identifier channelName, ClientConfigurationNetworking.ConfigurationChannelHandler channelHandler) Registers a handler to a channel. A global receiver is registered to all connections, in the present and future.The handler runs on the network thread. After reading the buffer there, access to game state must be performed in the render thread by calling
ThreadExecutor.execute(Runnable)
.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.For new code,
registerGlobalReceiver(PacketType, ConfigurationPacketHandler)
is preferred, as it is designed in a way that prevents thread safety issues.- Parameters:
channelName
- the id of the channelchannelHandler
- the handler- Returns:
- false if a handler is already registered to the channel
- See Also:
-
registerGlobalReceiver
public static <T extends FabricPacket> boolean registerGlobalReceiver(PacketType<T> type, ClientConfigurationNetworking.ConfigurationPacketHandler<T> handler) Registers a handler for a packet 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(PacketType)
to unregister the existing handler.- Parameters:
type
- the packet typehandler
- the handler- Returns:
- false if a handler is already registered to the channel
- See Also:
-
unregisterGlobalReceiver
@Nullable public static ClientConfigurationNetworking.ConfigurationChannelHandler 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:
-
unregisterGlobalReceiver
@Nullable public static <T extends FabricPacket> ClientConfigurationNetworking.ConfigurationPacketHandler<T> unregisterGlobalReceiver(PacketType<T> type) Removes the handler for a packet type. A global receiver is registered to all connections, in the present and future.The
type
is guaranteed not to have an associated handler after this call.- Parameters:
type
- the packet type- Returns:
- the previous handler, or
null
if no handler was bound to the channel, or it was not registered usingregisterGlobalReceiver(PacketType, ConfigurationPacketHandler)
- 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, ClientConfigurationNetworking.ConfigurationChannelHandler 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.For new code,
registerReceiver(PacketType, ConfigurationPacketHandler)
is preferred, as it is designed in a way that prevents thread safety issues.- 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:
-
registerReceiver
public static <T extends FabricPacket> boolean registerReceiver(PacketType<T> type, ClientConfigurationNetworking.ConfigurationPacketHandler<T> handler) Registers a handler for a packet type.If a handler is already registered for the
type
, this method will returnfalse
, and no change will be made. UseunregisterReceiver(PacketType)
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:
type
- the packet typehandler
- the handler- Returns:
false
if a handler is already registered for the type- Throws:
IllegalStateException
- if the client is not connected to a server- See Also:
-
unregisterReceiver
@Nullable public static ClientConfigurationNetworking.ConfigurationChannelHandler 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
-
unregisterReceiver
@Nullable public static <T extends FabricPacket> ClientConfigurationNetworking.ConfigurationPacketHandler<T> unregisterReceiver(PacketType<T> type) Removes the handler for a packet type.The
type
is guaranteed not to have an associated handler after this call.- Parameters:
type
- the packet type- Returns:
- the previous handler, or
null
if no handler was bound to the channel, or it was not registered usingregisterReceiver(PacketType, ConfigurationPacketHandler)
- 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
-
canSend
Checks if the connected server declared the ability to receive a packet on a specified channel name. This returnsfalse
if the client is not in game.- Parameters:
type
- the packet type- Returns:
true
if the connected server has declared the ability to receive a packet on the specified channel
-
createC2SPacket
public static Packet<ServerCommonPacketListener> 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
-
send
Sends a packet to the connected server.- Parameters:
packet
- the packet- Throws:
IllegalStateException
- if the client is not connected to a server
-