Skip to content

Commit 3139e82

Browse files
authored
Fixed TinyProtocol for 1.19+ (#2499)
1 parent 3385fe6 commit 3139e82

1 file changed

Lines changed: 55 additions & 3 deletions

File tree

TinyProtocol/src/main/java/com/comphenix/tinyprotocol/TinyProtocol.java

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public abstract class TinyProtocol {
6464

6565
// Packets we have to intercept
6666
private static final Class<?> PACKET_LOGIN_IN_START = Reflection.getClass("{nms}.PacketLoginInStart", "net.minecraft.network.protocol.login.PacketLoginInStart");
67-
private static final FieldAccessor<GameProfile> getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
67+
private static final FieldAccessor<String> getPlayerName = new PlayerNameAccessor();
6868

6969
// Speedup channel lookup
7070
private Map<String, Channel> channelLookup = new MapMaker().weakValues().makeMap();
@@ -492,6 +492,59 @@ public final void close() {
492492
}
493493
}
494494

495+
/**
496+
* Get the player name from the login start packet.
497+
* This fixes the issue from 1.19 where the GameProfile field has been removed from this login packet.
498+
*
499+
* @author gamerover98
500+
*/
501+
private static class PlayerNameAccessor implements FieldAccessor<String> {
502+
503+
private FieldAccessor<String> getPlayerName;
504+
private FieldAccessor<GameProfile> getGameProfile;
505+
506+
PlayerNameAccessor() {
507+
try {
508+
this.getGameProfile = Reflection.getField(PACKET_LOGIN_IN_START, GameProfile.class, 0);
509+
} catch (IllegalArgumentException illegalArgumentException) {
510+
// nothing to do.
511+
}
512+
513+
try {
514+
//HOT-FIX for 1.19+
515+
this.getPlayerName = Reflection.getField(PACKET_LOGIN_IN_START, String.class, 0);
516+
} catch (IllegalArgumentException illegalArgumentException) {
517+
// nothing to do.
518+
}
519+
520+
if (getGameProfile == null && getPlayerName == null) {
521+
throw new UnsupportedOperationException("The current server version is not supported by TinyProtocol");
522+
}
523+
}
524+
525+
@Override
526+
public String get(Object target) {
527+
if (getPlayerName != null) {
528+
String playerName = getPlayerName.get(target);
529+
return playerName.substring(0, Math.min(16, playerName.length()));
530+
}
531+
532+
return getGameProfile.get(target).getName();
533+
}
534+
535+
@Override
536+
public void set(Object target, Object value) {
537+
throw new UnsupportedOperationException("Not supported");
538+
}
539+
540+
@Override
541+
public boolean hasField(Object target) {
542+
return getPlayerName != null
543+
? getPlayerName.hasField(target)
544+
: getGameProfile.hasField(target);
545+
}
546+
}
547+
495548
/**
496549
* Channel handler that is inserted into the player's channel pipeline, allowing us to intercept sent and received packets.
497550
*
@@ -533,8 +586,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
533586

534587
private void handleLoginStart(Channel channel, Object packet) {
535588
if (PACKET_LOGIN_IN_START.isInstance(packet)) {
536-
GameProfile profile = getGameProfile.get(packet);
537-
channelLookup.put(profile.getName(), channel);
589+
channelLookup.put(getPlayerName.get(packet), channel);
538590
}
539591
}
540592
}

0 commit comments

Comments
 (0)