类 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.
-
字段概要
字段修饰符和类型字段说明protected static final Map
<ResourceLocation, StreamCodec<FriendlyByteBuf, ? extends CustomPacketPayload>> Map of NeoForge payloads that may be sent before channel negotiation.protected static final Map
<ConnectionProtocol, Map<ResourceLocation, IPayloadHandler<?>>> private static final org.slf4j.Logger
protected static final Map
<ConnectionProtocol, Map<ResourceLocation, PayloadRegistration<?>>> Registry of all custom payload handlers.protected static final Map
<ConnectionProtocol, Map<ResourceLocation, IPayloadHandler<?>>> protected static boolean
Declared common networking versions currently supported by NeoForge. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明static void
checkCommonVersion
(ICommonPacketListener listener, CommonVersionPayload payload) Called when aCommonVersionPayload
is received.static void
checkPacket
(Packet<?> packet, ClientCommonPacketListener listener) Validates that aServerboundCustomPayloadPacket
may be sent to the server.static void
checkPacket
(Packet<?> packet, ServerCommonPacketListener listener) Validates that aClientboundCustomPayloadPacket
may be sent to the client.static void
configureMockConnection
(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 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.static @Nullable StreamCodec
<? super FriendlyByteBuf, ? extends CustomPacketPayload> getCodec
(ResourceLocation id, ConnectionProtocol protocol, PacketFlow flow) Attempts to retrieve theStreamCodec
for a non-vanilla payload.static Set
<ResourceLocation> static ConnectionType
getConnectionType
(Connection connection) static Set
<ResourceLocation> 返回 the initial channels for the configuration phase.。static Set
<ResourceLocation> static <T> CompletableFuture
<T> guard
(CompletableFuture<T> future, ResourceLocation payloadId) Helper to guard futures generated byIPayloadContext
against exceptions.static void
handleModdedPayload
(ServerCommonPacketListener listener, ServerboundCustomPayloadPacket packet) Handles modded payloads on the server.static <T extends PacketListener>
voidhandlePacketUnchecked
(Packet<T> packet, PacketListener listener) protected static boolean
hasAdhocChannel
(ConnectionProtocol protocol, ResourceLocation id, 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
(Connection connection, @Nullable ConnectionProtocol protocol, ResourceLocation payloadId) Indicates whether the given connection has a connection setup that can transmit the given payload id.static boolean
hasChannel
(ICommonPacketListener listener, ResourceLocation payloadId) Checks if the packet listener's connection can send/receive the given payload.static void
initializeNeoForgeConnection
(ServerConfigurationPacketListener listener, Map<ConnectionProtocol, Set<ModdedNetworkQueryComponent>> clientChannels) Invoked by the server when it completes the negotiation with the client during the configuration phase.static boolean
Invoked by theServerConfigurationPacketListenerImpl
when a vanilla or other connection is detected.static boolean
isModdedPayload
(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
(Connection connection, Set<ResourceLocation> resourceLocations) Invoked to add to the known ad-hoc channels on a connection.static void
onMinecraftUnregister
(Connection connection, Set<ResourceLocation> resourceLocations) Invoked to remove from the known ad-hoc channels on a connection.static <T extends CustomPacketPayload,
B extends FriendlyByteBuf>
voidregister
(CustomPacketPayload.Type<T> type, StreamCodec<? super B, T> codec, @Nullable IPayloadHandler<T> serverHandler, @Nullable IPayloadHandler<T> clientHandler, List<ConnectionProtocol> protocols, Optional<PacketFlow> flow, String version, boolean optional) Registers a new payload.protected static <T extends CustomPacketPayload>
voidregisterHandler
(Map<ConnectionProtocol, Map<ResourceLocation, IPayloadHandler<?>>> handlers, ConnectionProtocol protocol, PacketFlow flow, CustomPacketPayload.Type<T> type, IPayloadHandler<T> handler) static void
setup()
Sets up the network registry by firingRegisterPayloadHandlersEvent
, storing the resulting payload registrations inPAYLOAD_REGISTRATIONS
.
-
字段详细资料
-
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
protected static final Map<ResourceLocation,StreamCodec<FriendlyByteBuf, BUILTIN_PAYLOADS? extends CustomPacketPayload>> Map of NeoForge payloads that may be sent before channel negotiation. TODO: Separate by protocol + flow. -
PAYLOAD_REGISTRATIONS
protected static final Map<ConnectionProtocol,Map<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. -
SERVERBOUND_HANDLERS
protected static final Map<ConnectionProtocol,Map<ResourceLocation, SERVERBOUND_HANDLERSIPayloadHandler<?>>> -
CLIENTBOUND_HANDLERS
protected static final Map<ConnectionProtocol,Map<ResourceLocation, CLIENTBOUND_HANDLERSIPayloadHandler<?>>> -
setup
protected static boolean setup
-
-
构造器详细资料
-
NetworkRegistry
protected NetworkRegistry()
-
-
方法详细资料
-
setup
public static void setup()Sets up the network registry by firingRegisterPayloadHandlersEvent
, storing the resulting payload registrations inPAYLOAD_REGISTRATIONS
. -
register
public static <T extends CustomPacketPayload,B extends FriendlyByteBuf> void register(CustomPacketPayload.Type<T> type, StreamCodec<? super B, T> codec, @Nullable @Nullable IPayloadHandler<T> serverHandler, @Nullable @Nullable IPayloadHandler<T> clientHandler, List<ConnectionProtocol> protocols, Optional<PacketFlow> flow, String version, boolean optional) Registers a new payload.- 类型参数:
T
- The class of the payload.B
- The class of the ByteBuf. Onlyplay
payloads may useRegistryFriendlyByteBuf
.- 参数:
type
- The type of the payload.codec
- The codec for the payload.serverHandler
- The server-side handler for the payload. This handler will be executed on the network thread.clientHandler
- The client-side handler for the payload. This handler 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.
-
registerHandler
protected static <T extends CustomPacketPayload> void registerHandler(Map<ConnectionProtocol, Map<ResourceLocation, IPayloadHandler<?>>> handlers, ConnectionProtocol protocol, PacketFlow flow, CustomPacketPayload.Type<T> type, IPayloadHandler<T> handler) -
getCodec
@Nullable public static @Nullable StreamCodec<? super FriendlyByteBuf,? extends CustomPacketPayload> getCodec(ResourceLocation id, ConnectionProtocol 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.- 参数:
id
- The id of the payload.protocol
- The protocol of the connection.flow
- The flow of the connection.- 返回:
- A codec for the payload, or null if the payload should be discarded on receipt.
- API Note:
- This method must not throw exceptions, as it is called within another codec on the network thread.
-
isModdedPayload
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.- 返回:
- True if the payload is modded, and modded payload handling should be invoked for it.
-
handleModdedPayload
public static void handleModdedPayload(ServerCommonPacketListener listener, ServerboundCustomPayloadPacket packet) Handles modded payloads on the server. Invoked after built-in handling.Called on the network thread.
- 参数:
listener
- The listener which received the packet.packet
- The packet that was received.
-
initializeNeoForgeConnection
public static void initializeNeoForgeConnection(ServerConfigurationPacketListener listener, Map<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.
- 参数:
listener
- The listener which completed the negotiation.clientChannels
- The network channels that the client has available.
-
initializeOtherConnection
Invoked by theServerConfigurationPacketListenerImpl
when a vanilla or other connection is detected.- 参数:
listener
- The listener which detected the vanilla connection during the configuration phase.- 返回:
- True if the vanilla connection should be handled by the server, false otherwise.
-
checkPacket
Validates that aClientboundCustomPayloadPacket
may be sent to the client.- 参数:
packet
- The packet that is about to be sent.listener
- The listener that wants to send the packet.- 抛出:
UnsupportedOperationException
- if the packet may not be sent.
-
checkPacket
Validates that aServerboundCustomPayloadPacket
may be sent to the server.- 参数:
packet
- The packet that is about to be sent.listener
- The listener that wants to send the packet.- 抛出:
UnsupportedOperationException
- if the packet may not be sent.
-
hasAdhocChannel
protected static boolean hasAdhocChannel(ConnectionProtocol protocol, ResourceLocation id, 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.
- 参数:
id
- The id of the packet.flow
- The flow of the packet.- 返回:
- True if the packet is ad-hoc readable, false otherwise.
-
hasChannel
Checks if the packet listener's connection can send/receive the given payload.- 参数:
listener
- The listener to check.payloadId
- The payload id to check.- 返回:
- True if the listener has a connection setup that can transmit the given payload id, false otherwise.
-
hasChannel
public static boolean hasChannel(Connection connection, @Nullable @Nullable ConnectionProtocol protocol, ResourceLocation payloadId) Indicates whether the given connection has a connection setup that can transmit the given payload id.- 参数:
connection
- The connection to check.protocol
- The protocol to check. Pass null to check against all protocols.payloadId
- The payload id to check.- 返回:
- True if the connection has a connection setup that can transmit the given payload id, false otherwise.
-
filterGameBundlePackets
public static <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.- 类型参数:
T
- The type of the listener.- 参数:
context
- The context of the connection.packets
- The packets to filter.- 返回:
- The filtered packets.
-
configureMockConnection
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.- 参数:
connection
- The connection to configure.
-
onMinecraftRegister
public static void onMinecraftRegister(Connection connection, Set<ResourceLocation> resourceLocations) Invoked to add to the known ad-hoc channels on a connection.Invoked on the network thread.
- 参数:
connection
- The connection to add the channels to.resourceLocations
- The resource locations to add.
-
onMinecraftUnregister
public static void onMinecraftUnregister(Connection connection, Set<ResourceLocation> resourceLocations) Invoked to remove from the known ad-hoc channels on a connection.Invoked on the network thread.
- 参数:
connection
- The connection to remove the channels from.resourceLocations
- The resource locations to remove.
-
getInitialListeningChannels
返回 the initial channels for the configuration phase.。- 返回:
- 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.
- 参数:
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.
- 参数:
listener
- The receiving listener.payload
- The incoming register payload.
-
getCommonPlayChannels
-
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.
- 参数:
listener
- The packet listener finishing the configuration phase
-
getConnectionType
-
guard
public static <T> CompletableFuture<T> guard(CompletableFuture<T> future, ResourceLocation payloadId) Helper to guard futures generated byIPayloadContext
against exceptions. -
handlePacketUnchecked
public static <T extends PacketListener> void handlePacketUnchecked(Packet<T> packet, PacketListener listener) -
dumpStackToLog
private static void dumpStackToLog()Used in place ofThread.dumpStack()
as that logs toSystem.err
.
-