package com.arslaancodes.arslaansmagichax.mixin; import io.netty.channel.ChannelHandlerContext; import com.arslaancodes.arslaansmagichax.events.PacketEvent; import com.arslaancodes.arslaansmagichax.events.ReceivePacketEventBus; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import java.util.Iterator; import net.minecraft.network.Connection; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientboundBundlePacket; @Mixin(Connection.class) public class ClientConnectionMixin { @Inject(method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/protocol/Packet;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/Connection;genericsFtw(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketListener;)V", shift = At.Shift.BEFORE), cancellable = true) private void onHandlePacket(ChannelHandlerContext channelHandlerContext, Packet packet, CallbackInfo ci) { if (packet instanceof ClientboundBundlePacket bundle) { for (Iterator> it = bundle.subPackets().iterator(); it.hasNext(); ) { Packet innerPacket = it.next(); PacketEvent.Receive ev = new PacketEvent.Receive(innerPacket); ReceivePacketEventBus.post(ev); if (ev.isCancelled()) it.remove(); } } else { PacketEvent.Receive ev = new PacketEvent.Receive(packet); ReceivePacketEventBus.post(ev); if (ev.isCancelled()) ci.cancel(); } } }