16
16
*/
17
17
package com .comphenix .protocol .injector .netty ;
18
18
19
- import io .netty .channel .Channel ;
20
- import io .netty .channel .ChannelFuture ;
21
- import io .netty .channel .ChannelHandler ;
22
- import io .netty .channel .ChannelHandlerContext ;
23
- import io .netty .channel .ChannelInboundHandler ;
24
- import io .netty .channel .ChannelInboundHandlerAdapter ;
25
- import io .netty .channel .ChannelInitializer ;
26
-
27
19
import java .io .InputStream ;
28
20
import java .lang .reflect .Field ;
29
21
import java .lang .reflect .InvocationTargetException ;
30
22
import java .lang .reflect .Method ;
23
+ import java .lang .reflect .ParameterizedType ;
31
24
import java .net .InetSocketAddress ;
32
25
import java .util .List ;
33
26
import java .util .Set ;
57
50
import com .comphenix .protocol .utility .MinecraftReflection ;
58
51
import com .google .common .collect .Lists ;
59
52
53
+ import io .netty .channel .Channel ;
54
+ import io .netty .channel .ChannelFuture ;
55
+ import io .netty .channel .ChannelHandler ;
56
+ import io .netty .channel .ChannelHandlerContext ;
57
+ import io .netty .channel .ChannelInboundHandler ;
58
+ import io .netty .channel .ChannelInboundHandlerAdapter ;
59
+ import io .netty .channel .ChannelInitializer ;
60
+
60
61
public class ProtocolInjector implements ChannelListener {
61
62
public static final ReportType REPORT_CANNOT_INJECT_INCOMING_CHANNEL = new ReportType ("Unable to inject incoming channel %s." );
62
63
@@ -139,11 +140,12 @@ public synchronized void inject() {
139
140
// Handle connected channels
140
141
final ChannelInboundHandler endInitProtocol = new ChannelInitializer <Channel >() {
141
142
@ Override
142
- protected void initChannel (Channel channel ) throws Exception {
143
+ protected void initChannel (final Channel channel ) throws Exception {
143
144
try {
144
- // This can take a while, so we need to stop the main thread from interfering
145
+ // TODO I don't like this
145
146
synchronized (networkManagers ) {
146
- injectionFactory .fromChannel (channel , ProtocolInjector .this , playerFactory ).inject ();
147
+ channel .eventLoop ().submit (() ->
148
+ injectionFactory .fromChannel (channel , ProtocolInjector .this , playerFactory ).inject ());
147
149
}
148
150
} catch (Exception e ) {
149
151
reporter .reportDetailed (ProtocolInjector .this , Report .newBuilder (REPORT_CANNOT_INJECT_INCOMING_CHANNEL ).messageParam (channel ).error (e ));
@@ -171,9 +173,26 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
171
173
ctx .fireChannelRead (msg );
172
174
}
173
175
};
176
+
177
+ FuzzyReflection fuzzy = FuzzyReflection .fromObject (serverConnection , true );
178
+
179
+ try {
180
+ List <Field > fields = fuzzy .getFieldListByType (List .class );
181
+ for (Field field : fields ) {
182
+ ParameterizedType param = (ParameterizedType ) field .getGenericType ();
183
+ if (param .getActualTypeArguments ()[0 ].equals (MinecraftReflection .getNetworkManagerClass ())) {
184
+ field .setAccessible (true );
185
+ networkManagers = (List <Object >) field .get (serverConnection );
186
+ }
187
+ }
188
+ } catch (Exception ex ) {
189
+ networkManagers = (List <Object >) fuzzy .getMethodByParameters ("getNetworkManagers" , List .class , serverConnection .getClass ())
190
+ .invoke (null , serverConnection );
191
+ }
174
192
175
- // Get the current NetworkMananger list
176
- networkManagers = (List <Object >) FuzzyReflection .fromObject (serverConnection , true ).invokeMethod (null , "getNetworkManagers" , List .class , serverConnection );
193
+ if (networkManagers == null ) {
194
+ throw new RuntimeException ("Failed to obtain list of network managers." );
195
+ }
177
196
178
197
// Insert ProtocolLib's connection interceptor
179
198
bootstrapFields = getBootstrapFields (serverConnection );
@@ -191,7 +210,6 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
191
210
}
192
211
193
212
injected = true ;
194
-
195
213
} catch (Exception e ) {
196
214
throw new RuntimeException ("Unable to inject channel futures." , e );
197
215
}
0 commit comments