Class NetworkRegistry
This registry is responsible for storing all known modded payloads, and for handling the negotiation of modded network channels between the client and the server.
Additionally, this registry is responsible for handling all packets that are not natively known once they arrive at the receiving end.
To prevent payloads from being send to a client that has no idea what to do with them, the registry provides endpoints for the vanilla code base to check if a packet can be send to a client.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final Map
<net.minecraft.resources.ResourceLocation, net.minecraft.network.codec.StreamCodec<net.minecraft.network.FriendlyByteBuf, ? extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>> Map of NeoForge payloads that may be sent before channel negotiation.private static final org.slf4j.Logger
private static final Map
<net.minecraft.network.ConnectionProtocol, Map<net.minecraft.resources.ResourceLocation, PayloadRegistration<?>>> Registry of all custom payload handlers.private static boolean
Declared common networking versions currently supported by NeoForge. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkCommonVersion
(ICommonPacketListener listener, CommonVersionPayload payload) Called when aCommonVersionPayload
is received.static void
checkPacket
(net.minecraft.network.protocol.Packet<?> packet, net.minecraft.network.protocol.common.ClientCommonPacketListener listener) Validates that aServerboundCustomPayloadPacket
may be sent to the server.static void
checkPacket
(net.minecraft.network.protocol.Packet<?> packet, net.minecraft.network.protocol.common.ServerCommonPacketListener listener) Validates that aClientboundCustomPayloadPacket
may be sent to the client.static void
configureMockConnection
(net.minecraft.network.Connection connection) Configures a mock connection for use in game tests.private static void
Used in place ofThread.dumpStack()
as that logs toSystem.err
.static <T extends net.minecraft.network.PacketListener>
List<net.minecraft.network.protocol.Packet<?>> filterGameBundlePackets
(io.netty.channel.ChannelHandlerContext context, Iterable<net.minecraft.network.protocol.Packet<? super T>> packets) Filters the given packets for a bundle packet in the game phase of the connection.static @Nullable net.minecraft.network.codec.StreamCodec
<? super net.minecraft.network.FriendlyByteBuf, ? extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> getCodec
(net.minecraft.resources.ResourceLocation id, net.minecraft.network.ConnectionProtocol protocol, net.minecraft.network.protocol.PacketFlow flow) Attempts to retrieve theStreamCodec
for a non-vanilla payload.static Set
<net.minecraft.resources.ResourceLocation> getCommonPlayChannels
(net.minecraft.network.protocol.PacketFlow flow) static ConnectionType
getConnectionType
(net.minecraft.network.Connection connection) static Set
<net.minecraft.resources.ResourceLocation> getInitialListeningChannels
(net.minecraft.network.protocol.PacketFlow flow) Returns the initial channels for the configuration phase..static Set
<net.minecraft.resources.ResourceLocation> static <T> CompletableFuture
<T> guard
(CompletableFuture<T> future, net.minecraft.resources.ResourceLocation payloadId) Helper to guard futures generated byIPayloadContext
against exceptions.static void
handleModdedPayload
(net.minecraft.network.protocol.common.ClientCommonPacketListener listener, net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket packet) Handles modded payloads on the client.static void
handleModdedPayload
(net.minecraft.network.protocol.common.ServerCommonPacketListener listener, net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket packet) Handles modded payloads on the server.static <T extends net.minecraft.network.PacketListener>
voidhandlePacketUnchecked
(net.minecraft.network.protocol.Packet<T> packet, net.minecraft.network.PacketListener listener) private static boolean
hasAdhocChannel
(net.minecraft.network.ConnectionProtocol protocol, net.minecraft.resources.ResourceLocation id, net.minecraft.network.protocol.PacketFlow flow) Checks if a given payload is ad-hoc readable, that is, may be read without a negotiated channel being present.static boolean
hasChannel
(net.minecraft.network.Connection connection, @Nullable net.minecraft.network.ConnectionProtocol protocol, net.minecraft.resources.ResourceLocation payloadId) Indicates whether the given connection has a connection setup that can transmit the given payload id.static boolean
hasChannel
(ICommonPacketListener listener, net.minecraft.resources.ResourceLocation payloadId) Checks if the packet listener's connection can send/receive the given payload.static void
initializeNeoForgeConnection
(net.minecraft.network.protocol.configuration.ClientConfigurationPacketListener listener, NetworkPayloadSetup setup) Invoked by the client to indicate that it detect a connection to a modded server, by receiving aModdedNetworkPayload
.static void
initializeNeoForgeConnection
(net.minecraft.network.protocol.configuration.ServerConfigurationPacketListener listener, Map<net.minecraft.network.ConnectionProtocol, Set<ModdedNetworkQueryComponent>> clientChannels) Invoked by the server when it completes the negotiation with the client during the configuration phase.static void
initializeOtherConnection
(net.minecraft.network.protocol.configuration.ClientConfigurationPacketListener listener) Invoked by the client when noModdedNetworkQueryPayload
has been received, but instead aBrandPayload
has been received as the first packet during negotiation in the configuration phase.static boolean
initializeOtherConnection
(net.minecraft.network.protocol.configuration.ServerConfigurationPacketListener listener) Invoked by theServerConfigurationPacketListenerImpl
when a vanilla or other connection is detected.static boolean
isModdedPayload
(net.minecraft.network.protocol.common.custom.CustomPacketPayload payload) Checks if a payload is a modded payload.static void
onCommonRegister
(ICommonPacketListener listener, CommonRegisterPayload payload) Replaces any existing common channels with the incoming ones from aCommonRegisterPayload
.static void
onConfigurationFinished
(ICommonPacketListener listener) Invoked when the configuration phase of a connection is completed.static void
onMinecraftRegister
(net.minecraft.network.Connection connection, Set<net.minecraft.resources.ResourceLocation> resourceLocations) Invoked to add to the known ad-hoc channels on a connection.static void
onMinecraftUnregister
(net.minecraft.network.Connection connection, Set<net.minecraft.resources.ResourceLocation> resourceLocations) Invoked to remove from the known ad-hoc channels on a connection.static void
onNetworkQuery
(net.minecraft.network.protocol.configuration.ClientConfigurationPacketListener listener) Invoked by the client when a modded server queries it for its available channels.static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload,
B extends net.minecraft.network.FriendlyByteBuf>
voidregister
(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, net.minecraft.network.codec.StreamCodec<? super B, T> codec, IPayloadHandler<T> handler, List<net.minecraft.network.ConnectionProtocol> protocols, Optional<net.minecraft.network.protocol.PacketFlow> flow, String version, boolean optional) Registers a new payload.static void
setup()
Sets up the network registry by firingRegisterPayloadHandlersEvent
, storing the resulting payload registrations inPAYLOAD_REGISTRATIONS
.
-
Field Details
-
SUPPORTED_COMMON_NETWORKING_VERSIONS
Declared common networking versions currently supported by NeoForge.At the time of writing, the only version in existence is 1.
-
LOGGER
private static final org.slf4j.Logger LOGGER -
BUILTIN_PAYLOADS
private static final Map<net.minecraft.resources.ResourceLocation,net.minecraft.network.codec.StreamCodec<net.minecraft.network.FriendlyByteBuf, BUILTIN_PAYLOADS? extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>> Map of NeoForge payloads that may be sent before channel negotiation. TODO: Separate by protocol + flow. -
PAYLOAD_REGISTRATIONS
private static final Map<net.minecraft.network.ConnectionProtocol,Map<net.minecraft.resources.ResourceLocation, PAYLOAD_REGISTRATIONSPayloadRegistration<?>>> Registry of all custom payload handlers. The initial state of this map should reflect the protocols which support custom payloads. TODO: Change key type to a combination of protocol + flow. -
setup
private static boolean setup
-
-
Constructor Details
-
NetworkRegistry
private NetworkRegistry()
-
-
Method Details
-
setup
public static void setup()Sets up the network registry by firingRegisterPayloadHandlersEvent
, storing the resulting payload registrations inPAYLOAD_REGISTRATIONS
. -
register
public static <T extends net.minecraft.network.protocol.common.custom.CustomPacketPayload,B extends net.minecraft.network.FriendlyByteBuf> void register(net.minecraft.network.protocol.common.custom.CustomPacketPayload.Type<T> type, net.minecraft.network.codec.StreamCodec<? super B, T> codec, IPayloadHandler<T> handler, List<net.minecraft.network.ConnectionProtocol> protocols, Optional<net.minecraft.network.protocol.PacketFlow> flow, String version, boolean optional) Registers a new payload.- Type Parameters:
T
- The class of the payload.B
- The class of the ByteBuf. Onlyplay
payloads may useRegistryFriendlyByteBuf
.- Parameters:
type
- The type of the payload.codec
- The codec for the payload.handler
- The handler for the payload. This handler should expect to receive the payload on all declared protocols and flows. It will be executed on the network thread.protocols
- The protocols this payload supports being sent over. Onlyconfiguration
andplay
are supported.flow
- The flow of this payload. SpecifyOptional.empty()
to support sending in both directions.version
- The version of the payload. Increase the payload version if the codec logic or handler logic changes. Neo-Neo connections with mismatched versions are denied.optional
- If the payload is optional. Any connection with missing non-optional payloads is denied.
-
getCodec
@Nullable public static @Nullable net.minecraft.network.codec.StreamCodec<? super net.minecraft.network.FriendlyByteBuf,? extends net.minecraft.network.protocol.common.custom.CustomPacketPayload> getCodec(net.minecraft.resources.ResourceLocation id, net.minecraft.network.ConnectionProtocol protocol, net.minecraft.network.protocol.PacketFlow flow) Attempts to retrieve theStreamCodec
for a non-vanilla payload.This method hardcodes NeoForge custom packets, stored in
BUILTIN_PAYLOADS
, which may be sent before negotiation.If none of the hardcoded matches succeed, we instead query the registered handlers.
The only validation this method performs is that the
PacketFlow
is correct. Other checks should be done externally.- Parameters:
id
- The id of the payload.protocol
- The protocol of the connection.flow
- The flow of the connection.- Returns:
- A codec for the payload, or null if the payload should be discarded on receipt.
-
isModdedPayload
public static boolean isModdedPayload(net.minecraft.network.protocol.common.custom.CustomPacketPayload payload) Checks if a payload is a modded payload. A modded payload is any payload that does not have `minecraft` as the domain, and is not a discarded payload.The special handling for
DiscardedPayload
is because it falsely reports its type as the type that failed to decode.- Returns:
- True if the payload is modded, and modded payload handling should be invoked for it.
-
handleModdedPayload
public static void handleModdedPayload(net.minecraft.network.protocol.common.ServerCommonPacketListener listener, net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket packet) Handles modded payloads on the server. Invoked after built-in handling.Called on the network thread.
- Parameters:
listener
- The listener which received the packet.packet
- The packet that was received.
-
handleModdedPayload
public static void handleModdedPayload(net.minecraft.network.protocol.common.ClientCommonPacketListener listener, net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket packet) Handles modded payloads on the client. Invoked after built-in handling.Called on the network thread.
- Parameters:
listener
- The listener which received the packet.packet
- The packet that was received.
-
initializeNeoForgeConnection
public static void initializeNeoForgeConnection(net.minecraft.network.protocol.configuration.ServerConfigurationPacketListener listener, Map<net.minecraft.network.ConnectionProtocol, Set<ModdedNetworkQueryComponent>> clientChannels) Invoked by the server when it completes the negotiation with the client during the configuration phase.This method determines what the versions of each of the channels are, and checks if the client and server have a compatible set of network channels.
If the negotiation fails, a custom packet is sent to the client to inform it of the failure, and which will allow the client to disconnect gracefully with an indicative error screen.
This method should only be invoked for modded connections. Use
initializeOtherConnection(ServerConfigurationPacketListener)
to indicate that during the configuration phase of the network handshake between a client and the server, a vanilla connection was detected.Invoked on the network thread.
- Parameters:
listener
- The listener which completed the negotiation.configuration
- The configuration channels that the client has available.play
- The play channels that the client has available.
-
initializeOtherConnection
public static boolean initializeOtherConnection(net.minecraft.network.protocol.configuration.ServerConfigurationPacketListener listener) Invoked by theServerConfigurationPacketListenerImpl
when a vanilla or other connection is detected.- Parameters:
listener
- The listener which detected the vanilla connection during the configuration phase.- Returns:
- True if the vanilla connection should be handled by the server, false otherwise.
-
checkPacket
public static void checkPacket(net.minecraft.network.protocol.Packet<?> packet, net.minecraft.network.protocol.common.ServerCommonPacketListener listener) Validates that aClientboundCustomPayloadPacket
may be sent to the client.- Parameters:
packet
- The packet that is about to be sent.listener
- The listener that wants to send the packet.- Throws:
UnsupportedOperationException
- if the packet may not be sent.
-
checkPacket
public static void checkPacket(net.minecraft.network.protocol.Packet<?> packet, net.minecraft.network.protocol.common.ClientCommonPacketListener listener) Validates that aServerboundCustomPayloadPacket
may be sent to the server.- Parameters:
packet
- The packet that is about to be sent.listener
- The listener that wants to send the packet.- Throws:
UnsupportedOperationException
- if the packet may not be sent.
-
hasAdhocChannel
private static boolean hasAdhocChannel(net.minecraft.network.ConnectionProtocol protocol, net.minecraft.resources.ResourceLocation id, net.minecraft.network.protocol.PacketFlow flow) Checks if a given payload is ad-hoc readable, that is, may be read without a negotiated channel being present.This is possible when an optional registration exists for the payload.
- Parameters:
id
- The id of the packet.flow
- The flow of the packet.- Returns:
- True if the packet is ad-hoc readable, false otherwise.
-
onNetworkQuery
public static void onNetworkQuery(net.minecraft.network.protocol.configuration.ClientConfigurationPacketListener listener) Invoked by the client when a modded server queries it for its available channels. The negotiation happens solely on the server side, and the result is later transmitted to the client.Invoked on the network thread.
- Parameters:
listener
- The listener which received the query.
-
initializeNeoForgeConnection
public static void initializeNeoForgeConnection(net.minecraft.network.protocol.configuration.ClientConfigurationPacketListener listener, NetworkPayloadSetup setup) Invoked by the client to indicate that it detect a connection to a modded server, by receiving aModdedNetworkPayload
. This will configure the active connection to the server to use the channels that were negotiated.Once this method completes a
NetworkPayloadSetup
will be present on the connection.Invoked on the network thread.
- Parameters:
listener
- The listener which received the payload.configuration
- The configuration channels that were negotiated.play
- The play channels that were negotiated.
-
initializeOtherConnection
public static void initializeOtherConnection(net.minecraft.network.protocol.configuration.ClientConfigurationPacketListener listener) Invoked by the client when noModdedNetworkQueryPayload
has been received, but instead aBrandPayload
has been received as the first packet during negotiation in the configuration phase.If this happens then the client will do a negotiation of its own internal channel configuration, to check if any mods are installed that require a modded connection to the server. If those are found then the connection is aborted and the client disconnects from the server.
This method should never be invoked on a connection where the server is
ConnectionType.NEOFORGE
.Invoked on the network thread.
- Parameters:
listener
- The listener which received the brand payload.
-
hasChannel
public static boolean hasChannel(ICommonPacketListener listener, net.minecraft.resources.ResourceLocation payloadId) Checks if the packet listener's connection can send/receive the given payload.- Parameters:
listener
- The listener to check.payloadId
- The payload id to check.- Returns:
- True if the listener has a connection setup that can transmit the given payload id, false otherwise.
-
hasChannel
public static boolean hasChannel(net.minecraft.network.Connection connection, @Nullable @Nullable net.minecraft.network.ConnectionProtocol protocol, net.minecraft.resources.ResourceLocation payloadId) Indicates whether the given connection has a connection setup that can transmit the given payload id.- Parameters:
connection
- The connection to check.protocol
- The protocol to check. Pass null to check against all protocols.payloadId
- The payload id to check.- Returns:
- True if the connection has a connection setup that can transmit the given payload id, false otherwise.
-
filterGameBundlePackets
public static <T extends net.minecraft.network.PacketListener> List<net.minecraft.network.protocol.Packet<?>> filterGameBundlePackets(io.netty.channel.ChannelHandlerContext context, Iterable<net.minecraft.network.protocol.Packet<? super T>> packets) Filters the given packets for a bundle packet in the game phase of the connection.- Type Parameters:
T
- The type of the listener.- Parameters:
context
- The context of the connection.packets
- The packets to filter.- Returns:
- The filtered packets.
-
configureMockConnection
@VisibleForTesting public static void configureMockConnection(net.minecraft.network.Connection connection) Configures a mock connection for use in game tests. The mock connection will act as if the server and client are fully compatible and both NeoForge.- Parameters:
connection
- The connection to configure.
-
onMinecraftRegister
public static void onMinecraftRegister(net.minecraft.network.Connection connection, Set<net.minecraft.resources.ResourceLocation> resourceLocations) Invoked to add to the known ad-hoc channels on a connection.Invoked on the network thread.
- Parameters:
connection
- The connection to add the channels to.resourceLocations
- The resource locations to add.
-
onMinecraftUnregister
public static void onMinecraftUnregister(net.minecraft.network.Connection connection, Set<net.minecraft.resources.ResourceLocation> resourceLocations) Invoked to remove from the known ad-hoc channels on a connection.Invoked on the network thread.
- Parameters:
connection
- The connection to remove the channels from.resourceLocations
- The resource locations to remove.
-
getInitialListeningChannels
public static Set<net.minecraft.resources.ResourceLocation> getInitialListeningChannels(net.minecraft.network.protocol.PacketFlow flow) Returns the initial channels for the configuration phase..- Returns:
- the initial channels for the configuration phase.
-
getInitialServerUnregisterChannels
-
checkCommonVersion
Called when aCommonVersionPayload
is received. Triggers a disconnect if none of the supplied version match our supported ones. Since we only support one version, we don't need to do further handling or record the "active" version just yet.Invoked on the network thread.
- Parameters:
listener
- The receiving listener.payload
- The incoming version payload.
-
onCommonRegister
Replaces any existing common channels with the incoming ones from aCommonRegisterPayload
.Invoked on the network thread.
- Parameters:
listener
- The receiving listener.payload
- The incoming register payload.
-
getCommonPlayChannels
public static Set<net.minecraft.resources.ResourceLocation> getCommonPlayChannels(net.minecraft.network.protocol.PacketFlow flow) -
onConfigurationFinished
Invoked when the configuration phase of a connection is completed.Updates the ad-hoc channels to prepare for the game phase by removing the initial channels and building a new list based on the connection type.
- Parameters:
listener
-
-
getConnectionType
-
guard
public static <T> CompletableFuture<T> guard(CompletableFuture<T> future, net.minecraft.resources.ResourceLocation payloadId) Helper to guard futures generated byIPayloadContext
against exceptions. -
handlePacketUnchecked
public static <T extends net.minecraft.network.PacketListener> void handlePacketUnchecked(net.minecraft.network.protocol.Packet<T> packet, net.minecraft.network.PacketListener listener) -
dumpStackToLog
private static void dumpStackToLog()Used in place ofThread.dumpStack()
as that logs toSystem.err
.
-