Traté de usar el reflejo de Java de muchas maneras para obtener el ping de un jugador. Pero al 100%, devuelve 0ms.
He buscado durante mucho tiempo, así que... ¿alguien puede ayudarme?
Prueba 1:
public static int getPing(Player p) { try { Object craftPlayer = (CraftPlayer) p; return (int) craftPlayer.getClass().getField("ping").get(craftPlayer); } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) { throw new Error(e); } }Prueba 2:
public static int getPing(Player p) { EntityPlayer player = ((CraftPlayer) p).getHandle(); return player.ping; }Prueba 3:
int ping = 0; Class<?>[] parameterTypes = null; Class<CraftPlayer> metadata = CraftPlayer.class; Method[] methods = metadata.getDeclaredMethods(); for(Method method : methods) { if(method.getName().equalsIgnoreCase("getHandle")) { parameterTypes = method.getParameterTypes(); } } try { Object entityPlayer = p.getClass().getDeclaredMethod("getHandle", parameterTypes).invoke((CraftPlayer) p); ping = (int) entityPlayer.getClass().getDeclaredField("ping").get(entityPlayer); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException e) { e.printStackTrace(); }Prueba 4:
int ping = 0; try { Object entityPlayer = p.getClass().getMethod("getHandle").invoke(p); ping = entityPlayer.getClass().getField("ping").getInt(entityPlayer); } catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException e) { e.printStackTrace(); }Y 3 más... (Similar a este o definitivamente no es bueno para su uso)
Estoy probando mi complemento en un servidor PaperMC 1.16.4 alojado en VPS.
Tal vez sea diferente en una máquina dedicada.
Traté de usar un código "compacto" como este:
int ping = ((CraftPlayer) p).getHandle().ping;... pero devuelve 0ms de nuevo.
Tenga en cuenta que soy un verdadero novato de Java. Estoy practicando para mejorar mi nivel.
No estoy buscando la solución final, sino solo una respuesta si mi enfoque está bien. Si no, una buena manera de hacer esto.
Finalmente, funciona.
El problema es mi FAI: Hace unos días abrí 25565 puertos para UDP/TCP y también cambio otros parámetros.
Como desactivé todas estas opciones, mi ping volvió mágicamente a 8-12 ms.
Gracias kahveci por su sugerencia de edición y disculpe las molestias.