From fa998a40e5eec7e37a115d13a00e5a67273fa98a Mon Sep 17 00:00:00 2001 From: RaymondBlaze Date: Wed, 29 May 2024 18:01:24 +0800 Subject: [PATCH 1/6] Fix #986 with connection aware `StreamCodec` --- ...ConfigurationPacketListenerImpl.java.patch | 16 +++++++-- .../RegistryFriendlyByteBuf.java.patch | 35 +++++++++++++++++++ ...ConfigurationPacketListenerImpl.java.patch | 6 ++-- .../server/players/PlayerList.java.patch | 9 +++++ .../world/effect/MobEffectInstance.java.patch | 5 +-- .../network/codec/NeoForgeStreamCodecs.java | 24 +++++++++++++ 6 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch diff --git a/patches/net/minecraft/client/multiplayer/ClientConfigurationPacketListenerImpl.java.patch b/patches/net/minecraft/client/multiplayer/ClientConfigurationPacketListenerImpl.java.patch index d28024ca56..5373b83221 100644 --- a/patches/net/minecraft/client/multiplayer/ClientConfigurationPacketListenerImpl.java.patch +++ b/patches/net/minecraft/client/multiplayer/ClientConfigurationPacketListenerImpl.java.patch @@ -22,7 +22,16 @@ } @Override -@@ -135,10 +_,17 @@ +@@ -121,7 +_,7 @@ + ); + this.connection + .setupInboundProtocol( +- GameProtocols.CLIENTBOUND.bind(RegistryFriendlyByteBuf.decorator(registryaccess$frozen)), ++ GameProtocols.CLIENTBOUND.bind(RegistryFriendlyByteBuf.decorator(registryaccess$frozen, this.connectionType)), + new ClientPacketListener( + this.minecraft, + this.connection, +@@ -135,12 +_,19 @@ this.postDisconnectScreen, this.serverCookies, this.chatState, @@ -39,8 +48,11 @@ + } + net.neoforged.neoforge.network.registration.NetworkRegistry.onConfigurationFinished(this); this.connection.send(ServerboundFinishConfigurationPacket.INSTANCE); - this.connection.setupOutboundProtocol(GameProtocols.SERVERBOUND.bind(RegistryFriendlyByteBuf.decorator(registryaccess$frozen))); +- this.connection.setupOutboundProtocol(GameProtocols.SERVERBOUND.bind(RegistryFriendlyByteBuf.decorator(registryaccess$frozen))); ++ this.connection.setupOutboundProtocol(GameProtocols.SERVERBOUND.bind(RegistryFriendlyByteBuf.decorator(registryaccess$frozen, this.connectionType))); } + + @Override @@ -152,5 +_,51 @@ public void onDisconnect(Component p_314649_) { super.onDisconnect(p_314649_); diff --git a/patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch b/patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch new file mode 100644 index 0000000000..6b91fc9187 --- /dev/null +++ b/patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch @@ -0,0 +1,35 @@ +--- a/net/minecraft/network/RegistryFriendlyByteBuf.java ++++ b/net/minecraft/network/RegistryFriendlyByteBuf.java +@@ -6,10 +_,22 @@ + + public class RegistryFriendlyByteBuf extends FriendlyByteBuf { + private final RegistryAccess registryAccess; ++ private final net.neoforged.neoforge.network.connection.ConnectionType connectionType; + + public RegistryFriendlyByteBuf(ByteBuf p_320951_, RegistryAccess p_319803_) { + super(p_320951_); + this.registryAccess = p_319803_; ++ this.connectionType = net.neoforged.neoforge.network.connection.ConnectionType.NEOFORGE; ++ } ++ ++ public RegistryFriendlyByteBuf(ByteBuf p_320951_, RegistryAccess p_319803_, net.neoforged.neoforge.network.connection.ConnectionType connectionType) { ++ super(p_320951_); ++ this.registryAccess = p_319803_; ++ this.connectionType = connectionType; ++ } ++ ++ public net.neoforged.neoforge.network.connection.ConnectionType getConnectionType() { ++ return this.connectionType; + } + + public RegistryAccess registryAccess() { +@@ -18,5 +_,9 @@ + + public static Function decorator(RegistryAccess p_320166_) { + return p_320793_ -> new RegistryFriendlyByteBuf(p_320793_, p_320166_); ++ } ++ ++ public static Function decorator(RegistryAccess p_320166_, net.neoforged.neoforge.network.connection.ConnectionType connectionType) { ++ return p_320793_ -> new RegistryFriendlyByteBuf(p_320793_, p_320166_, connectionType); + } + } diff --git a/patches/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java.patch b/patches/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java.patch index e4ac592dea..a534c64e18 100644 --- a/patches/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java.patch +++ b/patches/net/minecraft/server/network/ServerConfigurationPacketListenerImpl.java.patch @@ -56,10 +56,12 @@ } @Override -@@ -118,6 +_,12 @@ +@@ -117,7 +_,13 @@ + public void handleConfigurationFinished(ServerboundFinishConfigurationPacket p_294283_) { PacketUtils.ensureRunningOnSameThread(p_294283_, this, this.server); this.finishCurrentTask(JoinWorldTask.TYPE); - this.connection.setupOutboundProtocol(GameProtocols.CLIENTBOUND.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess()))); +- this.connection.setupOutboundProtocol(GameProtocols.CLIENTBOUND.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess()))); ++ this.connection.setupOutboundProtocol(GameProtocols.CLIENTBOUND.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess(), this.connectionType))); + // Packets can only be sent after the outbound protocol is set up again + if (this.connectionType == net.neoforged.neoforge.network.connection.ConnectionType.OTHER) { + //We need to also initialize this here, as the client may have sent the packet before we have finished our configuration. diff --git a/patches/net/minecraft/server/players/PlayerList.java.patch b/patches/net/minecraft/server/players/PlayerList.java.patch index 9f49a030d7..57053e5490 100644 --- a/patches/net/minecraft/server/players/PlayerList.java.patch +++ b/patches/net/minecraft/server/players/PlayerList.java.patch @@ -8,6 +8,15 @@ public PlayerList(MinecraftServer p_203842_, LayeredRegistryAccess p_251844_, PlayerDataStorage p_203844_, int p_203845_) { this.server = p_203842_; +@@ -177,7 +_,7 @@ + p_11263_.loadGameTypes(optional1.orElse(null)); + ServerGamePacketListenerImpl servergamepacketlistenerimpl = new ServerGamePacketListenerImpl(this.server, p_11262_, p_11263_, p_301988_); + p_11262_.setupInboundProtocol( +- GameProtocols.SERVERBOUND.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess())), servergamepacketlistenerimpl ++ GameProtocols.SERVERBOUND.bind(RegistryFriendlyByteBuf.decorator(this.server.registryAccess(), servergamepacketlistenerimpl.getConnectionType())), servergamepacketlistenerimpl + ); + GameRules gamerules = serverlevel1.getGameRules(); + boolean flag = gamerules.getBoolean(GameRules.RULE_DO_IMMEDIATE_RESPAWN); @@ -201,6 +_,7 @@ servergamepacketlistenerimpl.send(new ClientboundChangeDifficultyPacket(leveldata.getDifficulty(), leveldata.isDifficultyLocked())); servergamepacketlistenerimpl.send(new ClientboundPlayerAbilitiesPacket(p_11263_.getAbilities())); diff --git a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch index d2ce3a5057..e70ebe7b86 100644 --- a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch +++ b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch @@ -87,8 +87,9 @@ .apply(p_324063_, MobEffectInstance.Details::create) ) ); - public static final StreamCodec STREAM_CODEC = StreamCodec.recursive( +- public static final StreamCodec STREAM_CODEC = StreamCodec.recursive( - p_329990_ -> StreamCodec.composite( ++ public static final StreamCodec STREAM_CODEC = StreamCodec.recursive( + p_329990_ -> net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.composite( ByteBufCodecs.VAR_INT, MobEffectInstance.Details::amplifier, @@ -97,7 +98,7 @@ MobEffectInstance.Details::showIcon, p_329990_.apply(ByteBufCodecs::optional), MobEffectInstance.Details::hiddenEffect, -+ net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.>apply(ByteBufCodecs.collection(java.util.HashSet::new)).apply(ByteBufCodecs::optional), ++ net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.cast().>apply(ByteBufCodecs.collection(java.util.HashSet::new)).apply(codec -> net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.connectionAware(ByteBufCodecs.optional(codec), StreamCodec.unit(Optional.empty()))), + MobEffectInstance.Details::cures, MobEffectInstance.Details::new ) diff --git a/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java b/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java index 2656bbb220..b87dca825e 100644 --- a/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java +++ b/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java @@ -10,6 +10,7 @@ import java.util.function.Supplier; import net.minecraft.core.Registry; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.RegistryFriendlyByteBuf; import net.minecraft.network.codec.StreamCodec; import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.ChunkPos; @@ -91,6 +92,29 @@ public void encode(B buf, ResourceKey> value) { }; } + /** + * Creates a stream codec that uses different implementation depending on the {@link net.neoforged.neoforge.network.connection.ConnectionType}. + * Should be used to keep vanilla connection compatibility. + */ + public static StreamCodec connectionAware( + StreamCodec neoForgeCodec, + StreamCodec otherCodec) { + return new StreamCodec<>() { + @Override + public V decode(RegistryFriendlyByteBuf buf) { + return buf.getConnectionType().isNeoForge() ? neoForgeCodec.decode(buf) : otherCodec.decode(buf); + } + + @Override + public void encode(RegistryFriendlyByteBuf buf, V value) { + switch (buf.getConnectionType()) { + case NEOFORGE -> neoForgeCodec.encode(buf, value); + case OTHER -> otherCodec.encode(buf, value); + } + } + }; + } + public static StreamCodec composite( final StreamCodec codec1, final Function getter1, From 3e07df7c777cd241a71afc9e204ab7de83e27b58 Mon Sep 17 00:00:00 2001 From: RaymondBlaze Date: Sat, 1 Jun 2024 20:40:44 +0800 Subject: [PATCH 2/6] Optimize patches --- .../RegistryFriendlyByteBuf.java.patch | 24 ++++++++++++------- .../world/effect/MobEffectInstance.java.patch | 14 +++++------ .../network/codec/NeoForgeStreamCodecs.java | 5 +++- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch b/patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch index 6b91fc9187..077cafdec4 100644 --- a/patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch +++ b/patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch @@ -1,15 +1,19 @@ --- a/net/minecraft/network/RegistryFriendlyByteBuf.java +++ b/net/minecraft/network/RegistryFriendlyByteBuf.java -@@ -6,10 +_,22 @@ +@@ -6,16 +_,40 @@ public class RegistryFriendlyByteBuf extends FriendlyByteBuf { private final RegistryAccess registryAccess; + private final net.neoforged.neoforge.network.connection.ConnectionType connectionType; ++ /** ++ * @deprecated Neo: use overload with ConnectionType context ++ */ ++ @Deprecated public RegistryFriendlyByteBuf(ByteBuf p_320951_, RegistryAccess p_319803_) { super(p_320951_); this.registryAccess = p_319803_; -+ this.connectionType = net.neoforged.neoforge.network.connection.ConnectionType.NEOFORGE; ++ this.connectionType = net.neoforged.neoforge.network.connection.ConnectionType.OTHER; + } + + public RegistryFriendlyByteBuf(ByteBuf p_320951_, RegistryAccess p_319803_, net.neoforged.neoforge.network.connection.ConnectionType connectionType) { @@ -23,13 +27,17 @@ } public RegistryAccess registryAccess() { -@@ -18,5 +_,9 @@ + return this.registryAccess; + } - public static Function decorator(RegistryAccess p_320166_) { - return p_320793_ -> new RegistryFriendlyByteBuf(p_320793_, p_320166_); -+ } -+ + public static Function decorator(RegistryAccess p_320166_, net.neoforged.neoforge.network.connection.ConnectionType connectionType) { + return p_320793_ -> new RegistryFriendlyByteBuf(p_320793_, p_320166_, connectionType); ++ } ++ ++ /** ++ * @deprecated Neo: use overload with ConnectionType context ++ */ ++ @Deprecated + public static Function decorator(RegistryAccess p_320166_) { + return p_320793_ -> new RegistryFriendlyByteBuf(p_320793_, p_320166_); } - } diff --git a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch index e70ebe7b86..eb856f2992 100644 --- a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch +++ b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch @@ -76,13 +76,12 @@ public static final MapCodec MAP_CODEC = MapCodec.recursive( "MobEffectInstance.Details", p_323465_ -> RecordCodecBuilder.mapCodec( -@@ -408,13 +_,14 @@ - Codec.BOOL.optionalFieldOf("ambient", Boolean.valueOf(false)).forGetter(MobEffectInstance.Details::ambient), +@@ -409,12 +_,14 @@ Codec.BOOL.optionalFieldOf("show_particles", Boolean.valueOf(true)).forGetter(MobEffectInstance.Details::showParticles), Codec.BOOL.optionalFieldOf("show_icon").forGetter(p_323788_ -> Optional.of(p_323788_.showIcon())), -- p_323465_.optionalFieldOf("hidden_effect").forGetter(MobEffectInstance.Details::hiddenEffect) -+ p_323465_.optionalFieldOf("hidden_effect").forGetter(MobEffectInstance.Details::hiddenEffect), -+ net.neoforged.neoforge.common.util.NeoForgeExtraCodecs.setOf(net.neoforged.neoforge.common.EffectCure.CODEC).optionalFieldOf("neoforge:cures").forGetter(MobEffectInstance.Details::cures) + p_323465_.optionalFieldOf("hidden_effect").forGetter(MobEffectInstance.Details::hiddenEffect) ++ // Neo: Add additional serialization logic for custom EffectCure(s) ++ , net.neoforged.neoforge.common.util.NeoForgeExtraCodecs.setOf(net.neoforged.neoforge.common.EffectCure.CODEC).optionalFieldOf("neoforge:cures").forGetter(MobEffectInstance.Details::cures) ) .apply(p_324063_, MobEffectInstance.Details::create) ) @@ -94,11 +93,12 @@ ByteBufCodecs.VAR_INT, MobEffectInstance.Details::amplifier, ByteBufCodecs.VAR_INT, -@@ -427,14 +_,16 @@ +@@ -427,14 +_,17 @@ MobEffectInstance.Details::showIcon, p_329990_.apply(ByteBufCodecs::optional), MobEffectInstance.Details::hiddenEffect, -+ net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.cast().>apply(ByteBufCodecs.collection(java.util.HashSet::new)).apply(codec -> net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.connectionAware(ByteBufCodecs.optional(codec), StreamCodec.unit(Optional.empty()))), ++ // Neo: Add additional serialization logic for custom EffectCure(s) ++ net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.connectionAware(ByteBufCodecs.optional(net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.apply(ByteBufCodecs.collection(java.util.HashSet::new))), StreamCodec.unit(Optional.empty())), + MobEffectInstance.Details::cures, MobEffectInstance.Details::new ) diff --git a/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java b/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java index b87dca825e..96ebbed7fb 100644 --- a/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java +++ b/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java @@ -102,7 +102,10 @@ public static StreamCodec connectionAware( return new StreamCodec<>() { @Override public V decode(RegistryFriendlyByteBuf buf) { - return buf.getConnectionType().isNeoForge() ? neoForgeCodec.decode(buf) : otherCodec.decode(buf); + return switch (buf.getConnectionType()) { + case NEOFORGE -> neoForgeCodec.decode(buf); + case OTHER -> otherCodec.decode(buf); + }; } @Override From cd35a6e7f9fdb1ab84e354cdaf89ce13b7908b38 Mon Sep 17 00:00:00 2001 From: RaymondBlaze Date: Sat, 1 Jun 2024 21:20:32 +0800 Subject: [PATCH 3/6] Fix NeoForge's RegistryFriendlyByteBuf usages --- .../neoforged/neoforge/common/util/FriendlyByteBufUtil.java | 3 ++- .../neoforge/network/handlers/ClientPayloadHandler.java | 4 ++-- .../neoforge/debug/entity/EntityDataSerializerTest.java | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/neoforged/neoforge/common/util/FriendlyByteBufUtil.java b/src/main/java/net/neoforged/neoforge/common/util/FriendlyByteBufUtil.java index eddc5db4f6..2a7c32f214 100644 --- a/src/main/java/net/neoforged/neoforge/common/util/FriendlyByteBufUtil.java +++ b/src/main/java/net/neoforged/neoforge/common/util/FriendlyByteBufUtil.java @@ -10,6 +10,7 @@ import net.minecraft.core.RegistryAccess; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.RegistryFriendlyByteBuf; +import net.neoforged.neoforge.network.connection.ConnectionType; /** * Utility class for working with {@link FriendlyByteBuf}s. @@ -27,7 +28,7 @@ private FriendlyByteBufUtil() { * @return The written data. */ public static byte[] writeCustomData(Consumer dataWriter, RegistryAccess registryAccess) { - final RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), registryAccess); + final RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), registryAccess, ConnectionType.NEOFORGE); try { dataWriter.accept(buf); return buf.array(); diff --git a/src/main/java/net/neoforged/neoforge/network/handlers/ClientPayloadHandler.java b/src/main/java/net/neoforged/neoforge/network/handlers/ClientPayloadHandler.java index ea228bbaf5..79a8b98959 100644 --- a/src/main/java/net/neoforged/neoforge/network/handlers/ClientPayloadHandler.java +++ b/src/main/java/net/neoforged/neoforge/network/handlers/ClientPayloadHandler.java @@ -87,7 +87,7 @@ public static void handle(AdvancedAddEntityPayload advancedAddEntityPayload, IPa try { Entity entity = context.player().level().getEntity(advancedAddEntityPayload.entityId()); if (entity instanceof IEntityWithComplexSpawn entityAdditionalSpawnData) { - final RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.wrappedBuffer(advancedAddEntityPayload.customPayload()), entity.registryAccess()); + final RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.wrappedBuffer(advancedAddEntityPayload.customPayload()), entity.registryAccess(), context.listener().getConnectionType()); try { entityAdditionalSpawnData.readSpawnData(buf); } finally { @@ -102,7 +102,7 @@ public static void handle(AdvancedAddEntityPayload advancedAddEntityPayload, IPa public static void handle(AdvancedOpenScreenPayload msg, IPayloadContext context) { Minecraft mc = Minecraft.getInstance(); RegistryAccess registryAccess = mc.player.registryAccess(); - final RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.wrappedBuffer(msg.additionalData()), registryAccess); + final RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.wrappedBuffer(msg.additionalData()), registryAccess, context.listener().getConnectionType()); try { createMenuScreen(msg.name(), msg.menuType(), msg.windowId(), buf); } catch (Throwable t) { diff --git a/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityDataSerializerTest.java b/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityDataSerializerTest.java index 9ae494f84d..fa7145d562 100644 --- a/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityDataSerializerTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/debug/entity/EntityDataSerializerTest.java @@ -23,6 +23,7 @@ import net.minecraft.world.entity.MobCategory; import net.minecraft.world.level.Level; import net.neoforged.neoforge.common.CommonHooks; +import net.neoforged.neoforge.network.connection.ConnectionType; import net.neoforged.neoforge.registries.DeferredHolder; import net.neoforged.neoforge.registries.NeoForgeRegistries; import net.neoforged.testframework.DynamicTest; @@ -63,7 +64,7 @@ static void customEntityDataSerializer(final DynamicTest test, final Registratio } var pkt = new ClientboundSetEntityDataPacket(entity.getId(), items); FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); - ClientboundSetEntityDataPacket.STREAM_CODEC.encode(new RegistryFriendlyByteBuf(buf, helper.getLevel().registryAccess()), pkt); + ClientboundSetEntityDataPacket.STREAM_CODEC.encode(new RegistryFriendlyByteBuf(buf, helper.getLevel().registryAccess(), ConnectionType.NEOFORGE), pkt); helper.assertTrue(buf.readVarInt() == entity.getId(), "Entity ID didn't match"); // Drop entity ID buf.readByte(); // Drop item ID int expectedId = NeoForgeRegistries.ENTITY_DATA_SERIALIZERS.getId(TEST_SERIALIZER.get()) + CommonHooks.VANILLA_SERIALIZER_LIMIT; From 845f1d17af4db1f5ba1e5899dfbdcd8eb63f4abc Mon Sep 17 00:00:00 2001 From: RaymondBlaze Date: Tue, 4 Jun 2024 10:30:08 +0800 Subject: [PATCH 4/6] Replace `StreamCodec#unit` with `NeoForgeStreamCodecs#uncheckedUnit` --- .../world/effect/MobEffectInstance.java.patch | 2 +- .../network/codec/NeoForgeStreamCodecs.java | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch index eb856f2992..29a017f5c3 100644 --- a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch +++ b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch @@ -98,7 +98,7 @@ p_329990_.apply(ByteBufCodecs::optional), MobEffectInstance.Details::hiddenEffect, + // Neo: Add additional serialization logic for custom EffectCure(s) -+ net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.connectionAware(ByteBufCodecs.optional(net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.apply(ByteBufCodecs.collection(java.util.HashSet::new))), StreamCodec.unit(Optional.empty())), ++ net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.connectionAware(ByteBufCodecs.optional(net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.apply(ByteBufCodecs.collection(java.util.HashSet::new))), net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.uncheckedUnit(Optional.empty())), + MobEffectInstance.Details::cures, MobEffectInstance.Details::new ) diff --git a/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java b/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java index 96ebbed7fb..b752d7552c 100644 --- a/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java +++ b/src/main/java/net/neoforged/neoforge/network/codec/NeoForgeStreamCodecs.java @@ -118,6 +118,21 @@ public void encode(RegistryFriendlyByteBuf buf, V value) { }; } + /** + * Similar to {@link StreamCodec#unit(Object)}, but without checks for the value to be encoded. + */ + public static StreamCodec uncheckedUnit(final V defaultValue) { + return new StreamCodec<>() { + @Override + public V decode(B buf) { + return defaultValue; + } + + @Override + public void encode(B buf, V value) {} + }; + } + public static StreamCodec composite( final StreamCodec codec1, final Function getter1, @@ -134,7 +149,7 @@ public static StreamCodec composite( final StreamCodec codec7, final Function getter7, final Function7 p_331335_) { - return new StreamCodec() { + return new StreamCodec<>() { @Override public C decode(B p_330310_) { T1 t1 = codec1.decode(p_330310_); From e751fa764063dff199635c664aabae9dbe4525fa Mon Sep 17 00:00:00 2001 From: RaymondBlaze Date: Wed, 5 Jun 2024 11:10:10 +0800 Subject: [PATCH 5/6] Reformat patches --- .../minecraft/world/effect/MobEffectInstance.java.patch | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch index 29a017f5c3..4120df8315 100644 --- a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch +++ b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch @@ -93,12 +93,15 @@ ByteBufCodecs.VAR_INT, MobEffectInstance.Details::amplifier, ByteBufCodecs.VAR_INT, -@@ -427,14 +_,17 @@ +@@ -427,14 +_,20 @@ MobEffectInstance.Details::showIcon, p_329990_.apply(ByteBufCodecs::optional), MobEffectInstance.Details::hiddenEffect, + // Neo: Add additional serialization logic for custom EffectCure(s) -+ net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.connectionAware(ByteBufCodecs.optional(net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.apply(ByteBufCodecs.collection(java.util.HashSet::new))), net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.uncheckedUnit(Optional.empty())), ++ net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.connectionAware( ++ ByteBufCodecs.optional(net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.apply(ByteBufCodecs.collection(java.util.HashSet::new))), ++ net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.uncheckedUnit(Optional.empty()) ++ ), + MobEffectInstance.Details::cures, MobEffectInstance.Details::new ) From e71a8d4206bebeae304a4e0aa6f0168b080f3685 Mon Sep 17 00:00:00 2001 From: RaymondBlaze Date: Sat, 8 Jun 2024 21:11:12 +0800 Subject: [PATCH 6/6] Add back vanilla methods --- .../world/effect/MobEffectInstance.java.patch | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch index 4120df8315..3731b52c95 100644 --- a/patches/net/minecraft/world/effect/MobEffectInstance.java.patch +++ b/patches/net/minecraft/world/effect/MobEffectInstance.java.patch @@ -93,7 +93,7 @@ ByteBufCodecs.VAR_INT, MobEffectInstance.Details::amplifier, ByteBufCodecs.VAR_INT, -@@ -427,14 +_,20 @@ +@@ -427,10 +_,28 @@ MobEffectInstance.Details::showIcon, p_329990_.apply(ByteBufCodecs::optional), MobEffectInstance.Details::hiddenEffect, @@ -107,12 +107,18 @@ ) ); - private static MobEffectInstance.Details create( -- int p_323657_, int p_324205_, boolean p_324263_, boolean p_324000_, Optional p_323607_, Optional p_324604_ ++ @Deprecated ++ Details(int amplifier, int duration, boolean ambient, boolean showParticles, boolean showIcon, Optional hiddenEffect) { ++ this(amplifier, duration, ambient, showParticles, showIcon, hiddenEffect, Optional.empty()); ++ } ++ ++ private static MobEffectInstance.Details create( + int p_323657_, int p_324205_, boolean p_324263_, boolean p_324000_, Optional p_323607_, Optional p_324604_, Optional> cures - ) { -- return new MobEffectInstance.Details(p_323657_, p_324205_, p_324263_, p_324000_, p_323607_.orElse(p_324000_), p_324604_); ++ ) { + return new MobEffectInstance.Details(p_323657_, p_324205_, p_324263_, p_324000_, p_323607_.orElse(p_324000_), p_324604_, cures); - } - } - } ++ } ++ ++ @Deprecated + private static MobEffectInstance.Details create( + int p_323657_, int p_324205_, boolean p_324263_, boolean p_324000_, Optional p_323607_, Optional p_324604_ + ) {