Class NetworkRegistry

java.lang.Object
net.neoforged.neoforge.network.registration.NetworkRegistry

@Internal public class NetworkRegistry extends Object
Defines the registry for all modded network packets.

This registry is responsible for storing all known modded network packets, and for handling the negotiation of modded network packets between the client and the server.

Additionally, this registry is responsible for handling all modded network 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 Details

    • LOGGER

      private static final org.slf4j.Logger LOGGER
    • ATTRIBUTE_PAYLOAD_SETUP

      private static final io.netty.util.AttributeKey<NetworkPayloadSetup> ATTRIBUTE_PAYLOAD_SETUP
    • ATTRIBUTE_ADHOC_CHANNELS

      private static final io.netty.util.AttributeKey<Set<ResourceLocation>> ATTRIBUTE_ADHOC_CHANNELS
    • ATTRIBUTE_CONNECTION_TYPE

      private static final io.netty.util.AttributeKey<ConnectionType> ATTRIBUTE_CONNECTION_TYPE
    • ATTRIBUTE_FLOW

      private static final io.netty.util.AttributeKey<PacketFlow> ATTRIBUTE_FLOW
    • INSTANCE

      private static final NetworkRegistry INSTANCE
    • setup

      private boolean setup
    • knownConfigurationRegistrations

      private final Map<ResourceLocation,ConfigurationRegistration<?>> knownConfigurationRegistrations
    • knownPlayRegistrations

      private final Map<ResourceLocation,PlayRegistration<?>> knownPlayRegistrations
  • Constructor Details

    • NetworkRegistry

      private NetworkRegistry()
  • Method Details

    • getInstance

      public static NetworkRegistry getInstance()
    • setup

      public void setup()
      Invoked to initially set up the registry.

      This fires an event on the mod bus to allow mods to register their custom packets. And then stores the registered packets in the registry.

      This method can only be invoked once.

    • getReader

      @Nullable public FriendlyByteBuf.Reader<? extends CustomPacketPayload> getReader(ResourceLocation id, io.netty.channel.ChannelHandlerContext context, ConnectionProtocol protocol, Map<ResourceLocation,FriendlyByteBuf.Reader<? extends CustomPacketPayload>> knownTypes)
      Invoked by the network subsystem to get a reader for a custom packet payload.

      This method special cases three situations:

      • Vanilla custom packets, they are defined as "known packets" and if the payload id matches the known vanilla reader is returned
      • ModdedNetworkQueryPayload, it has a hardcoded id check, since it can be used before a network setup exists.
      • ModdedNetworkPayload, it also has a hardcoded id check, since it can be used before a network setup exists, as well.
      • ModdedNetworkSetupFailedPayload, it also has a hardcoded id check, since it can be used before a network setup exists, as well.

      This method will then check if the connection is properly configured to be used for modded packets. If not a warning is logged, and null is returned. Which causes the packet to be discarded.

      If the connection is properly configured, the method will check if the packet is known to the connection, and if it is not, null is returned. Then the method will check if the packet is known to the registry, and if it is not, null is returned. If the packet is known to the registry, the method will return a reader that will invoke the registered replyHandler.

      Parameters:
      id - The id of the payload.
      context - The context of the channel.
      protocol - The protocol of the connection.
      knownTypes - The known types of the connection.
      Returns:
      A reader for the payload, or null if the payload should be discarded.
    • onModdedPacketAtServer

      public void onModdedPacketAtServer(ServerCommonPacketListener listener, ServerboundCustomPayloadPacket packet)
      Invoked by a ServerCommonPacketListener when a modded packet is received on a modded connection that is not natively known to the vanilla code base.

      The method will first validate that a proper modded connection is setup and that a NetworkPayloadSetup is present on the connection. If that is not the case a warning is logged, and the client is disconnected with a generic error message.

      If the connection is setup properly, the method will check if the packet is known to the connection, and if it is not, the client is disconnected. Then checks are executed against the stored known packet handlers to see if the packet is known to the server. Technically, this list is considered fixed, based on the fact that the registration event is only fired once during bootstrap, so in practice this is just a safe-guard against people messing with the registration map. Once that completes the registered replyHandler is invoked.

      Parameters:
      listener - The listener which received the packet.
      packet - The packet that was received.
    • onModdedPacketAtClient

      public boolean onModdedPacketAtClient(ClientCommonPacketListener listener, ClientboundCustomPayloadPacket packet)
      Invoked by a ClientCommonPacketListener when a modded packet is received on a modded connection that is not natively known to the vanilla code base.

      The method will first validate that a proper modded connection is setup and that a NetworkPayloadSetup is present on the connection. If that is not the case a warning is logged, and the client is disconnected with a generic error message.

      If the connection is setup properly, the method will check if the packet is known to the connection, and if it is not, the client is disconnected. Then checks are executed against the stored known packet handlers to see if the packet is known to the client. Technically, this list is considered fixed, based on the fact that the registration event is only fired once during bootstrap, so in practice this is just a safe-guard against people messing with the registration map. Once that completes the registered replyHandler is invoked.

      Parameters:
      listener - The listener which received the packet.
      packet - The packet that was received.
    • onModdedConnectionDetectedAtServer

      public void onModdedConnectionDetectedAtServer(ServerConfigurationPacketListener sender, Set<ModdedNetworkQueryComponent> configuration, Set<ModdedNetworkQueryComponent> play)
      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 send 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 onVanillaOrOtherConnectionDetectedAtServer(ServerConfigurationPacketListener) to indicate that during the configuration phase of the network handshake between a client and the server, a vanilla connection was detected.

      Parameters:
      sender - The listener which completed the negotiation.
      configuration - The configuration channels that the client has available.
      play - The play channels that the client has available.
    • onVanillaOrOtherConnectionDetectedAtServer

      public boolean onVanillaOrOtherConnectionDetectedAtServer(ServerConfigurationPacketListener sender)
      Invoked by the ServerConfigurationPacketListenerImpl when a vanilla or other connection is detected.
      Parameters:
      sender - 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.
    • canSendPacket

      public boolean canSendPacket(Packet<?> packet, ServerCommonPacketListener listener)
      Indicates if the given packet can be sent via the given listener.

      This method is invoked by the vanilla code base to check if any packet can be sent to a client. It will always return true for a packet that is not a ClientboundCustomPayloadPacket. For a custom payload packet, it will check if the packet is known to the client, and if it is not, it will return false.

      If this method is invoked before the negotiation during the configuration phase has completed, and as such no NetworkPayloadSetup is present then it will only allow ModdedNetworkQueryPayload packets to be sent.

      Parameters:
      packet - The packet that is about to be sent.
      listener - The listener that wants to send the packet.
      Returns:
      True if the packet can be sent, false otherwise.
    • shouldSendPacketRaw

      public boolean shouldSendPacketRaw(Packet<?> packet)
    • canSendPacket

      public boolean canSendPacket(Packet<?> packet, ClientCommonPacketListener listener)
      Indicates if the given packet can be sent via the given listener.

      This method is invoked by the vanilla code base to check if any packet can be sent to a server. It will always return true for a packet that is not a ServerboundCustomPayloadPacket. For a custom payload packet, it will check if the packet is known to the server, and if it is not, it will return false.

      If this method is invoked before the negotiation during the configuration phase has completed, and as such no NetworkPayloadSetup is present then it will only allow ModdedNetworkQueryPayload packets to be sent.

      Parameters:
      packet - The packet that is about to be sent.
      listener - The listener that wants to send the packet.
      Returns:
      True if the packet can be sent, false otherwise.
    • getKnownAdHocChannelsOfOtherEnd

      private Set<ResourceLocation> getKnownAdHocChannelsOfOtherEnd(Connection connection)
      Returns a mutable map of the currently known ad-hoc channels.
    • isAdhocConfigurationChannelReadable

      private boolean isAdhocConfigurationChannelReadable(ResourceLocation id, PacketFlow flow)
      Indicates if the given packet is ad hoc readable for us.
      Parameters:
      id - The id of the packet.
      flow - The flow of the packet.
      Returns:
      True if the packet is ad hoc readable, false otherwise.
    • isAdhocPlayChannelReadable

      private boolean isAdhocPlayChannelReadable(ResourceLocation id, PacketFlow flow)
      Indicates if the given packet is ad hoc readable for us.
      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 void onNetworkQuery(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.
      Parameters:
      listener - The listener which received the query.
    • onModdedNetworkConnectionEstablished

      public void onModdedNetworkConnectionEstablished(ClientConfigurationPacketListener listener, Set<ModdedNetworkComponent> configuration, Set<ModdedNetworkComponent> play)
      Invoked by the client to indicate that it detect a connection to a modded server, by receiving a ModdedNetworkPayload. 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.

      Parameters:
      listener - The listener which received the payload.
      configuration - The configuration channels that were negotiated.
      play - The play channels that were negotiated.
    • onVanillaNetworkConnectionEstablished

      public boolean onVanillaNetworkConnectionEstablished(ClientConfigurationPacketListener sender)
      Invoked by the client when no ModdedNetworkQueryPayload has been received, but instead a BrandPayload 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 mods are installed which need 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 serverside is a modded environment.

      Parameters:
      sender - The listener which received the brand payload.
      Returns:
      True if the vanilla connection should be handled by the client, false otherwise.
    • isConnected

      public boolean isConnected(ServerCommonPacketListener listener, ResourceLocation payloadId)
      Indicates whether the server listener has a connection setup that can transmit the given payload id.
      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.
    • isConnected

      public boolean isConnected(ClientCommonPacketListener listener, ResourceLocation payloadId)
      Indicates whether the client listener has a connection setup that can transmit the given payload id.
      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.
    • isConnected

      public boolean isConnected(Connection connection, ConnectionPhase connectionPhase, ResourceLocation payloadId)
      Indicates whether the given connection has a connection setup that can transmit the given payload id.
      Parameters:
      connection - The connection to check.
      connectionPhase - The phase of the connection to check.
      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 <T extends PacketListener> List<Packet<?>> filterGameBundlePackets(io.netty.channel.ChannelHandlerContext context, Iterable<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

      public void configureMockConnection(Connection connection)
      Configures a mock connection.
      Parameters:
      connection - The connection to configure.
    • onMinecraftRegister

      public void onMinecraftRegister(ClientCommonPacketListener listener, Set<ResourceLocation> resourceLocations)
      Invoked by the ClientCommonPacketListener when a dinnerbone protocol registration payload is received.
      Parameters:
      listener - The listener which received the payload.
      resourceLocations - The resource locations that were registered.
    • onMinecraftRegister

      public void onMinecraftRegister(ServerCommonPacketListener listener, Set<ResourceLocation> resourceLocations)
      Invoked by the ServerCommonPacketListener when a dinnerbone protocol registration payload is received.
      Parameters:
      listener - The listener which received the payload.
      resourceLocations - The resource locations that were registered.
    • onMinecraftRegister

      private void onMinecraftRegister(Set<ResourceLocation> resourceLocations, Connection connection)
      Invoked to add to the known ad-hoc channels on a connection.
      Parameters:
      resourceLocations - The resource locations to add.
      connection - The connection to add the channels to.
    • onMinecraftUnregister

      public void onMinecraftUnregister(ClientCommonPacketListener listener, Set<ResourceLocation> resourceLocations)
      Invoked by the ClientCommonPacketListener when a dinnerbone protocol unregistration payload is received.
      Parameters:
      listener - The listener which received the payload.
      resourceLocations - The resource locations that were unregistered.
    • onMinecraftUnregister

      public void onMinecraftUnregister(ServerCommonPacketListener listener, Set<ResourceLocation> resourceLocations)
      Invoked by the ServerCommonPacketListener when a dinnerbone protocol unregistration payload is received.
      Parameters:
      listener - The listener which received the payload.
      resourceLocations - The resource locations that were unregistered.
    • onMinecraftUnregister

      private void onMinecraftUnregister(Set<ResourceLocation> resourceLocations, Connection connection)
      Invoked to remove from the known ad-hoc channels on a connection.
      Parameters:
      resourceLocations - The resource locations to remove.
      connection - The connection to remove the channels from.
    • getInitialServerListeningChannels

      public Set<ResourceLocation> getInitialServerListeningChannels()
      Returns the initial channels that the server listens on during the configuration phase..
      Returns:
      the initial channels that the server listens on during the configuration phase.
    • getInitialServerUnregisterChannels

      public Set<ResourceLocation> getInitialServerUnregisterChannels()
    • getInitialClientListeningChannels

      private static Set<ResourceLocation> getInitialClientListeningChannels()
    • onConfigurationFinished

      public void onConfigurationFinished(ServerConfigurationPacketListener serverConfigurationPacketListener)
    • onConfigurationFinished

      public void onConfigurationFinished(ClientConfigurationPacketListener listener)