Skip to content

Instantly share code, notes, and snippets.

@PaulBGD
Created May 11, 2015 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PaulBGD/41d65ba46e7e995dec1b to your computer and use it in GitHub Desktop.
Save PaulBGD/41d65ba46e7e995dec1b to your computer and use it in GitHub Desktop.
Find CraftPlayer ping
private static Class<?> craftPlayerClass;
private static Method getHandleMethod;
private static Field pingField;
private int getPing(Player player) {
if (player.getClass().getSimpleName().equals("CraftPlayer")) {
if (craftPlayerClass == null) {
craftPlayerClass = player.getClass();
}
if (getHandleMethod == null) {
try {
getHandleMethod = craftPlayerClass.getDeclaredMethod("getHandle");
if (!getHandleMethod.isAccessible()) {
getHandleMethod.setAccessible(true);
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
if (pingField == null) {
try {
pingField = getHandleMethod.getReturnType().getDeclaredField("ping");
if (!pingField.isAccessible()) {
pingField.setAccessible(true);
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
try {
Object nmsPlayer = getHandleMethod.invoke(player);
return pingField.getInt(nmsPlayer);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
// this isn't craftbukkit/spigot, so we know nothing
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment