Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

483
Visualizações
How to get player's ping in spigot 1.16.4

I tried to use java reflection in many ways to get the ping of a player. But at 100%, it returns 0ms.

I've searched for a long time, so... can someone help me?

Try 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);
       }

    }

Try2 :

    public static int getPing(Player p) {

       EntityPlayer player = ((CraftPlayer) p).getHandle();
       return player.ping;

    }
    

Try 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();
        }

Try 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();
        }

And 3 more... (Similar to this or definitively not good for usage)

I'm testing my plugin on a VPS-Hosted 1.16.4 PaperMC server.

Maybe it will be different on a dedicated machine.

I tried to use "compact" code like this :

int ping = ((CraftPlayer) p).getHandle().ping;

... but it returns 0ms again.

Please note that I'm a real Java novice. I'm practicing to improve my level.

I'm not searching the final solution, but just an answer if my aproach is OK. If not, a good way to do this.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Finally, it works.

The problem is my FAI : A few days ago, I opened 25565 ports for UDP/TCP and I also change other parameters.

Since I turn off all these options, my ping was magically back to 8-12 ms.

Thanks kahveci for your edit suggestion and sorry for the inconvenience.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda