From d14d193dec3734cb66b5f36290b51f049ba18e2e Mon Sep 17 00:00:00 2001 From: Wyvest <45589059+Wyvest@users.noreply.github.com> Date: Sat, 4 Jun 2022 22:09:25 +0700 Subject: receive and send packet events (#32) * move from mod events to mixin * packet events --- .../oneconfig/mixin/NetHandlerPlayClientMixin.java | 14 ++++++++++++- .../oneconfig/mixin/NetworkManagerMixin.java | 23 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cc/polyfrost/oneconfig/mixin/NetworkManagerMixin.java (limited to 'src/main/java/cc/polyfrost/oneconfig/mixin') diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/NetHandlerPlayClientMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/NetHandlerPlayClientMixin.java index c7056d5..5d78531 100644 --- a/src/main/java/cc/polyfrost/oneconfig/mixin/NetHandlerPlayClientMixin.java +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/NetHandlerPlayClientMixin.java @@ -2,15 +2,27 @@ package cc.polyfrost.oneconfig.mixin; import cc.polyfrost.oneconfig.events.EventManager; import cc.polyfrost.oneconfig.events.event.ChatReceiveEvent; +import cc.polyfrost.oneconfig.events.event.SendPacketEvent; import net.minecraft.client.network.NetHandlerPlayClient; +import net.minecraft.network.Packet; import net.minecraft.network.play.server.S02PacketChat; 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; -@Mixin(NetHandlerPlayClient.class) +@Mixin(value = NetHandlerPlayClient.class, priority = Integer.MAX_VALUE) public class NetHandlerPlayClientMixin { + + @Inject(method = "addToSendQueue", at = @At("HEAD"), cancellable = true) + private void onSendPacket(Packet p_147297_1_, CallbackInfo ci) { + SendPacketEvent event = new SendPacketEvent(p_147297_1_); + EventManager.INSTANCE.post(event); + if (event.isCancelled) { + ci.cancel(); + } + } + @Inject(method = "handleChat", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/event/ForgeEventFactory;onClientChat(BLnet/minecraft/util/IChatComponent;)Lnet/minecraft/util/IChatComponent;", remap = false), cancellable = true, remap = true) private void onClientChat(S02PacketChat packetIn, CallbackInfo ci) { if (packetIn.getType() == 0) { diff --git a/src/main/java/cc/polyfrost/oneconfig/mixin/NetworkManagerMixin.java b/src/main/java/cc/polyfrost/oneconfig/mixin/NetworkManagerMixin.java new file mode 100644 index 0000000..e7a9396 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/mixin/NetworkManagerMixin.java @@ -0,0 +1,23 @@ +package cc.polyfrost.oneconfig.mixin; + +import cc.polyfrost.oneconfig.events.EventManager; +import cc.polyfrost.oneconfig.events.event.ReceivePacketEvent; +import io.netty.channel.ChannelHandlerContext; +import net.minecraft.network.NetworkManager; +import net.minecraft.network.Packet; +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; + +@Mixin(value = NetworkManager.class, priority = Integer.MAX_VALUE) +public class NetworkManagerMixin { + @Inject(method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/Packet;)V", at = @At("HEAD"), cancellable = true) + private void onReceivePacket(ChannelHandlerContext p_channelRead0_1_, Packet p_channelRead0_2_, CallbackInfo ci) { + ReceivePacketEvent event = new ReceivePacketEvent(p_channelRead0_2_); + EventManager.INSTANCE.post(event); + if (event.isCancelled) { + ci.cancel(); + } + } +} -- cgit