-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathClientConnectionMixin.java
85 lines (73 loc) · 2.89 KB
/
ClientConnectionMixin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* Copyright (c) 2014-2025 Wurst-Imperium and contributors.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.mixin;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.jetbrains.annotations.Nullable;
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.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import net.minecraft.network.ClientConnection;
import net.minecraft.network.PacketCallbacks;
import net.minecraft.network.packet.Packet;
import net.wurstclient.event.EventManager;
import net.wurstclient.events.ConnectionPacketOutputListener.ConnectionPacketOutputEvent;
import net.wurstclient.events.PacketInputListener.PacketInputEvent;
@Mixin(ClientConnection.class)
public abstract class ClientConnectionMixin
extends SimpleChannelInboundHandler<Packet<?>>
{
private ConcurrentLinkedQueue<ConnectionPacketOutputEvent> events =
new ConcurrentLinkedQueue<>();
@Inject(at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/ClientConnection;handlePacket(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/listener/PacketListener;)V",
ordinal = 0),
method = "channelRead0(Lio/netty/channel/ChannelHandlerContext;Lnet/minecraft/network/packet/Packet;)V",
cancellable = true)
private void onChannelRead0(ChannelHandlerContext context, Packet<?> packet,
CallbackInfo ci)
{
PacketInputEvent event = new PacketInputEvent(packet);
EventManager.fire(event);
if(event.isCancelled())
ci.cancel();
}
@ModifyVariable(at = @At("HEAD"),
method = "send(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/PacketCallbacks;)V")
public Packet<?> modifyPacket(Packet<?> packet)
{
ConnectionPacketOutputEvent event =
new ConnectionPacketOutputEvent(packet);
events.add(event);
EventManager.fire(event);
return event.getPacket();
}
@Inject(at = @At("HEAD"),
method = "send(Lnet/minecraft/network/packet/Packet;Lnet/minecraft/network/PacketCallbacks;)V",
cancellable = true)
private void onSend(Packet<?> packet, @Nullable PacketCallbacks callback,
CallbackInfo ci)
{
ConnectionPacketOutputEvent event = getEvent(packet);
if(event == null)
return;
if(event.isCancelled())
ci.cancel();
events.remove(event);
}
private ConnectionPacketOutputEvent getEvent(Packet<?> packet)
{
for(ConnectionPacketOutputEvent event : events)
if(event.getPacket() == packet)
return event;
return null;
}
}