Class ServerConfigurationNetworking
Server-side networking functionalities include receiving serverbound packets, sending clientbound packets, and events related to server-side network handlers.
This class should be only used for the logical server.
See ServerPlayNetworking
for information on how to use the packet
object-based API.
See the documentation on each class for more information.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static interface
A thread-safe packet handler utilizingFabricPacket
. -
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
canSend
(ServerConfigurationNetworkHandler handler, PacketType<?> type) Checks if the connected client declared the ability to receive a specific type of packet.static boolean
canSend
(ServerConfigurationNetworkHandler handler, Identifier channelName) Checks if the connected client declared the ability to receive a packet on a specified channel name.static Packet
<ClientCommonPacketListener> createS2CPacket
(Identifier channelName, PacketByteBuf buf) Creates a packet which may be sent to a connected client.static <T extends FabricPacket>
Packet<ClientCommonPacketListener> createS2CPacket
(T packet) Creates a packet which may be sent to a connected client.static Set
<Identifier> Gets all channel names which global receivers are registered for.static Set
<Identifier> Gets all the channel names that the server can receive packets on.static Set
<Identifier> Gets all channel names that a connected client declared the ability to receive a packets on.static PacketSender
Gets the packet sender which sends packets to the connected client.static MinecraftServer
Returns the Minecraft Server of a server configuration network handler.static <T extends FabricPacket>
booleanregisterGlobalReceiver
(PacketType<T> type, ServerConfigurationNetworking.ConfigurationPacketHandler<T> handler) Registers a handler for a packet type.static boolean
registerGlobalReceiver
(Identifier channelName, ServerConfigurationNetworking.ConfigurationChannelHandler channelHandler) Registers a handler to a channel.static <T extends FabricPacket>
booleanregisterReceiver
(ServerConfigurationNetworkHandler networkHandler, PacketType<T> type, ServerConfigurationNetworking.ConfigurationPacketHandler<T> handler) Registers a handler for a packet type.static boolean
registerReceiver
(ServerConfigurationNetworkHandler networkHandler, Identifier channelName, ServerConfigurationNetworking.ConfigurationChannelHandler channelHandler) Registers a handler to a channel.static void
send
(ServerConfigurationNetworkHandler handler, Identifier channelName, PacketByteBuf buf) Sends a packet to a configuring player.static <T extends FabricPacket>
voidsend
(ServerConfigurationNetworkHandler handler, T packet) Sends a packet to a configuring player.static <T extends FabricPacket>
ServerConfigurationNetworking.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>
ServerConfigurationNetworking.ConfigurationPacketHandler<T> unregisterReceiver
(ServerConfigurationNetworkHandler networkHandler, PacketType<T> type) Removes the handler for a packet type.unregisterReceiver
(ServerConfigurationNetworkHandler networkHandler, Identifier channelName) Removes the handler of a channel.
-
Method Details
-
registerGlobalReceiver
public static boolean registerGlobalReceiver(Identifier channelName, ServerConfigurationNetworking.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, the server must be modified in the server 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. UseunregisterReceiver(ServerConfigurationNetworkHandler, 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, ServerConfigurationNetworking.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. UseunregisterReceiver(ServerConfigurationNetworkHandler, 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 ServerConfigurationNetworking.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> ServerConfigurationNetworking.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(ServerConfigurationNetworkHandler networkHandler, Identifier channelName, ServerConfigurationNetworking.ConfigurationChannelHandler channelHandler) Registers a handler to a channel. This method differs fromregisterGlobalReceiver(Identifier, ConfigurationChannelHandler)
since the channel handler will only be applied to the client represented by theServerConfigurationNetworkHandler
.The handler runs on the network thread. After reading the buffer there, the world must be modified in the server thread by calling
ThreadExecutor.execute(Runnable)
.For example, if you only register a receiver using this method when a ServerLoginNetworking.registerGlobalReceiver(Identifier, ServerLoginNetworking.LoginQueryResponseHandler) login response has been received, you should use
ServerPlayConnectionEvents.INIT
to register the channel handler.If a handler is already registered to the
channelName
, this method will returnfalse
, and no change will be made. UseunregisterReceiver(ServerConfigurationNetworkHandler, Identifier)
to unregister the existing handler.For new code,
registerReceiver(ServerConfigurationNetworkHandler, PacketType, ConfigurationPacketHandler)
is preferred, as it is designed in a way that prevents thread safety issues.- Parameters:
networkHandler
- the handlerchannelName
- the id of the channelchannelHandler
- the handler- Returns:
- false if a handler is already registered to the channel name
- See Also:
-
registerReceiver
public static <T extends FabricPacket> boolean registerReceiver(ServerConfigurationNetworkHandler networkHandler, PacketType<T> type, ServerConfigurationNetworking.ConfigurationPacketHandler<T> handler) Registers a handler for a packet type. This method differs fromregisterGlobalReceiver(PacketType, ConfigurationPacketHandler)
since the channel handler will only be applied to the client represented by theServerConfigurationNetworkHandler
.For example, if you only register a receiver using this method when a ServerLoginNetworking.registerGlobalReceiver(Identifier, ServerLoginNetworking.LoginQueryResponseHandler) login response has been received, you should use
ServerPlayConnectionEvents.INIT
to register the channel handler.If a handler is already registered for the
type
, this method will returnfalse
, and no change will be made. UseunregisterReceiver(ServerConfigurationNetworkHandler, PacketType)
to unregister the existing handler.- Parameters:
networkHandler
- the network handlertype
- the packet typehandler
- the handler- Returns:
false
if a handler is already registered to the channel name- See Also:
-
unregisterReceiver
@Nullable public static ServerConfigurationNetworking.ConfigurationChannelHandler unregisterReceiver(ServerConfigurationNetworkHandler networkHandler, Identifier channelName) 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 name
-
unregisterReceiver
@Nullable public static <T extends FabricPacket> ServerConfigurationNetworking.ConfigurationPacketHandler<T> unregisterReceiver(ServerConfigurationNetworkHandler networkHandler, 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 type of the packet- Returns:
- the previous handler, or
null
if no handler was bound to the channel, or it was not registered usingregisterReceiver(ServerConfigurationNetworkHandler, PacketType, ConfigurationPacketHandler)
-
getReceived
Gets all the channel names that the server can receive packets on.- Parameters:
handler
- the network handler- Returns:
- All the channel names that the server can receive packets on
-
getSendable
Gets all channel names that a connected client declared the ability to receive a packets on.- Parameters:
handler
- the network handler- Returns:
true
if the connected client has declared the ability to receive a packet on the specified channel
-
canSend
Checks if the connected client declared the ability to receive a packet on a specified channel name.- Parameters:
handler
- the network handlerchannelName
- the channel name- Returns:
true
if the connected client has declared the ability to receive a packet on the specified channel
-
canSend
Checks if the connected client declared the ability to receive a specific type of packet.- Parameters:
handler
- the network handlertype
- the packet type- Returns:
true
if the connected client has declared the ability to receive a specific type of packet
-
createS2CPacket
public static Packet<ClientCommonPacketListener> createS2CPacket(Identifier channelName, PacketByteBuf buf) Creates a packet which may be sent to a connected client.- Parameters:
channelName
- the channel namebuf
- the packet byte buf which represents the payload of the packet- Returns:
- a new packet
-
createS2CPacket
Creates a packet which may be sent to a connected client.- Parameters:
packet
- the fabric packet- Returns:
- a new packet
-
getSender
Gets the packet sender which sends packets to the connected client.- Parameters:
handler
- the network handler, representing the connection to the player/client- Returns:
- the packet sender
-
send
public static void send(ServerConfigurationNetworkHandler handler, Identifier channelName, PacketByteBuf buf) Sends a packet to a configuring player.- Parameters:
handler
- the handler to send the packet tochannelName
- the channel of the packetbuf
- the payload of the packet.
-
send
public static <T extends FabricPacket> void send(ServerConfigurationNetworkHandler handler, T packet) Sends a packet to a configuring player.- Parameters:
handler
- the network handler to send the packet topacket
- the packet
-
getServer
Returns the Minecraft Server of a server configuration network handler.- Parameters:
handler
- the server configuration network handler
-