Class ClientConfigurationNetworking
Client-side networking functionalities include receiving clientbound packets,
sending serverbound packets, and events related to client-side network handlers.
Packets received by this class must be registered to PayloadTypeRegistry.configurationS2C()
on both ends.
Packets sent by this class must be registered to PayloadTypeRegistry.configurationC2S()
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 packet
object-based API.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
A packet handler utilizingCustomPayload
.static interface
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canSend
(CustomPayload.Id<?> 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 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 CustomPayload>
booleanregisterGlobalReceiver
(CustomPayload.Id<T> type, ClientConfigurationNetworking.ConfigurationPayloadHandler<T> handler) Registers a handler for a packet type.static <T extends CustomPayload>
booleanregisterReceiver
(CustomPayload.Id<T> id, ClientConfigurationNetworking.ConfigurationPayloadHandler<T> handler) Registers a handler for a packet type.static void
send
(CustomPayload payload) Sends a packet to the connected server.Removes the handler for a packet type.Removes the handler for a packet type.
-
Method Details
-
registerGlobalReceiver
public static <T extends CustomPayload> boolean registerGlobalReceiver(CustomPayload.Id<T> type, ClientConfigurationNetworking.ConfigurationPayloadHandler<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(CustomPayload.Id)
to unregister the existing handler.- Parameters:
type
- the packet typehandler
- the handler- Returns:
- false if a handler is already registered to the channel
- Throws:
IllegalArgumentException
- if the codec fortype
has not been registered yet- See Also:
-
unregisterGlobalReceiver
@Nullable public static ClientConfigurationNetworking.ConfigurationPayloadHandler<?> unregisterGlobalReceiver(CustomPayload.Id<?> id) 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:
id
- the packet id- Returns:
- the previous handler, or
null
if no handler was bound to the channel, or it was not registered usingregisterGlobalReceiver(CustomPayload.Id, ConfigurationPayloadHandler)
- 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 CustomPayload> boolean registerReceiver(CustomPayload.Id<T> id, ClientConfigurationNetworking.ConfigurationPayloadHandler<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(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:
id
- the payload idhandler
- the handler- Returns:
false
if a handler is already registered for the type- Throws:
IllegalArgumentException
- if the codec fortype
has not been registered yetIllegalStateException
- if the client is not connected to a server- See Also:
-
unregisterReceiver
@Nullable public static ClientConfigurationNetworking.ConfigurationPayloadHandler<?> unregisterReceiver(Identifier id) Removes the handler for a packet type.The
type
is guaranteed not to have an associated handler after this call.- Parameters:
id
- the payload id to unregister- Returns:
- the previous handler, or
null
if no handler was bound to the channel, or it was not registered usingregisterReceiver(CustomPayload.Id, ConfigurationPayloadHandler)
- 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
-
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.Any packets sent must be registered.
- Parameters:
payload
- to be sent- Throws:
IllegalStateException
- if the client is not connected to a server
-