From 1ea10788331734457b302ac9cf4b4ed8516ae672 Mon Sep 17 00:00:00 2001 From: games647 Date: Thu, 5 Jan 2017 20:38:20 +0100 Subject: [PATCH] Check the complete class hierachy for finding the ping field (Related #153) --- .../defaults/PlayerPingVariable.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/games647/scoreboardstats/variables/defaults/PlayerPingVariable.java b/src/main/java/com/github/games647/scoreboardstats/variables/defaults/PlayerPingVariable.java index c445fa90..22506bc2 100644 --- a/src/main/java/com/github/games647/scoreboardstats/variables/defaults/PlayerPingVariable.java +++ b/src/main/java/com/github/games647/scoreboardstats/variables/defaults/PlayerPingVariable.java @@ -62,20 +62,23 @@ private int getReflectionPing(Player player) { private static boolean isModdedServer() { //aggressive checking for modded servers - List checkVersions = Lists.newArrayList(Bukkit.getVersion(), Bukkit.getName() - , Bukkit.getServer().toString()); + List checkVersions = Lists.newArrayList(Bukkit.getVersion(), Bukkit.getName(), + Bukkit.getServer().toString()); return checkVersions.stream().anyMatch(version -> (version.contains("MCPC") || version.contains("Cauldron"))); } - private void setMCPCPing(Object entityPlayer) { + private void setMCPCPing(Object entityPlayer) throws IllegalAccessException { getPlugin().getLogger().info("Looking for ping field"); + Class entityPlayerClazz = findEntityPlayer(entityPlayer.getClass(), entityPlayer); + getPlugin().getLogger().info("Looking for player clazz " + entityPlayerClazz); + //this isn't a secure, because it detects the ping variable by the ordering //a remaping (deobfuscate the variables) would work, but it won't be forwardcompatible Class lastType = null; Field lastIntField = null; - for (Field field : entityPlayer.getClass().getDeclaredFields()) { + for (Field field : entityPlayerClazz.getDeclaredFields()) { if (field.getType() == Integer.TYPE && Modifier.isPublic(field.getModifiers()) && lastType == Boolean.TYPE) { @@ -94,6 +97,23 @@ private void setMCPCPing(Object entityPlayer) { lastType = field.getType(); } - getPlugin().getLogger().log(Level.INFO, "Ping field {0} for {1}", new Object[] {lastIntField, entityPlayer}); + getPlugin().getLogger().log(Level.INFO, "Ping field {0} for {1}", new Object[]{lastIntField, entityPlayer}); + } + + private Class findEntityPlayer(Class startClazz, Object instance) throws IllegalAccessException { + if (startClazz != null) { + for (Field declaredField : startClazz.getDeclaredFields()) { + if (declaredField.getType() == String.class) { + Object val = declaredField.get(instance); + if ("en_US".equals(val)) { + return startClazz; + } + } + } + + return findEntityPlayer(startClazz.getSuperclass(), instance); + } + + return null; } }