Skip to content

Commit

Permalink
fix: Replace player key setter method lookup with Reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
MemencioPerez committed May 8, 2024
1 parent 46137b1 commit 4a34735
Showing 1 changed file with 7 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,14 @@
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.connection.PostLoginEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.crypto.IdentifiedKey;
import com.velocitypowered.proxy.connection.client.ConnectedPlayer;
import io.github._4drian3d.unsignedvelocity.UnSignedVelocity;
import io.github._4drian3d.unsignedvelocity.configuration.Configuration;
import io.github._4drian3d.unsignedvelocity.listener.EventListener;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field;

public class ConnectListener implements EventListener {
private static final MethodHandle KEY_SETTER;

static {
try {
final var lookup = MethodHandles.privateLookupIn(ConnectedPlayer.class, MethodHandles.lookup());
KEY_SETTER = lookup.findSetter(ConnectedPlayer.class, "playerKey", IdentifiedKey.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Inject
private EventManager eventManager;
@Inject
Expand All @@ -39,7 +27,12 @@ void onJoin(PostLoginEvent event) throws Throwable {
}
final Player player = event.getPlayer();
if (player.getIdentifiedKey() != null) {
KEY_SETTER.invoke(player, null);
ConnectedPlayer connectedPlayer = (ConnectedPlayer) player;
Class<?> connectedPlayerClass = connectedPlayer.getClass();
Field playerKeyField = connectedPlayerClass.getDeclaredField("playerKey");
playerKeyField.setAccessible(true);
playerKeyField.set(connectedPlayer, null);
playerKeyField.setAccessible(false);
}
}

Expand Down

0 comments on commit 4a34735

Please sign in to comment.