Skip to content

Commit 74833f8

Browse files
authored
Correctly resolve protocol version of player (#1473)
1 parent e77f8ce commit 74833f8

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/java/com/comphenix/protocol/injector/netty/ChannelInjector.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.comphenix.protocol.reflect.VolatileField;
3131
import com.comphenix.protocol.reflect.accessors.Accessors;
3232
import com.comphenix.protocol.reflect.accessors.FieldAccessor;
33+
import com.comphenix.protocol.reflect.fuzzy.FuzzyFieldContract;
3334
import com.comphenix.protocol.utility.ByteBuddyFactory;
3435
import com.comphenix.protocol.utility.ByteBuddyGenerated;
3536
import com.comphenix.protocol.utility.MinecraftFields;
@@ -56,6 +57,7 @@
5657
import java.lang.reflect.Field;
5758
import java.lang.reflect.InvocationTargetException;
5859
import java.lang.reflect.Method;
60+
import java.lang.reflect.Modifier;
5961
import java.net.Socket;
6062
import java.net.SocketAddress;
6163
import java.util.*;
@@ -84,6 +86,7 @@ public class ChannelInjector extends ByteToMessageDecoder implements Injector {
8486

8587
// Versioning
8688
private static Class<?> PACKET_SET_PROTOCOL = null;
89+
private static FieldAccessor PROTOCOL_VERSION_ACCESSOR = null;
8790

8891
private static AtomicInteger keyId = new AtomicInteger();
8992
private static AttributeKey<Integer> PROTOCOL_KEY;
@@ -662,15 +665,21 @@ private void handleLogin(Class<?> packetClass, Object packet) {
662665
if (PACKET_SET_PROTOCOL == null) {
663666
try {
664667
PACKET_SET_PROTOCOL = PacketType.Handshake.Client.SET_PROTOCOL.getPacketClass();
668+
669+
// resolve the protocol version field here - if we're unable to find it we don't need to worry about it
670+
Field protocolVersionField = FuzzyReflection.fromClass(PACKET_SET_PROTOCOL, true).getField(FuzzyFieldContract.newBuilder()
671+
.banModifier(Modifier.STATIC)
672+
.typeExact(int.class)
673+
.build());
674+
PROTOCOL_VERSION_ACCESSOR = Accessors.getFieldAccessor(protocolVersionField);
665675
} catch (Throwable ex) {
666676
PACKET_SET_PROTOCOL = getClass(); // If we can't find it don't worry about it
667677
}
668678
}
669679

670680
if (PACKET_SET_PROTOCOL.equals(packetClass)) {
671-
FuzzyReflection fuzzy = FuzzyReflection.fromObject(packet);
672681
try {
673-
int protocol = (int) fuzzy.invokeMethod(packet, "getProtocol", int.class);
682+
int protocol = (int) PROTOCOL_VERSION_ACCESSOR.get(packet);
674683
originalChannel.attr(PROTOCOL_KEY).set(protocol);
675684
} catch (Throwable ex) {
676685
// Oh well

0 commit comments

Comments
 (0)