@@ -79,23 +79,23 @@ public class ChannelInjector extends ByteToMessageDecoder implements Injector {
79
79
// Versioning
80
80
private static Class <?> PACKET_SET_PROTOCOL = null ;
81
81
82
- private static AtomicInteger keyId = new AtomicInteger ();
83
- private static AttributeKey <Integer > PROTOCOL_KEY ;
82
+ private static final AtomicInteger keyId = new AtomicInteger ();
83
+ private static final AttributeKey <Integer > PROTOCOL_KEY ;
84
84
85
85
static {
86
86
PROTOCOL_KEY = AttributeKey .valueOf ("PROTOCOL-" + keyId .getAndIncrement ());
87
87
}
88
88
89
89
// Saved accessors
90
- private static Method DECODE_BUFFER ;
91
- private static Method ENCODE_BUFFER ;
90
+ private Method decodeBuffer ;
91
+ private Method encodeBuffer ;
92
92
private static FieldAccessor ENCODER_TYPE_MATCHER ;
93
93
94
94
// For retrieving the protocol
95
95
private static FieldAccessor PROTOCOL_ACCESSOR ;
96
96
97
97
// The factory that created this injector
98
- private InjectionFactory factory ;
98
+ private final InjectionFactory factory ;
99
99
100
100
// The player, or temporary player
101
101
private Player player ;
@@ -108,10 +108,10 @@ public class ChannelInjector extends ByteToMessageDecoder implements Injector {
108
108
// The current network manager and channel
109
109
private final Object networkManager ;
110
110
private final Channel originalChannel ;
111
- private VolatileField channelField ;
111
+ private final VolatileField channelField ;
112
112
113
113
// Known network markers
114
- private Map <Object , NetworkMarker > packetMarker = new WeakHashMap <>();
114
+ private final Map <Object , NetworkMarker > packetMarker = new WeakHashMap <>();
115
115
116
116
/**
117
117
* Indicate that this packet has been processed by event listeners.
@@ -134,13 +134,13 @@ public class ChannelInjector extends ByteToMessageDecoder implements Injector {
134
134
private ByteToMessageDecoder vanillaDecoder ;
135
135
private MessageToByteEncoder <Object > vanillaEncoder ;
136
136
137
- private Deque <PacketEvent > finishQueue = new ArrayDeque <>();
137
+ private final Deque <PacketEvent > finishQueue = new ArrayDeque <>();
138
138
139
139
// The channel listener
140
- private ChannelListener channelListener ;
140
+ private final ChannelListener channelListener ;
141
141
142
142
// Processing network markers
143
- private NetworkProcessor processor ;
143
+ private final NetworkProcessor processor ;
144
144
145
145
// Closed
146
146
private boolean injected ;
@@ -178,6 +178,24 @@ public int getProtocolVersion() {
178
178
return value != null ? value : MinecraftProtocolVersion .getCurrentVersion ();
179
179
}
180
180
181
+ private void updateBufferMethods () {
182
+ try {
183
+ decodeBuffer = vanillaDecoder .getClass ().getDeclaredMethod ("decode" ,
184
+ ChannelHandlerContext .class , ByteBuf .class , List .class );
185
+ decodeBuffer .setAccessible (true );
186
+ } catch (NoSuchMethodException ex ) {
187
+ throw new IllegalArgumentException ("Unable to find decode method in " + vanillaDecoder .getClass ());
188
+ }
189
+
190
+ try {
191
+ encodeBuffer = vanillaEncoder .getClass ().getDeclaredMethod ("encode" ,
192
+ ChannelHandlerContext .class , Object .class , ByteBuf .class );
193
+ encodeBuffer .setAccessible (true );
194
+ } catch (NoSuchMethodException ex ) {
195
+ throw new IllegalArgumentException ("Unable to find encode method in " + vanillaEncoder .getClass ());
196
+ }
197
+ }
198
+
181
199
@ Override
182
200
@ SuppressWarnings ("unchecked" )
183
201
public boolean inject () {
@@ -212,25 +230,7 @@ public boolean inject() {
212
230
throw new IllegalArgumentException ("Unable to find vanilla encoder in " + originalChannel .pipeline ());
213
231
patchEncoder (vanillaEncoder );
214
232
215
- if (DECODE_BUFFER == null ) {
216
- try {
217
- DECODE_BUFFER = vanillaDecoder .getClass ().getDeclaredMethod ("decode" ,
218
- ChannelHandlerContext .class , ByteBuf .class , List .class );
219
- DECODE_BUFFER .setAccessible (true );
220
- } catch (NoSuchMethodException ex ) {
221
- throw new IllegalArgumentException ("Unable to find decode method in " + vanillaDecoder .getClass ());
222
- }
223
- }
224
-
225
- if (ENCODE_BUFFER == null ) {
226
- try {
227
- ENCODE_BUFFER = vanillaEncoder .getClass ().getDeclaredMethod ("encode" ,
228
- ChannelHandlerContext .class , Object .class , ByteBuf .class );
229
- ENCODE_BUFFER .setAccessible (true );
230
- } catch (NoSuchMethodException ex ) {
231
- throw new IllegalArgumentException ("Unable to find encode method in " + vanillaEncoder .getClass ());
232
- }
233
- }
233
+ updateBufferMethods ();
234
234
235
235
// Intercept sent packets
236
236
MessageToByteEncoder <Object > protocolEncoder = new MessageToByteEncoder <Object >() {
@@ -447,7 +447,7 @@ private void encode(ChannelHandlerContext ctx, Object packet, ByteBuf output) th
447
447
// Process output handler
448
448
if (packet != null && event != null && NetworkMarker .hasOutputHandlers (marker )) {
449
449
ByteBuf packetBuffer = ctx .alloc ().buffer ();
450
- ENCODE_BUFFER .invoke (vanillaEncoder , ctx , packet , packetBuffer );
450
+ encodeBuffer .invoke (vanillaEncoder , ctx , packet , packetBuffer );
451
451
452
452
// Let each handler prepare the actual output
453
453
byte [] data = processor .processOutput (event , marker , getBytes (packetBuffer ));
@@ -470,7 +470,7 @@ private void encode(ChannelHandlerContext ctx, Object packet, ByteBuf output) th
470
470
// Attempt to handle the packet nevertheless
471
471
if (packet != null ) {
472
472
try {
473
- ENCODE_BUFFER .invoke (vanillaEncoder , ctx , packet , output );
473
+ encodeBuffer .invoke (vanillaEncoder , ctx , packet , output );
474
474
} catch (InvocationTargetException ex ) {
475
475
if (ex .getCause () instanceof Exception ) {
476
476
//noinspection ThrowFromFinallyBlock
@@ -510,7 +510,12 @@ private void scheduleMainThread(final Object packetCopy) {
510
510
@ Override
511
511
protected void decode (ChannelHandlerContext ctx , ByteBuf byteBuffer , List <Object > packets ) throws Exception {
512
512
try {
513
- DECODE_BUFFER .invoke (vanillaDecoder , ctx , byteBuffer , packets );
513
+ try {
514
+ decodeBuffer .invoke (vanillaDecoder , ctx , byteBuffer , packets );
515
+ } catch (IllegalArgumentException ex ) {
516
+ updateBufferMethods ();
517
+ decodeBuffer .invoke (vanillaDecoder , ctx , byteBuffer , packets );
518
+ }
514
519
515
520
// Reset queue
516
521
finishQueue .clear ();
0 commit comments