Skip to content

Commit

Permalink
Add support packetflow based payload handling.
Browse files Browse the repository at this point in the history
Add packet registration for registry packets.
  • Loading branch information
marchermans committed Nov 13, 2023
1 parent f4b4702 commit 3ccaaea
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 46 deletions.
10 changes: 10 additions & 0 deletions patches/net/minecraft/network/protocol/PacketFlow.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- a/net/minecraft/network/protocol/PacketFlow.java
+++ b/net/minecraft/network/protocol/PacketFlow.java
@@ -1,6 +1,6 @@
package net.minecraft.network.protocol;

-public enum PacketFlow {
+public enum PacketFlow implements net.neoforged.neoforge.common.extensions.IPacketFlowExtension {
SERVERBOUND,
CLIENTBOUND;

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.neoforged.neoforge.common.extensions;

import net.minecraft.network.protocol.PacketFlow;

public interface IPacketFlowExtension {

default PacketFlow self() {
return (PacketFlow) this;
}

default boolean isClientbound() {
return self() == PacketFlow.CLIENTBOUND;
}

default boolean isServerbound() {
return self() == PacketFlow.SERVERBOUND;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@
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 org.jetbrains.annotations.ApiStatus;
Expand All @@ -26,12 +33,27 @@ public class NetworkInitialization {
public static void register(final RegisterPacketHandlerEvent event) {
event.registrar()
.withVersion(buildNetworkVersion())
.flowing(PacketFlow.CLIENTBOUND)
.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
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package net.neoforged.neoforge.network.configuration;

import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.server.network.ConfigurationTask;
import net.neoforged.neoforge.network.payload.FrozenRegistrySyncStartPayload;
import net.neoforged.neoforge.registries.RegistryManager;

import java.util.function.Consumer;

Expand All @@ -13,7 +12,7 @@ public record SyncRegistries() implements ICustomConfigurationTask {

@Override
public void run(Consumer<CustomPacketPayload> sender) {
sender.accept(new FrozenRegistrySyncStartPayload());
sender.accept(new FrozenRegistrySyncStartPayload(RegistryManager.getRegistryNamesForSyncToClient()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
import com.mojang.brigadier.tree.RootCommandNode;
import com.mojang.logging.LogUtils;
import io.netty.channel.ChannelHandler;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.Commands;
import net.minecraft.commands.SharedSuggestionProvider;
Expand All @@ -32,12 +27,18 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagNetworkSerialization;
import net.neoforged.neoforge.network.NetworkHooks;
import net.neoforged.neoforge.network.registration.NetworkRegistry;
import net.neoforged.neoforge.registries.ForgeRegistries;
import net.neoforged.neoforge.registries.RegistryManager;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

/**
* A filter for impl packets, used to filter/modify parts of vanilla impl messages that
* will cause errors or warnings on vanilla clients, for example entity attributes that are added by Forge or mods.
Expand All @@ -57,7 +58,7 @@ public VanillaConnectionNetworkFilter() {

@Override
protected boolean isNecessary(Connection manager) {
return NetworkHooks.isVanillaConnection(manager);
return NetworkRegistry.getInstance().isVanillaConnection(manager);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.neoforged.neoforge.network.handlers;

import net.neoforged.neoforge.network.configuration.SyncRegistries;
import net.neoforged.neoforge.network.handling.ConfigurationPayloadContext;
import net.neoforged.neoforge.network.handling.IConfigurationPayloadHandler;
import net.neoforged.neoforge.network.payload.FrozenRegistrySyncCompletedPayload;
import net.neoforged.neoforge.network.payload.FrozenRegistrySyncCompletePayload;

public class ServerForgeRegistryHandler {

Expand All @@ -15,7 +15,7 @@ public static ServerForgeRegistryHandler getInstance() {
private ServerForgeRegistryHandler() {
}

public void handle(ConfigurationPayloadContext context, FrozenRegistrySyncCompletedPayload payload) {
context.taskCompletedHandler().onTaskCompleted();
public void handle(ConfigurationPayloadContext context, FrozenRegistrySyncCompletePayload payload) {
context.taskCompletedHandler().onTaskCompleted(SyncRegistries.TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.reading.PayloadReadingContext;
import net.neoforged.neoforge.registries.ForgeRegistry;

public record FrozenRegistryPayload(ResourceLocation registryName, ForgeRegistry.Snapshot snapshot) implements CustomPacketPayload {

public static final ResourceLocation ID = new ResourceLocation("neoforge:frozen_registry");

public FrozenRegistryPayload(FriendlyByteBuf buf) {
public FrozenRegistryPayload(FriendlyByteBuf buf, PayloadReadingContext context) {
this(buf.readResourceLocation(), ForgeRegistry.Snapshot.read(buf));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.reading.PayloadReadingContext;
import org.jetbrains.annotations.NotNull;

public record FrozenRegistrySyncCompletePayload() implements CustomPacketPayload {

private static final ResourceLocation ID = new ResourceLocation("neoforge:frozen_registry_sync_complete");
public static final ResourceLocation ID = new ResourceLocation("neoforge:frozen_registry_sync_complete");

public FrozenRegistrySyncCompletePayload(FriendlyByteBuf buf, PayloadReadingContext context) {
this();
}

@Override
public void write(@NotNull FriendlyByteBuf buf) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.reading.PayloadReadingContext;
import org.jetbrains.annotations.NotNull;

import java.util.Set;
Expand All @@ -11,7 +12,7 @@ public record FrozenRegistrySyncStartPayload(Set<ResourceLocation> toAccess) imp

public static final ResourceLocation ID = new ResourceLocation("neoforge:frozen_registry_sync_start");

public FrozenRegistrySyncStartPayload(FriendlyByteBuf buf) {
public FrozenRegistrySyncStartPayload(FriendlyByteBuf buf, PayloadReadingContext context) {
this(buf.readSet(FriendlyByteBuf::readResourceLocation));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ public record SplitPacketPayload(byte[] payload) implements CustomPacketPayload

public static final ResourceLocation ID = new ResourceLocation("neoforge", "split");

public SplitPacketPayload(FriendlyByteBuf buf) {
this(buf.readBytes(buf.readVarInt()).array());
}

public SplitPacketPayload(FriendlyByteBuf buf, PayloadReadingContext context) {
this(buf);
this(buf.readBytes(buf.readVarInt()).array());
}

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

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;

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

@Nullable
private final IConfigurationPayloadHandler<T> clientSide;
@Nullable
private final IConfigurationPayloadHandler<T> serverSide;

private ConfigurationPayloadHandler(@Nullable IConfigurationPayloadHandler<T> clientSide, @Nullable IConfigurationPayloadHandler<T> serverSide) {
this.clientSide = clientSide;
this.serverSide = serverSide;
}

@Override
public void handle(ConfigurationPayloadContext context, T payload) {
if (context.flow().isClientbound()) {
if (clientSide != null) {
clientSide.handle(context, payload);
}
} else if (context.flow().isServerbound()) {
if (serverSide != null) {
serverSide.handle(context, payload);
}
}
}

public static class Builder<T extends CustomPacketPayload> {
private @Nullable IConfigurationPayloadHandler<T> clientSide;
private @Nullable IConfigurationPayloadHandler<T> serverSide;

public Builder<T> client(@NotNull IConfigurationPayloadHandler<T> clientSide) {
this.clientSide = clientSide;
return this;
}

public Builder<T> server(@NotNull IConfigurationPayloadHandler<T> serverSide) {
this.serverSide = serverSide;
return this;
}

ConfigurationPayloadHandler<T> create() {
return new ConfigurationPayloadHandler<T>(clientSide, serverSide);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ public interface IFlowBasedPayloadRegistrar extends IPayloadRegistrar {
* @return A registrar that is bound to the given flow.
*/
IPayloadRegistrarWithAcceptableRange flowing(PacketFlow flow);

/**
* Enables payloads registered with this registrar to flow in both directions.
* <p>
* This is the default value for all registrars, unless {@link #flowing(PacketFlow)} has been called.
* Invoking this method as such is only needed to reset the ability to flow in both directions.
* </p>
*
* @return A registrar that is bound to both directions.
*/
IPayloadRegistrarWithAcceptableRange bidirectional();
}
Loading

0 comments on commit 3ccaaea

Please sign in to comment.