Created
May 11, 2015 22:10
-
-
Save PaulBGD/41d65ba46e7e995dec1b to your computer and use it in GitHub Desktop.
Find CraftPlayer ping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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