Skip to content

Commit

Permalink
Massively simplify the internals of how stuff is registered.
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed Nov 14, 2023
1 parent 85cd3e0 commit 53449a9
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,40 @@

package net.neoforged.neoforge.network;

import java.util.Arrays;
import java.util.List;

import net.minecraft.network.protocol.PacketFlow;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.network.event.EventNetworkChannel;
import net.neoforged.neoforge.network.event.RegisterPacketHandlerEvent;
import net.neoforged.neoforge.network.handlers.ClientForgeRegistryHandler;
import net.neoforged.neoforge.network.handlers.ServerForgeRegistryHandler;
import net.neoforged.neoforge.network.handling.ConfigurationPayloadContext;
import net.neoforged.neoforge.network.handling.IConfigurationPayloadHandler;
import net.neoforged.neoforge.network.payload.FrozenRegistryPayload;
import net.neoforged.neoforge.network.payload.FrozenRegistrySyncCompletePayload;
import net.neoforged.neoforge.network.payload.FrozenRegistrySyncStartPayload;
import net.neoforged.neoforge.network.simple.SimpleChannel;
import net.neoforged.neoforge.registries.RegistryManager;
import net.neoforged.neoforge.network.registration.registrar.IPayloadRegistrar;
import org.jetbrains.annotations.ApiStatus;

@Mod.EventBusSubscriber(modid = NeoForgeVersion.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD)
@ApiStatus.Internal
public class NetworkInitialization {

@SubscribeEvent
public static void register(final RegisterPacketHandlerEvent event) {
event.registrar()
.withVersion(buildNetworkVersion())
.flowing(PacketFlow.CLIENTBOUND)
final IPayloadRegistrar registrar = event.registrar(NeoForgeVersion.MOD_ID)
.versioned(buildNetworkVersion());
registrar
.configuration(
FrozenRegistrySyncStartPayload.ID,
FrozenRegistrySyncStartPayload.class,
FrozenRegistrySyncStartPayload::new,
handlers -> handlers.client(ClientForgeRegistryHandler.getInstance()::handle)
)
.configuration(
FrozenRegistryPayload.ID,
FrozenRegistryPayload.class,
FrozenRegistryPayload::new,
handlers -> handlers.client(ClientForgeRegistryHandler.getInstance()::handle)
)
.bidirectional()
.configuration(
FrozenRegistrySyncCompletePayload.ID,
FrozenRegistrySyncCompletePayload.class,
FrozenRegistrySyncCompletePayload::new,
handlers -> handlers.client(ClientForgeRegistryHandler.getInstance()::handle)
.server(ServerForgeRegistryHandler.getInstance()::handle)
Expand Down Expand Up @@ -82,21 +71,8 @@ private static int buildNetworkVersion() {
/* public static SimpleChannel getHandshakeChannel() {
SimpleChannel handshakeChannel = NetworkRegistry.ChannelBuilder.named(NetworkConstants.FML_HANDSHAKE_RESOURCE).clientAcceptedVersions(a -> true).serverAcceptedVersions(a -> true).networkProtocolVersion(() -> NetworkConstants.NETVERSION).simpleChannel();
handshakeChannel.simpleLoginMessageBuilder(HandshakeMessages.C2SAcknowledge.class, 99, LoginNetworkDirection.LOGIN_TO_SERVER).decoder(HandshakeMessages.C2SAcknowledge::decode).consumerNetworkThread(HandshakeHandler.indexFirst(HandshakeHandler::handleClientAck)).add();
handshakeChannel.simpleLoginMessageBuilder(HandshakeMessages.S2CModData.class, 5, LoginNetworkDirection.LOGIN_TO_CLIENT).decoder(HandshakeMessages.S2CModData::decode).markAsLoginPacket().noResponse().consumerNetworkThread(HandshakeHandler.consumerFor(HandshakeHandler::handleModData)).add();
handshakeChannel.simpleLoginMessageBuilder(HandshakeMessages.S2CModList.class, 1, LoginNetworkDirection.LOGIN_TO_CLIENT).decoder(HandshakeMessages.S2CModList::decode).markAsLoginPacket().consumerNetworkThread(HandshakeHandler.consumerFor(HandshakeHandler::handleServerModListOnClient)).add();
handshakeChannel.simpleLoginMessageBuilder(HandshakeMessages.C2SModListReply.class, 2, LoginNetworkDirection.LOGIN_TO_SERVER).decoder(HandshakeMessages.C2SModListReply::decode).consumerNetworkThread(HandshakeHandler.indexFirst(HandshakeHandler::handleClientModListOnServer)).add();
handshakeChannel.simpleLoginMessageBuilder(HandshakeMessages.S2CRegistry.class, 3, LoginNetworkDirection.LOGIN_TO_CLIENT).decoder(HandshakeMessages.S2CRegistry::decode).buildLoginPacketList(RegistryManager::generateRegistryPackets). //TODO: Make this non-static, and store a cache on the client.
consumerNetworkThread(HandshakeHandler.consumerFor(HandshakeHandler::handleRegistryMessage)).add();
handshakeChannel.simpleLoginMessageBuilder(HandshakeMessages.S2CConfigData.class, 4, LoginNetworkDirection.LOGIN_TO_CLIENT).decoder(HandshakeMessages.S2CConfigData::decode).buildLoginPacketList(ConfigSync.INSTANCE::syncConfigs).consumerNetworkThread(HandshakeHandler.consumerFor(HandshakeHandler::handleConfigSync)).add();
handshakeChannel.simpleLoginMessageBuilder(HandshakeMessages.S2CChannelMismatchData.class, 6, LoginNetworkDirection.LOGIN_TO_CLIENT).decoder(HandshakeMessages.S2CChannelMismatchData::decode).consumerNetworkThread(HandshakeHandler.consumerFor(HandshakeHandler::handleModMismatchData)).add();
return handshakeChannel;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
import com.google.common.collect.ImmutableMap;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.bus.api.Event;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.event.IModBusEvent;
import net.neoforged.neoforge.network.registration.registrar.ConfigurationRegistration;
import net.neoforged.neoforge.network.registration.registrar.IPayloadRegistrar;
import net.neoforged.neoforge.network.registration.registrar.ModdedPacketRegistrar;
import net.neoforged.neoforge.network.registration.registrar.PlayRegistration;

import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class RegisterPacketHandlerEvent extends Event implements IModBusEvent {
public class RegisterPacketHandlerEvent extends Event {

private final Map<String, ModdedPacketRegistrar> registrarsByNamespace = Collections.synchronizedMap(new HashMap<>());

public ModdedPacketRegistrar registrar() {
final String namespace = ModLoadingContext.get().getActiveNamespace();
public IPayloadRegistrar registrar(String namespace) {
return registrarsByNamespace.computeIfAbsent(namespace, ModdedPacketRegistrar::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ public class VanillaPacketSplitter {

@SubscribeEvent
public static void register(final RegisterPacketHandlerEvent event) {
event.registrar()
.withVersion(VERSION)
event.registrar(NeoForgeVersion.MOD_ID)
.versioned(versioning -> versioning.withVersion(VERSION).optional())
.common(
SplitPacketPayload.ID,
SplitPacketPayload.class,
SplitPacketPayload::new,
VanillaPacketSplitter::receivedPacket
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ public interface IPlayPayloadHandler<T extends CustomPacketPayload> {
* @param payload The payload.
*/
void handle(PlayPayloadContext context, T payload);

/**
* Creates a handler that does nothing.
*
* @return The handler.
* @param <Z> The type of payload.
*/
static <Z extends CustomPacketPayload> IPlayPayloadHandler<Z> noop() {
return (context, payload) -> {};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.neoforged.neoforge.network.payload;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;

public record ConfigFilePayload() implements CustomPacketPayload {
@Override
public void write(FriendlyByteBuf p_294947_) {

}

@Override
public ResourceLocation id() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import net.minecraft.util.thread.ReentrantBlockableEventLoop;
import net.neoforged.fml.ModLoader;
import net.neoforged.fml.config.ConfigTracker;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.NeoForgeMod;
import net.neoforged.neoforge.internal.versions.neoforge.NeoForgeVersion;
import net.neoforged.neoforge.network.connection.ConnectionUtils;
import net.neoforged.neoforge.network.event.RegisterPacketHandlerEvent;
Expand Down Expand Up @@ -104,9 +106,7 @@ public void setup() {

setup = true;

final RegisterPacketHandlerEvent event = new RegisterPacketHandlerEvent();
ModLoader.get().postEventWrapContainerInModOrder(event);

final RegisterPacketHandlerEvent event = NeoForge.EVENT_BUS.post(new RegisterPacketHandlerEvent());
knownConfigurationRegistrations.clear();
knownPlayRegistrations.clear();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package net.neoforged.neoforge.network.registration.registrar;

import net.minecraft.network.protocol.PacketFlow;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.neoforged.neoforge.network.handling.ConfigurationPayloadContext;
import net.neoforged.neoforge.network.handling.IConfigurationPayloadHandler;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public final class ConfigurationPayloadHandler<T extends CustomPacketPayload> implements IConfigurationPayloadHandler<T> {

@Nullable
Expand All @@ -31,6 +34,22 @@ public void handle(ConfigurationPayloadContext context, T payload) {
}
}

Optional<PacketFlow> flow() {
if (clientSide == null && serverSide == null) {
return Optional.empty();
}

if (clientSide == null) {
return Optional.of(PacketFlow.SERVERBOUND);
}

if (serverSide == null) {
return Optional.of(PacketFlow.CLIENTBOUND);
}

return Optional.empty();
}

public static class Builder<T extends CustomPacketPayload> {
private @Nullable IConfigurationPayloadHandler<T> clientSide;
private @Nullable IConfigurationPayloadHandler<T> serverSide;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.OptionalInt;

public record ConfigurationRegistration<T extends CustomPacketPayload>(
Class<T> type,
IPayloadReader<T> reader,
IConfigurationPayloadHandler<T> handler,
OptionalInt version,
Expand All @@ -21,9 +20,10 @@ public record ConfigurationRegistration<T extends CustomPacketPayload>(
Optional<PacketFlow> flow,
boolean optional
) implements IConfigurationPayloadHandler<CustomPacketPayload>, IPayloadReader<CustomPacketPayload> {
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void handle(ConfigurationPayloadContext context, CustomPacketPayload payload) {
handler.handle(context, type.cast(payload));
((IConfigurationPayloadHandler) handler).handle(context, payload);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package net.neoforged.neoforge.network.registration.registrar;

public interface INetworkPayloadVersioningBuilder {

INetworkPayloadVersioningBuilder withVersion(int version);

INetworkPayloadVersioningBuilder withMinimalVersion(int min);

INetworkPayloadVersioningBuilder withMaximalVersion(int max);

INetworkPayloadVersioningBuilder optional();

default INetworkPayloadVersioningBuilder withAcceptableRange(int min, int max) {
return withMinimalVersion(min).withMaximalVersion(max);
}
}
Loading

0 comments on commit 53449a9

Please sign in to comment.