14
14
import com .comphenix .protocol .utility .MinecraftReflection ;
15
15
import com .comphenix .protocol .wrappers .WrappedChatComponent ;
16
16
17
- import io .netty .channel .ChannelHandlerContext ;
18
-
19
17
/**
20
18
* This class facilitates the invocation of methods on the current packet listener.
21
- * It attempts to execute the <code>send </code>, <code>read</code>, and <code>disconnect</code>
22
- * methods and, upon failure (either due to the absence of the method or the packet
23
- * listener being of an incorrect type), it delegates the call to the network manager.
19
+ * It attempts to execute the <code>disconnect </code> method, upon failure
20
+ * (either due to the absence of the method or the packet listener being of an incorrect type),
21
+ * it delegates the call to the network manager.
24
22
*
25
23
* <p>Supported packet listener types include CONFIGURATION and PLAY. If the packet
26
24
* listener does not match these types, or if the required method is missing, the
@@ -38,36 +36,15 @@ public class PacketListenerInvoker {
38
36
private static final Class <?> COMMON_PACKET_LISTENER_CLASS = MinecraftReflection .getNullableNMS ("server.network.ServerCommonPacketListenerImpl" );
39
37
private static final Class <?> PREFERRED_PACKET_LISTENER_CLASS = COMMON_PACKET_LISTENER_CLASS != null ? COMMON_PACKET_LISTENER_CLASS : GAME_PACKET_LISTENER_CLASS ;
40
38
41
- private static final MethodAccessor PACKET_LISTENER_SEND = getPacketListenerSend ();
42
39
private static final MethodAccessor PACKET_LISTENER_DISCONNECT = getPacketListenerDisconnect ();
43
40
private static final boolean DOES_PACKET_LISTENER_DISCONNECT_USE_COMPONENT = doesPacketListenerDisconnectUseComponent ();
44
41
45
- private static final MethodAccessor NETWORK_MANAGER_SEND = getNetworkManagerSend ();
46
- private static final MethodAccessor NETWORK_MANAGER_READ = getNetworkManagerRead ();
47
42
private static final MethodAccessor NETWORK_MANAGER_DISCONNECT = getNetworkManagerDisconnect ();
48
43
private static final MethodAccessor NETWORK_MANAGER_PACKET_LISTENER = getNetworkManagerPacketListener ();
49
44
50
45
public static void ensureStaticInitializedWithoutError () {
51
46
}
52
47
53
- private static MethodAccessor getPacketListenerSend () {
54
- FuzzyReflection packetListener = FuzzyReflection .fromClass (PREFERRED_PACKET_LISTENER_CLASS );
55
-
56
- List <Method > send = packetListener .getMethodList (FuzzyMethodContract .newBuilder ()
57
- .banModifier (Modifier .STATIC )
58
- .returnTypeVoid ()
59
- .parameterCount (1 )
60
- .parameterExactType (MinecraftReflection .getPacketClass (), 0 )
61
- .build ());
62
-
63
- if (send .isEmpty ()) {
64
- ProtocolLogger .debug ("Can't get packet listener send method" );
65
- return null ;
66
- }
67
-
68
- return Accessors .getMethodAccessor (send .get (0 ));
69
- }
70
-
71
48
private static MethodAccessor getPacketListenerDisconnect () {
72
49
FuzzyReflection packetListener = FuzzyReflection .fromClass (PREFERRED_PACKET_LISTENER_CLASS );
73
50
@@ -104,28 +81,6 @@ private static boolean doesPacketListenerDisconnectUseComponent() {
104
81
return false ;
105
82
}
106
83
107
- private static MethodAccessor getNetworkManagerSend () {
108
- FuzzyReflection networkManager = FuzzyReflection .fromClass (MinecraftReflection .getNetworkManagerClass ());
109
-
110
- Method send = networkManager .getMethod (FuzzyMethodContract .newBuilder ()
111
- .banModifier (Modifier .STATIC )
112
- .returnTypeVoid ()
113
- .parameterCount (1 )
114
- .parameterExactType (MinecraftReflection .getPacketClass (), 0 )
115
- .build ());
116
-
117
- return Accessors .getMethodAccessor (send );
118
- }
119
-
120
- private static MethodAccessor getNetworkManagerRead () {
121
- FuzzyReflection networkManager = FuzzyReflection .fromClass (MinecraftReflection .getNetworkManagerClass (), true );
122
-
123
- Method read = networkManager
124
- .getMethodByParameters ("read" , ChannelHandlerContext .class , MinecraftReflection .getPacketClass ());
125
-
126
- return Accessors .getMethodAccessor (read );
127
- }
128
-
129
84
private static MethodAccessor getNetworkManagerDisconnect () {
130
85
FuzzyReflection networkManager = FuzzyReflection .fromClass (MinecraftReflection .getNetworkManagerClass ());
131
86
@@ -189,30 +144,6 @@ private Object getPacketListener() {
189
144
return this .packetListener .get ();
190
145
}
191
146
192
- /**
193
- * Sends a packet using the current packet listener if available and valid; otherwise,
194
- * falls back to the network manager.
195
- *
196
- * @param packet The packet to be sent.
197
- */
198
- public void send (Object packet ) {
199
- Object packetListener = this .getPacketListener ();
200
- if (PACKET_LISTENER_SEND != null && packetListener != null ) {
201
- PACKET_LISTENER_SEND .invoke (packetListener , packet );
202
- } else {
203
- NETWORK_MANAGER_SEND .invoke (this .networkManager , packet );
204
- }
205
- }
206
-
207
- /**
208
- * Reads a packet directly using the network manager.
209
- *
210
- * @param packet The packet to be read.
211
- */
212
- public void read (Object packet ) {
213
- NETWORK_MANAGER_READ .invoke (this .networkManager , null , packet );
214
- }
215
-
216
147
/**
217
148
* Disconnects the player using the current packet listener if available and valid; otherwise,
218
149
* falls back to the network manager.
0 commit comments