|
13 | 13 | import net.minecraft.network.protocol.common.ServerCommonPacketListener; |
14 | 14 | import net.minecraft.network.protocol.common.ServerboundCustomPayloadPacket; |
15 | 15 | import net.minecraft.network.protocol.common.custom.CustomPacketPayload; |
| 16 | +import net.minecraft.network.protocol.configuration.ServerConfigurationPacketListener; |
16 | 17 | import net.minecraft.resources.ResourceLocation; |
17 | 18 | import net.neoforged.neoforge.common.extensions.ICommonPacketListener; |
| 19 | +import net.neoforged.neoforge.network.negotiation.NegotiatedNetworkComponent; |
| 20 | +import net.neoforged.neoforge.network.negotiation.NegotiationResult; |
| 21 | +import net.neoforged.neoforge.network.payload.ModdedNetworkQueryComponent; |
18 | 22 | import net.neoforged.neoforge.network.registration.NetworkRegistry; |
19 | 23 | import org.sinytra.fabric.networking_api.NeoNetworkRegistrar; |
20 | 24 | import org.spongepowered.asm.mixin.Mixin; |
21 | 25 | import org.spongepowered.asm.mixin.injection.At; |
22 | 26 | import org.spongepowered.asm.mixin.injection.Inject; |
| 27 | +import org.spongepowered.asm.mixin.injection.ModifyVariable; |
23 | 28 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 29 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.Set; |
24 | 35 |
|
25 | 36 | @Mixin(NetworkRegistry.class) |
26 | 37 | public class NetworkRegistryMixin { |
27 | 38 |
|
28 | | - @ModifyReturnValue(method = "getCodec", at = @At(value = "RETURN", ordinal = 1)) |
29 | | - private static StreamCodec<? super FriendlyByteBuf, ? extends CustomPacketPayload> getCodec(StreamCodec<? super FriendlyByteBuf, ? extends CustomPacketPayload> codec, ResourceLocation id, ConnectionProtocol protocol, PacketFlow flow) { |
| 39 | + @Inject(method = "getCodec", at = @At(value = "INVOKE", target = "Lorg/slf4j/Logger;warn(Ljava/lang/String;Ljava/lang/Object;)V", ordinal = 0), cancellable = true) |
| 40 | + private static void getCodec(ResourceLocation id, ConnectionProtocol protocol, PacketFlow flow, CallbackInfoReturnable<StreamCodec<? super FriendlyByteBuf, ? extends CustomPacketPayload>> cir) { |
30 | 41 | PayloadTypeRegistryImpl<? extends FriendlyByteBuf> registry = NeoNetworkRegistrar.getPayloadRegistry(protocol, flow); |
31 | 42 | CustomPacketPayload.TypeAndCodec<? extends FriendlyByteBuf, ? extends CustomPacketPayload> fabricCodec = registry.get(id); |
32 | | - return fabricCodec != null ? (StreamCodec) fabricCodec.codec() : codec; |
| 43 | + if (fabricCodec != null) { |
| 44 | + cir.setReturnValue((StreamCodec) fabricCodec.codec()); |
| 45 | + } |
33 | 46 | } |
34 | 47 |
|
35 | 48 | @ModifyReturnValue(method = "getCodec", at = @At(value = "RETURN", ordinal = 3)) |
@@ -69,4 +82,18 @@ private static boolean includeFabricChannels(ICommonPacketListener listener, Res |
69 | 82 | // TODO Use original args that include the packet |
70 | 83 | return original.call(listener, location) || NeoNetworkRegistrar.hasCodecFor(listener.protocol(), listener.flow() == PacketFlow.SERVERBOUND ? PacketFlow.CLIENTBOUND : PacketFlow.SERVERBOUND, location); |
71 | 84 | } |
| 85 | + |
| 86 | + @ModifyVariable(method = "initializeNeoForgeConnection", at = @At(value = "INVOKE", target = "Lnet/neoforged/neoforge/network/registration/NetworkPayloadSetup;from(Ljava/util/Map;)Lnet/neoforged/neoforge/network/registration/NetworkPayloadSetup;"), ordinal = 1) |
| 87 | + private static Map<ConnectionProtocol, NegotiationResult> preserveSendableChannels(Map<ConnectionProtocol, NegotiationResult> results, ServerConfigurationPacketListener listener, Map<ConnectionProtocol, Set<ModdedNetworkQueryComponent>> clientChannels) { |
| 88 | + Set<ModdedNetworkQueryComponent> channels = clientChannels.get(ConnectionProtocol.PLAY); |
| 89 | + if (channels != null && !channels.isEmpty()) { |
| 90 | + NegotiationResult negotiation = results.get(ConnectionProtocol.PLAY); |
| 91 | + List<NegotiatedNetworkComponent> components = new ArrayList<>(negotiation.components()); |
| 92 | + channels.stream() |
| 93 | + .filter(c -> PayloadTypeRegistryImpl.PLAY_S2C.get(c.id()) != null) |
| 94 | + .forEach(c -> components.add(new NegotiatedNetworkComponent(c.id(), c.version()))); |
| 95 | + results.put(ConnectionProtocol.PLAY, new NegotiationResult(components, negotiation.success(), negotiation.failureReasons())); |
| 96 | + } |
| 97 | + return results; |
| 98 | + } |
72 | 99 | } |
0 commit comments