Skip to content

Commit

Permalink
[1.21] Implement common networking with config tasks (#1483)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadows-of-Fire authored Aug 31, 2024
1 parent 38ed034 commit 659d33f
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
+ }
+
+ if (p_295727_.payload() instanceof net.neoforged.neoforge.network.payload.CommonVersionPayload commonVersionPayload) {
+ net.neoforged.neoforge.network.registration.NetworkRegistry.checkCommonVersion(this.getConnection(), commonVersionPayload);
+ net.neoforged.neoforge.network.registration.NetworkRegistry.checkCommonVersion(this, commonVersionPayload);
+ return;
+ }
+
+ if (p_295727_.payload() instanceof net.neoforged.neoforge.network.payload.CommonRegisterPayload commonRegisterPayload) {
+ net.neoforged.neoforge.network.registration.NetworkRegistry.onCommonRegister(this.getConnection(), commonRegisterPayload);
+ net.neoforged.neoforge.network.registration.NetworkRegistry.onCommonRegister(this, commonRegisterPayload);
+ return;
+ }
+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
+ }
+
+ if (p_294276_.payload() instanceof net.neoforged.neoforge.network.payload.CommonVersionPayload commonVersionPayload) {
+ net.neoforged.neoforge.network.registration.NetworkRegistry.checkCommonVersion(this.getConnection(), commonVersionPayload);
+ net.neoforged.neoforge.network.registration.NetworkRegistry.checkCommonVersion(this, commonVersionPayload);
+ return;
+ }
+
+ if (p_294276_.payload() instanceof net.neoforged.neoforge.network.payload.CommonRegisterPayload commonRegisterPayload) {
+ net.neoforged.neoforge.network.registration.NetworkRegistry.onCommonRegister(this.getConnection(), commonRegisterPayload);
+ net.neoforged.neoforge.network.registration.NetworkRegistry.onCommonRegister(this, commonRegisterPayload);
+ return;
+ }
+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.network.configuration.CheckExtensibleEnums;
import net.neoforged.neoforge.network.configuration.CommonRegisterTask;
import net.neoforged.neoforge.network.configuration.CommonVersionTask;
import net.neoforged.neoforge.network.configuration.RegistryDataMapNegotiation;
import net.neoforged.neoforge.network.configuration.SyncConfig;
import net.neoforged.neoforge.network.configuration.SyncRegistries;
import net.neoforged.neoforge.network.event.RegisterConfigurationTasksEvent;
import net.neoforged.neoforge.network.payload.CommonRegisterPayload;
import net.neoforged.neoforge.network.payload.CommonVersionPayload;
import net.neoforged.neoforge.network.payload.ConfigFilePayload;
import net.neoforged.neoforge.network.payload.FrozenRegistryPayload;
import net.neoforged.neoforge.network.payload.FrozenRegistrySyncCompletedPayload;
Expand All @@ -39,12 +43,18 @@ public static void configureEarlyTasks(ServerConfigurationPacketListener listene

@SubscribeEvent
private static void configureModdedClient(RegisterConfigurationTasksEvent event) {
if (event.getListener().hasChannel(ConfigFilePayload.TYPE)) {
event.register(new SyncConfig(event.getListener()));
ServerConfigurationPacketListener listener = event.getListener();
if (listener.hasChannel(CommonVersionPayload.TYPE) && listener.hasChannel(CommonRegisterPayload.TYPE)) {
event.register(new CommonVersionTask());
event.register(new CommonRegisterTask());
}

if (listener.hasChannel(ConfigFilePayload.TYPE)) {
event.register(new SyncConfig(listener));
}

//These two can always be registered they detect the listener connection type internally and will skip themselves.
event.register(new RegistryDataMapNegotiation(event.getListener()));
event.register(new CheckExtensibleEnums(event.getListener()));
event.register(new RegistryDataMapNegotiation(listener));
event.register(new CheckExtensibleEnums(listener));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.network.configuration;

import java.util.function.Consumer;
import net.minecraft.network.ConnectionProtocol;
import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.network.payload.CommonRegisterPayload;
import net.neoforged.neoforge.network.registration.NetworkRegistry;
import org.jetbrains.annotations.ApiStatus;

/**
* Common Register configuration task. After completion of {@link CommonVersionTask}, sends a {@link CommonRegisterPayload} to the client
* containing all known serverbound channels, and awaits a response containing the client's known clientbound channels.
*/
@ApiStatus.Internal
public record CommonRegisterTask() implements ICustomConfigurationTask {
public static final Type TYPE = new Type(ResourceLocation.fromNamespaceAndPath(NeoForgeVersion.MOD_ID, "common_register"));

@Override
public Type type() {
return TYPE;
}

@Override
public void run(Consumer<CustomPacketPayload> sender) {
// There is currently no implementation for a version handshake, and the only existing version is 1, so we only send 1.
// Version negotiation will have to be implemented properly if a version 2 is ever added.
sender.accept(new CommonRegisterPayload(1, ConnectionProtocol.PLAY, NetworkRegistry.getCommonPlayChannels(PacketFlow.SERVERBOUND)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.network.configuration;

import java.util.function.Consumer;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.network.payload.CommonVersionPayload;
import org.jetbrains.annotations.ApiStatus;

/**
* Common Version configuration task. Initiated after registry sync to begin the c:register handshake.
* The server will start the task, send c:version to the client, and await a reply. Upon reply, we transition to {@link CommonRegisterTask}.
*/
@ApiStatus.Internal
public record CommonVersionTask() implements ICustomConfigurationTask {
public static final Type TYPE = new Type(ResourceLocation.fromNamespaceAndPath(NeoForgeVersion.MOD_ID, "common_version"));

@Override
public Type type() {
return TYPE;
}

@Override
public void run(Consumer<CustomPacketPayload> sender) {
sender.accept(new CommonVersionPayload());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import net.neoforged.neoforge.common.extensions.ICommonPacketListener;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.network.configuration.CheckExtensibleEnums;
import net.neoforged.neoforge.network.configuration.CommonRegisterTask;
import net.neoforged.neoforge.network.configuration.CommonVersionTask;
import net.neoforged.neoforge.network.connection.ConnectionType;
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent;
import net.neoforged.neoforge.network.filters.NetworkFilters;
Expand Down Expand Up @@ -379,7 +381,6 @@ public static void initializeNeoForgeConnection(ServerConfigurationPacketListene
nowListeningOn.addAll(getInitialListeningChannels(listener.flow()));
nowListeningOn.addAll(setup.getChannels(ConnectionProtocol.CONFIGURATION).keySet());
listener.send(new MinecraftRegisterPayload(nowListeningOn.build()));
sendCommonPayloads(listener);
}

/**
Expand Down Expand Up @@ -419,7 +420,6 @@ public static boolean initializeOtherConnection(ServerConfigurationPacketListene
.filter(registration -> registration.getValue().optional())
.forEach(registration -> nowListeningOn.add(registration.getKey()));
listener.send(new MinecraftRegisterPayload(nowListeningOn.build()));
sendCommonPayloads(listener);

return true;
}
Expand Down Expand Up @@ -516,7 +516,6 @@ public static void initializeNeoForgeConnection(ClientConfigurationPacketListene
nowListeningOn.addAll(getInitialListeningChannels(listener.flow()));
nowListeningOn.addAll(setup.getChannels(ConnectionProtocol.CONFIGURATION).keySet());
listener.send(new MinecraftRegisterPayload(nowListeningOn.build()));
sendCommonPayloads(listener);
}

/**
Expand Down Expand Up @@ -569,7 +568,6 @@ public static void initializeOtherConnection(ClientConfigurationPacketListener l
.filter(registration -> registration.getValue().optional())
.forEach(registration -> nowListeningOn.add(registration.getKey()));
listener.send(new MinecraftRegisterPayload(nowListeningOn.build()));
sendCommonPayloads(listener);
}

/**
Expand Down Expand Up @@ -724,14 +722,22 @@ public static Set<ResourceLocation> getInitialServerUnregisterChannels() {
* <p>
* Invoked on the network thread.
*
* @param connection The current connection.
* @param payload The incoming version payload.
* @param listener The receiving listener.
* @param payload The incoming version payload.
*/
public static void checkCommonVersion(Connection connection, CommonVersionPayload payload) {
public static void checkCommonVersion(ICommonPacketListener listener, CommonVersionPayload payload) {
List<Integer> otherVersions = payload.versions();
if (otherVersions.stream().noneMatch(SUPPORTED_COMMON_NETWORKING_VERSIONS::contains)) {
String versions = String.join(", ", SUPPORTED_COMMON_NETWORKING_VERSIONS.stream().map(i -> i.toString()).toList());
connection.disconnect(Component.literal("Unsupported common network version. This installation of NeoForge only supports: " + versions));
listener.disconnect(Component.literal("Unsupported common network version. This installation of NeoForge only supports: " + versions));
}

if (listener.protocol() == ConnectionProtocol.CONFIGURATION) {
if (listener.flow() == PacketFlow.SERVERBOUND) {
((ServerConfigurationPacketListener) listener).finishCurrentTask(CommonVersionTask.TYPE);
} else {
listener.send(new CommonVersionPayload());
}
}
}

Expand All @@ -740,13 +746,21 @@ public static void checkCommonVersion(Connection connection, CommonVersionPayloa
* <p>
* Invoked on the network thread.
*
* @param connection The connection to add the channels to.
* @param payload The incoming register payload.
* @param listener The receiving listener.
* @param payload The incoming register payload.
*/
public static void onCommonRegister(Connection connection, CommonRegisterPayload payload) {
Set<ResourceLocation> channels = ChannelAttributes.getOrCreateCommonChannels(connection, payload.protocol());
public static void onCommonRegister(ICommonPacketListener listener, CommonRegisterPayload payload) {
Set<ResourceLocation> channels = ChannelAttributes.getOrCreateCommonChannels(listener.getConnection(), payload.protocol());
channels.clear();
channels.addAll(payload.channels());

if (listener.protocol() == ConnectionProtocol.CONFIGURATION) {
if (listener.flow() == PacketFlow.SERVERBOUND) {
((ServerConfigurationPacketListener) listener).finishCurrentTask(CommonRegisterTask.TYPE);
} else {
listener.send(new CommonRegisterPayload(1, ConnectionProtocol.PLAY, getCommonPlayChannels(PacketFlow.CLIENTBOUND)));
}
}
}

public static Set<ResourceLocation> getCommonPlayChannels(PacketFlow flow) {
Expand All @@ -758,16 +772,6 @@ public static Set<ResourceLocation> getCommonPlayChannels(PacketFlow flow) {
.collect(Collectors.toSet());
}

public static void sendCommonPayloads(ICommonPacketListener listener) {
if (listener.hasChannel(CommonVersionPayload.ID)) {
listener.send(new CommonVersionPayload());
}

if (listener.hasChannel(CommonRegisterPayload.ID)) {
listener.send(new CommonRegisterPayload(1, ConnectionProtocol.PLAY, getCommonPlayChannels(listener.flow())));
}
}

/**
* Invoked when the configuration phase of a connection is completed.
* <p>
Expand Down

0 comments on commit 659d33f

Please sign in to comment.