Class NetworkRegistry

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

@Internal public class NetworkRegistry extends Object
Core registry for all modded networking.

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

    Fields
    Modifier and Type
    Field
    Description
    private 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
     
    static final List<Integer>
    Declared common networking versions currently supported by NeoForge.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Called when a CommonVersionPayload is received.
    static void
    checkPacket(net.minecraft.network.protocol.Packet<?> packet, net.minecraft.network.protocol.common.ClientCommonPacketListener listener)
    Validates that a ServerboundCustomPayloadPacket may be sent to the server.
    static void
    checkPacket(net.minecraft.network.protocol.Packet<?> packet, net.minecraft.network.protocol.common.ServerCommonPacketListener listener)
    Validates that a ClientboundCustomPayloadPacket 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 of Thread.dumpStack() as that logs to System.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 the StreamCodec for a non-vanilla payload.
    static Set<net.minecraft.resources.ResourceLocation>
    getCommonPlayChannels(net.minecraft.network.protocol.PacketFlow flow)
     
    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 by IPayloadContext 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>
    void
    handlePacketUnchecked(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 a ModdedNetworkPayload.
    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 no ModdedNetworkQueryPayload has been received, but instead a BrandPayload 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 the ServerConfigurationPacketListenerImpl 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
    Replaces any existing common channels with the incoming ones from a CommonRegisterPayload.
    static void
    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>
    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.
    static void
    Sets up the network registry by firing RegisterPayloadHandlersEvent, storing the resulting payload registrations in PAYLOAD_REGISTRATIONS.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SUPPORTED_COMMON_NETWORKING_VERSIONS

      public static final List<Integer> 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,? extends net.minecraft.network.protocol.common.custom.CustomPacketPayload>> BUILTIN_PAYLOADS
      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,PayloadRegistration<?>>> PAYLOAD_REGISTRATIONS
      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 firing RegisterPayloadHandlersEvent, storing the resulting payload registrations in PAYLOAD_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. Only play payloads may use RegistryFriendlyByteBuf.
      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. Only configuration and play are supported.
      flow - The flow of this payload. Specify Optional.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 the StreamCodec 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 the ServerConfigurationPacketListenerImpl 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 a ClientboundCustomPayloadPacket 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 a ServerboundCustomPayloadPacket 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 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.

      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 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 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

      public static Set<net.minecraft.resources.ResourceLocation> getInitialServerUnregisterChannels()
    • checkCommonVersion

      public static void checkCommonVersion(ICommonPacketListener listener, CommonVersionPayload payload)
      Called when a CommonVersionPayload 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

      public static void onCommonRegister(ICommonPacketListener listener, CommonRegisterPayload payload)
      Replaces any existing common channels with the incoming ones from a CommonRegisterPayload.

      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

      public static void onConfigurationFinished(ICommonPacketListener listener)
      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

      public static ConnectionType getConnectionType(net.minecraft.network.Connection connection)
    • guard

      public static <T> CompletableFuture<T> guard(CompletableFuture<T> future, net.minecraft.resources.ResourceLocation payloadId)
      Helper to guard futures generated by IPayloadContext 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 of Thread.dumpStack() as that logs to System.err.