-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
DenizenNetworkManager.java
170 lines (137 loc) · 5.52 KB
/
DenizenNetworkManager.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package net.aufdemrand.denizen.utilities.packets.intercept;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.minecraft.server.v1_8_R3.*;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Player;
import javax.crypto.SecretKey;
import java.lang.reflect.Field;
import java.net.SocketAddress;
public class DenizenNetworkManager extends NetworkManager {
private final NetworkManager oldManager;
private final DenizenPacketListener packetListener;
private final EntityPlayer entityPlayer;
public DenizenNetworkManager(EntityPlayer entityPlayer, NetworkManager oldManager) {
super(getProtocolDirection(oldManager));
this.oldManager = oldManager;
this.channel = oldManager.channel;
PacketListenerPlayIn oldListener = (PacketListenerPlayIn) oldManager.getPacketListener();
this.packetListener = new DenizenPacketListener(entityPlayer, oldListener);
a(this.packetListener); // in case something caught the old manager somehow
this.entityPlayer = this.packetListener.entityPlayer;
}
public static void setNetworkManager(Player player) {
EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
PlayerConnection playerConnection = entityPlayer.playerConnection;
setNetworkManager(playerConnection, new DenizenNetworkManager(entityPlayer, playerConnection.networkManager));
}
public void channelActive(ChannelHandlerContext channelhandlercontext) throws Exception {
oldManager.channelActive(channelhandlercontext);
}
public void a(EnumProtocol enumprotocol) {
oldManager.a(enumprotocol);
}
public void channelInactive(ChannelHandlerContext channelhandlercontext) throws Exception {
oldManager.channelInactive(channelhandlercontext);
}
public void exceptionCaught(ChannelHandlerContext channelhandlercontext, Throwable throwable) throws Exception {
oldManager.exceptionCaught(channelhandlercontext, throwable);
}
protected void a(ChannelHandlerContext channelhandlercontext, Packet packet) throws Exception {
if(oldManager.channel.isOpen()) {
try {
packet.a(this.packetListener);
} catch (Exception e) {
dB.echoError(e);
}
}
}
public void a(PacketListener packetlistener) {
oldManager.a(packetlistener);
}
public void handle(Packet packet) {
// If the packet sending isn't cancelled, allow normal sending
if (!PacketOutHandler.handle(entityPlayer, packet)) {
oldManager.handle(packet);
}
}
public void a(Packet packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener, GenericFutureListener<? extends Future<? super Void>>... agenericfuturelistener) {
oldManager.a(packet, genericfuturelistener, agenericfuturelistener);
}
public void a() {
oldManager.a();
}
public SocketAddress getSocketAddress() {
return oldManager.getSocketAddress();
}
public void close(IChatBaseComponent ichatbasecomponent) {
oldManager.close(ichatbasecomponent);
}
public boolean c() {
return oldManager.c();
}
public void a(SecretKey secretkey) {
oldManager.a(secretkey);
}
public boolean g() {
return oldManager.g();
}
public boolean h() {
return oldManager.h();
}
public PacketListener getPacketListener() {
return oldManager.getPacketListener();
}
public IChatBaseComponent j() {
return oldManager.j();
}
public void k() {
oldManager.k();
}
public void a(int i) {
oldManager.a(i);
}
public void l() {
oldManager.l();
}
protected void channelRead0(ChannelHandlerContext channelhandlercontext, Packet object) throws Exception {
this.a(channelhandlercontext, object);
}
//////////////////////////////////
//// Reflection Methods/Fields
///////////
private static final Field protocolDirectionField;
private static final Field networkManagerField;
static {
Field directionField = null;
Field managerField = null;
try {
directionField = NetworkManager.class.getDeclaredField("h");
directionField.setAccessible(true);
managerField = PlayerConnection.class.getDeclaredField("networkManager");
managerField.setAccessible(true);
} catch (Exception e) {
dB.echoError(e);
}
protocolDirectionField = directionField;
networkManagerField = managerField;
}
private static EnumProtocolDirection getProtocolDirection(NetworkManager networkManager) {
EnumProtocolDirection direction = null;
try {
direction = (EnumProtocolDirection) protocolDirectionField.get(networkManager);
} catch (Exception e) {
dB.echoError(e);
}
return direction;
}
private static void setNetworkManager(PlayerConnection playerConnection, NetworkManager networkManager) {
try {
networkManagerField.set(playerConnection, networkManager);
} catch (Exception e) {
dB.echoError(e);
}
}
}