diff options
author | Wyvest <45589059+Wyvest@users.noreply.github.com> | 2022-06-04 22:09:25 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-04 17:09:25 +0200 |
commit | d14d193dec3734cb66b5f36290b51f049ba18e2e (patch) | |
tree | 3e26e871097ce7028efb03b33803501b2ba7b4c9 | |
parent | 140ffd89293cdad8963d1fc80db382dc5681229f (diff) | |
download | OneConfig-d14d193dec3734cb66b5f36290b51f049ba18e2e.tar.gz OneConfig-d14d193dec3734cb66b5f36290b51f049ba18e2e.tar.bz2 OneConfig-d14d193dec3734cb66b5f36290b51f049ba18e2e.zip |
receive and send packet events (#32)
* move from mod events to mixin
* packet events
6 files changed, 66 insertions, 1 deletions
diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/ReceivePacketEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/ReceivePacketEvent.java new file mode 100644 index 0000000..274f7a4 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/ReceivePacketEvent.java @@ -0,0 +1,10 @@ +package cc.polyfrost.oneconfig.events.event; + +import net.minecraft.network.Packet; + +public class ReceivePacketEvent extends CancellableEvent { + public final Packet<?> packet; + public ReceivePacketEvent(Packet<?> packet) { + this.packet = packet; + } +} diff --git a/src/main/java/cc/polyfrost/oneconfig/events/event/SendPacketEvent.java b/src/main/java/cc/polyfrost/oneconfig/events/event/SendPacketEvent.java new file mode 100644 index 0000000..f149d59 --- /dev/null +++ b/src/main/java/cc/polyfrost/oneconfig/events/event/SendPacketEvent.java @@ -0,0 +1,10 @@ +package cc.polyfrost.oneconfig.events.event; + +import net.minecraft.network.Packet; + +public class SendPacketEvent extends CancellableEvent { + public final Packet<?> packet; + public SendPacketEvent(Packet<?> packet) { + this.packet = packet; + } +} 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(); + } + } +} diff --git a/src/main/kotlin/cc/polyfrost/oneconfig/utils/dsl/DSLs.kt b/src/main/kotlin/cc/polyfrost/oneconfig/utils/dsl/DSLs.kt new file mode 100644 index 0000000..a71e3f1 --- /dev/null +++ b/src/main/kotlin/cc/polyfrost/oneconfig/utils/dsl/DSLs.kt @@ -0,0 +1,9 @@ +package cc.polyfrost.oneconfig.utils.dsl + +import cc.polyfrost.oneconfig.libs.universal.UMinecraft + +/** + * Gets the current [net.minecraft.client.Minecraft] instance. + */ +val mc +get() = UMinecraft.getMinecraft()
\ No newline at end of file diff --git a/src/main/resources/mixins.oneconfig.json b/src/main/resources/mixins.oneconfig.json index 86e61e1..fa23932 100644 --- a/src/main/resources/mixins.oneconfig.json +++ b/src/main/resources/mixins.oneconfig.json @@ -12,6 +12,7 @@ "GuiIngameForgeMixin", "MinecraftMixin", "NetHandlerPlayClientMixin", + "NetworkManagerMixin", "ShaderGroupAccessor", "WorldClientMixin" ] |