Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EffectCure syncing by introducing connection aware StreamCodec #1041

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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_);
Expand Down
35 changes: 35 additions & 0 deletions patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch
Original file line number Diff line number Diff line change
@@ -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_) {
RaymondBlaze marked this conversation as resolved.
Show resolved Hide resolved
super(p_320951_);
this.registryAccess = p_319803_;
+ this.connectionType = net.neoforged.neoforge.network.connection.ConnectionType.NEOFORGE;
RaymondBlaze marked this conversation as resolved.
Show resolved Hide resolved
+ }
+
+ 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<ByteBuf, RegistryFriendlyByteBuf> decorator(RegistryAccess p_320166_) {
RaymondBlaze marked this conversation as resolved.
Show resolved Hide resolved
return p_320793_ -> new RegistryFriendlyByteBuf(p_320793_, p_320166_);
+ }
+
+ public static Function<ByteBuf, RegistryFriendlyByteBuf> decorator(RegistryAccess p_320166_, net.neoforged.neoforge.network.connection.ConnectionType connectionType) {
+ return p_320793_ -> new RegistryFriendlyByteBuf(p_320793_, p_320166_, connectionType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions patches/net/minecraft/server/players/PlayerList.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@

public PlayerList(MinecraftServer p_203842_, LayeredRegistryAccess<RegistryLayer> 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()));
Expand Down
34 changes: 15 additions & 19 deletions patches/net/minecraft/world/effect/MobEffectInstance.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,30 @@
public static final MapCodec<MobEffectInstance.Details> MAP_CODEC = MapCodec.recursive(
"MobEffectInstance.Details",
p_323465_ -> RecordCodecBuilder.mapCodec(
@@ -409,10 +_,13 @@
@@ -408,13 +_,14 @@
Codec.BOOL.optionalFieldOf("ambient", Boolean.valueOf(false)).forGetter(MobEffectInstance.Details::ambient),
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)
+ // 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)
- p_323465_.optionalFieldOf("hidden_effect").forGetter(MobEffectInstance.Details::hiddenEffect)
+ p_323465_.optionalFieldOf("hidden_effect").forGetter(MobEffectInstance.Details::hiddenEffect),
RaymondBlaze marked this conversation as resolved.
Show resolved Hide resolved
+ 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)
)
);
+ // TODO: https://github.com/neoforged/NeoForge/issues/986 - Fix this to have vanilla-compatible sync.
public static final StreamCodec<ByteBuf, MobEffectInstance.Details> STREAM_CODEC = StreamCodec.recursive(
p_329990_ -> StreamCodec.composite(
- public static final StreamCodec<ByteBuf, MobEffectInstance.Details> STREAM_CODEC = StreamCodec.recursive(
- p_329990_ -> StreamCodec.composite(
+ public static final StreamCodec<RegistryFriendlyByteBuf, MobEffectInstance.Details> STREAM_CODEC = StreamCodec.recursive(
+ p_329990_ -> net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.composite(
ByteBufCodecs.VAR_INT,
@@ -427,14 +_,23 @@
MobEffectInstance.Details::amplifier,
ByteBufCodecs.VAR_INT,
@@ -427,14 +_,16 @@
MobEffectInstance.Details::showIcon,
p_329990_.apply(ByteBufCodecs::optional),
MobEffectInstance.Details::hiddenEffect,
+ // net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.<java.util.Set<net.neoforged.neoforge.common.EffectCure>>apply(ByteBufCodecs.collection(java.util.HashSet::new)).apply(ByteBufCodecs::optional),
+ // MobEffectInstance.Details::cures,
+ net.neoforged.neoforge.common.EffectCure.STREAM_CODEC.<RegistryFriendlyByteBuf>cast().<java.util.Set<net.neoforged.neoforge.common.EffectCure>>apply(ByteBufCodecs.collection(java.util.HashSet::new)).apply(codec -> net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs.connectionAware(ByteBufCodecs.optional(codec), StreamCodec.unit(Optional.empty()))),
RaymondBlaze marked this conversation as resolved.
Show resolved Hide resolved
+ MobEffectInstance.Details::cures,
MobEffectInstance.Details::new
)
);
Expand All @@ -105,15 +109,7 @@
+ int p_323657_, int p_324205_, boolean p_324263_, boolean p_324000_, Optional<Boolean> p_323607_, Optional<MobEffectInstance.Details> p_324604_, Optional<java.util.Set<net.neoforged.neoforge.common.EffectCure>> 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);
+ }
+
+ // TODO: https://github.com/neoforged/NeoForge/issues/986 - Delete these once custom cure sync is fixed.
+ @Deprecated(forRemoval = true, since = "1.20.6")
+ private Details(int amplifier, int duration, boolean ambient, boolean showParticles, boolean showIcon, Optional<MobEffectInstance.Details> hiddenEffect) {
RaymondBlaze marked this conversation as resolved.
Show resolved Hide resolved
+ this(amplifier, duration, ambient, showParticles, showIcon, hiddenEffect, Optional.empty());
+ }
+
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,6 +92,29 @@ public void encode(B buf, ResourceKey<? extends Registry<?>> 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 <V> StreamCodec<RegistryFriendlyByteBuf, V> connectionAware(
StreamCodec<? super RegistryFriendlyByteBuf, V> neoForgeCodec,
StreamCodec<? super RegistryFriendlyByteBuf, V> otherCodec) {
return new StreamCodec<>() {
@Override
public V decode(RegistryFriendlyByteBuf buf) {
return buf.getConnectionType().isNeoForge() ? neoForgeCodec.decode(buf) : otherCodec.decode(buf);
RaymondBlaze marked this conversation as resolved.
Show resolved Hide resolved
}

@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 <B, C, T1, T2, T3, T4, T5, T6, T7> StreamCodec<B, C> composite(
final StreamCodec<? super B, T1> codec1,
final Function<C, T1> getter1,
Expand Down
Loading