Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

476
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!