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
43 changes: 43 additions & 0 deletions patches/net/minecraft/network/RegistryFriendlyByteBuf.java.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--- a/net/minecraft/network/RegistryFriendlyByteBuf.java
+++ b/net/minecraft/network/RegistryFriendlyByteBuf.java
@@ -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
Technici4n marked this conversation as resolved.
Show resolved Hide resolved
+ */
+ @Deprecated
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.OTHER;
+ }
+
+ 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() {
return this.registryAccess;
}

+ 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);
+ }
+
+ /**
+ * @deprecated Neo: use overload with ConnectionType context
Technici4n marked this conversation as resolved.
Show resolved Hide resolved
+ */
+ @Deprecated
public static Function<ByteBuf, RegistryFriendlyByteBuf> decorator(RegistryAccess p_320166_) {
return p_320793_ -> new RegistryFriendlyByteBuf(p_320793_, p_320166_);
}
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
45 changes: 25 additions & 20 deletions patches/net/minecraft/world/effect/MobEffectInstance.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
public static final MapCodec<MobEffectInstance.Details> MAP_CODEC = MapCodec.recursive(
"MobEffectInstance.Details",
p_323465_ -> RecordCodecBuilder.mapCodec(
@@ -409,10 +_,13 @@
@@ -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)
Expand All @@ -86,34 +86,39 @@
.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,10 +_,28 @@
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,
+ // 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())
RaymondBlaze marked this conversation as resolved.
Show resolved Hide resolved
+ ),
+ MobEffectInstance.Details::cures,
MobEffectInstance.Details::new
)
);

private static MobEffectInstance.Details create(
- int p_323657_, int p_324205_, boolean p_324263_, boolean p_324000_, Optional<Boolean> p_323607_, Optional<MobEffectInstance.Details> p_324604_
+ 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);
+ @Deprecated
+ Details(int amplifier, int duration, boolean ambient, boolean showParticles, boolean showIcon, Optional<MobEffectInstance.Details> hiddenEffect) {
+ this(amplifier, duration, ambient, showParticles, showIcon, hiddenEffect, Optional.empty());
+ }
+
+ // 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());
+ private static MobEffectInstance.Details create(
+ 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_, cures);
+ }
+
}
}
+ @Deprecated
private static MobEffectInstance.Details create(
int p_323657_, int p_324205_, boolean p_324263_, boolean p_324000_, Optional<Boolean> p_323607_, Optional<MobEffectInstance.Details> p_324604_
) {
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -27,7 +28,7 @@ private FriendlyByteBufUtil() {
* @return The written data.
*/
public static byte[] writeCustomData(Consumer<RegistryFriendlyByteBuf> dataWriter, RegistryAccess registryAccess) {
final RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), registryAccess);
final RegistryFriendlyByteBuf buf = new RegistryFriendlyByteBuf(Unpooled.buffer(), registryAccess, ConnectionType.NEOFORGE);
Technici4n marked this conversation as resolved.
Show resolved Hide resolved
try {
dataWriter.accept(buf);
return buf.array();
Expand Down
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,47 @@ 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 switch (buf.getConnectionType()) {
case NEOFORGE -> neoForgeCodec.decode(buf);
case OTHER -> 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);
}
}
};
}

/**
* Similar to {@link StreamCodec#unit(Object)}, but without checks for the value to be encoded.
*/
public static <B, V> StreamCodec<B, V> 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 <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 All @@ -107,7 +149,7 @@ public static <B, C, T1, T2, T3, T4, T5, T6, T7> StreamCodec<B, C> composite(
final StreamCodec<? super B, T7> codec7,
final Function<C, T7> getter7,
final Function7<T1, T2, T3, T4, T5, T6, T7, C> p_331335_) {
return new StreamCodec<B, C>() {
return new StreamCodec<>() {
@Override
public C decode(B p_330310_) {
T1 t1 = codec1.decode(p_330310_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading