Skip to content

Commit 5883c88

Browse files
emyfopsbladekt
authored andcommitted
refactor: packet event types
1 parent d94821b commit 5883c88

File tree

4 files changed

+72
-22
lines changed

4 files changed

+72
-22
lines changed

common/src/main/java/com/lambda/mixin/ClientConnectionMixin.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import io.netty.channel.ChannelHandlerContext;
77
import net.minecraft.network.ClientConnection;
88
import net.minecraft.network.NetworkSide;
9+
import net.minecraft.network.listener.ClientPacketListener;
910
import net.minecraft.network.listener.PacketListener;
11+
import net.minecraft.network.listener.ServerPacketListener;
1012
import net.minecraft.network.packet.Packet;
1113
import net.minecraft.network.packet.c2s.handshake.ConnectionIntent;
1214
import net.minecraft.text.Text;
@@ -25,14 +27,14 @@ public class ClientConnectionMixin {
2527

2628
@Inject(method = "send(Lnet/minecraft/network/packet/Packet;)V", at = @At("HEAD"), cancellable = true)
2729
private void sendingPacket(Packet<?> packet, final CallbackInfo callbackInfo) {
28-
if (EventFlow.post(new PacketEvent.Send.Pre(packet)).isCanceled()) {
30+
if (EventFlow.post(new PacketEvent.Send.Pre((Packet<ServerPacketListener>) packet)).isCanceled()) {
2931
callbackInfo.cancel();
3032
}
3133
}
3234

3335
@Inject(method = "send(Lnet/minecraft/network/packet/Packet;)V", at = @At("RETURN"))
3436
private void sendingPacketPost(Packet<?> packet, final CallbackInfo callbackInfo) {
35-
EventFlow.post(new PacketEvent.Send.Post(packet));
37+
EventFlow.post(new PacketEvent.Send.Post((Packet<ServerPacketListener>) packet));
3638
}
3739

3840
@Inject(method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/packet/Packet;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/network/ClientConnection;handlePacket(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/listener/PacketListener;)V", shift = At.Shift.BEFORE), cancellable = true, require = 1)
@@ -42,7 +44,7 @@ private void receivingPacket(
4244
CallbackInfo callbackInfo
4345
) {
4446
if (side != NetworkSide.CLIENTBOUND) return;
45-
if (EventFlow.post(new PacketEvent.Receive.Pre(packet)).isCanceled()) {
47+
if (EventFlow.post(new PacketEvent.Receive.Pre((Packet<ClientPacketListener>) packet)).isCanceled()) {
4648
callbackInfo.cancel();
4749
}
4850
}
@@ -55,7 +57,7 @@ private void receivingPacketPost(
5557
) {
5658
if (side != NetworkSide.CLIENTBOUND) return;
5759

58-
EventFlow.post(new PacketEvent.Receive.Post(packet));
60+
EventFlow.post(new PacketEvent.Receive.Post((Packet<ClientPacketListener>) packet));
5961
}
6062

6163
@Inject(method = "connect(Ljava/lang/String;ILnet/minecraft/network/listener/PacketListener;Lnet/minecraft/network/packet/c2s/handshake/ConnectionIntent;)V", at = @At("HEAD"))

common/src/main/kotlin/com/lambda/event/events/PacketEvent.kt

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import com.lambda.event.callback.Cancellable
66
import com.lambda.event.callback.ICancellable
77
import com.lambda.event.events.PacketEvent.Receive
88
import com.lambda.event.events.PacketEvent.Send
9+
import net.minecraft.network.listener.ClientPacketListener
10+
import net.minecraft.network.listener.ServerPacketListener
911
import net.minecraft.network.packet.Packet
1012

1113
/**
1214
* An abstract class representing a [PacketEvent] in the [EventFlow].
1315
*
1416
* A [PacketEvent] is a type of [Event] that is triggered when a packet is sent or received.
15-
* It has two subclasses: [Send] and [Receive],
17+
* It has two sealed subclasses: [Send] and [Receive],
1618
* which are triggered when a packet is sent and received, respectively.
1719
*
1820
* Each subclass has two further subclasses: `Pre` and `Post`,
@@ -25,20 +27,42 @@ import net.minecraft.network.packet.Packet
2527
*/
2628
abstract class PacketEvent : Event {
2729
/**
28-
* Representing a [PacketEvent] that is triggered when a packet is sent.
30+
* Represents a [PacketEvent] that is triggered when a packet is sent.
2931
* It has two subclasses: [Pre] and [Post], which are triggered before and after the packet is sent.
3032
*/
31-
abstract class Send : PacketEvent() {
32-
class Pre(val packet: Packet<*>) : Send(), ICancellable by Cancellable()
33-
class Post(val packet: Packet<*>) : Send()
33+
sealed class Send : PacketEvent() {
34+
/**
35+
* Represents the event triggered before a packet is sent.
36+
*
37+
* @param packet the packet that is about to be sent.
38+
*/
39+
class Pre(val packet: Packet<out ServerPacketListener>) : Send(), ICancellable by Cancellable()
40+
41+
/**
42+
* Represents the event triggered after a packet is sent.
43+
*
44+
* @param packet the packet that has been sent.
45+
*/
46+
class Post(val packet: Packet<out ServerPacketListener>) : Send()
3447
}
3548

3649
/**
37-
* Representing a `PacketEvent` that is triggered when a packet is received.
50+
* Represents a [PacketEvent] that is triggered when a packet is received.
3851
* It has two subclasses: [Pre] and [Post], which are triggered before and after the packet is received.
3952
*/
40-
abstract class Receive : PacketEvent() {
41-
class Pre(val packet: Packet<*>) : Receive(), ICancellable by Cancellable()
42-
class Post(val packet: Packet<*>) : Receive()
53+
sealed class Receive : PacketEvent() {
54+
/**
55+
* Represents the event triggered before a packet is received.
56+
*
57+
* @param packet the packet that is about to be received.
58+
*/
59+
class Pre(val packet: Packet<out ClientPacketListener>) : Receive(), ICancellable by Cancellable()
60+
61+
/**
62+
* Represents the event triggered after a packet is received.
63+
*
64+
* @param packet the packet that has been received.
65+
*/
66+
class Post(val packet: Packet<out ClientPacketListener>) : Receive()
4367
}
4468
}

common/src/main/kotlin/com/lambda/module/modules/movement/Blink.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import com.lambda.module.Module
1010
import com.lambda.module.modules.client.GuiSettings
1111
import com.lambda.module.modules.combat.KillAura
1212
import com.lambda.module.tag.ModuleTag
13+
import com.lambda.util.PacketUtils.handlePacketSilently
1314
import com.lambda.util.PacketUtils.sendPacketSilently
1415
import com.lambda.util.math.ColorUtils.setAlpha
15-
import net.minecraft.network.ClientConnection
16+
import net.minecraft.network.listener.ServerPacketListener
1617
import net.minecraft.network.packet.Packet
1718
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket
1819
import net.minecraft.network.packet.s2c.play.EntityVelocityUpdateS2CPacket
@@ -31,7 +32,7 @@ object Blink : Module(
3132

3233
private val isActive get() = (KillAura.isEnabled && KillAura.target != null) || !requiresAura
3334

34-
private var packetPool = ConcurrentLinkedDeque<Packet<*>>()
35+
private var packetPool = ConcurrentLinkedDeque<Packet<out ServerPacketListener>>()
3536
private var lastVelocity: EntityVelocityUpdateS2CPacket? = null
3637
private var lastUpdate = 0L
3738

@@ -64,8 +65,6 @@ object Blink : Module(
6465

6566
listener<PacketEvent.Receive.Pre> { event ->
6667
if (!isActive || !shiftVelocity) return@listener
67-
if (!connection.connection.isOpen) return@listener
68-
if (connection.connection.packetListener?.accepts(event.packet) == false) return@listener
6968

7069
if (event.packet !is EntityVelocityUpdateS2CPacket) return@listener
7170
if (event.packet.id != player.id) return@listener
@@ -84,7 +83,6 @@ object Blink : Module(
8483
while (packetPool.isNotEmpty()) {
8584
packetPool.poll().let { packet ->
8685
connection.sendPacketSilently(packet)
87-
connection.connection.packetsSentCounter++
8886

8987
if (packet is PlayerMoveC2SPacket && packet.changesPosition()) {
9088
lastBox = player.boundingBox
@@ -95,8 +93,7 @@ object Blink : Module(
9593
}
9694

9795
lastVelocity?.let { velocity ->
98-
ClientConnection.handlePacket(velocity, connection.connection.packetListener)
99-
connection.connection.packetsReceivedCounter++
96+
connection.handlePacketSilently(velocity)
10097
lastVelocity = null
10198
}
10299
}
Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
package com.lambda.util
22

33
import net.minecraft.client.network.ClientPlayNetworkHandler
4+
import net.minecraft.network.ClientConnection
5+
import net.minecraft.network.listener.ClientPacketListener
6+
import net.minecraft.network.listener.ServerPacketListener
47
import net.minecraft.network.packet.Packet
58

69
object PacketUtils {
7-
fun ClientPlayNetworkHandler.sendPacketSilently(packet: Packet<*>) {
10+
/**
11+
* Sends a packet to the server without notifying the client.
12+
* It bypasses the mixins that would normally intercept the packet
13+
* and send it through the client's event bus.
14+
*
15+
* @param packet The packet to send.
16+
*/
17+
fun ClientPlayNetworkHandler.sendPacketSilently(packet: Packet<out ServerPacketListener>) {
18+
if (!connection.isOpen) return
819
connection.send(packet, null, true)
20+
connection.packetsSentCounter++
921
}
10-
}
22+
23+
/**
24+
* Handles a packet without notifying the client.
25+
* It bypasses the mixins that would normally intercept the packet
26+
* and send it through the client's event bus.
27+
*
28+
* @param packet The packet to handle.
29+
*/
30+
fun ClientPlayNetworkHandler.handlePacketSilently(packet: Packet<out ClientPacketListener>) {
31+
if (!connection.isOpen) return
32+
if (connection.packetListener?.accepts(packet) == false) return
33+
34+
ClientConnection.handlePacket(packet, connection.packetListener)
35+
connection.packetsReceivedCounter++
36+
}
37+
}

0 commit comments

Comments
 (0)