Solved Get player ping 1.17

Discussion in 'Spigot Plugin Development' started by LIMPIX31, Jul 6, 2021.

  1. How get player ping in spigot 1.17
     
  2. Why are forums needed? Probably they are needed for people who could not find an answer to a question or solve a problem by searching on the Internet.
    Code (Text):
    Cannot resolve method 'getPing' in 'Player'
    HTML:
    <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot</artifactId>
                <version>1.17-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
     
  3. Please use the Spigot api not spigot.
    You probably trying to call the method on the entity player (nms).
    Call it on the bukkit Player please.
     
  4. Code (Java):
    import org.bukkit.entity.Player;
    HTML:
    <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot-api</artifactId>
                <version>1.17-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
    Code (Text):
    Cannot resolve method 'getPing' in 'Player'
     
  5. Ensure that you don't have a dependency fucking up you spigot api dependency.
    Vault for example had a problem that it had a transient dependency on Bukkit 1.8.

    Look if you dependency view shows something like that.
     
  6. I use NMS, so my pom.xml looks like this, but even if I leave 1 dependency like you said it won't change anything
    HTML:

    <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot</artifactId>
        <version>1.16.1-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.spigotmc.</groupId>
        <artifactId>spigot</artifactId>
        <version>1.16.2-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.spigotmc..</groupId>
        <artifactId>spigot</artifactId>
        <version>1.16.3-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.spigotmc...</groupId>
        <artifactId>spigot</artifactId>
        <version>1.16.4-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.spigotmc....</groupId>
        <artifactId>spigot</artifactId>
        <version>1.16.5-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.spigotmc.....</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.17-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
     
     
  7. Why the fucking hell are you depending on multiple versions of Spigot?
    For Multi version use maven multi modules please.
     
    • Agree Agree x 2
  8. This should work on any server version
    Code (Text):
        public static int getPing(Player player) {
            try {
                Object entityPlayer = player.getClass().getMethod("getHandle").invoke(player);
                return (int) entityPlayer.getClass().getField("ping").get(entityPlayer);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException e) {
                e.printStackTrace();
                return -1;
            }
        }
     
  9. What is this? The Player class doesn't have a getHandle method, it only has CraftPlayer, but it needs an NMS.
     
  10. not ping changed field e
     
  11. Sorry, I'm from Russia, I don't understand what you mean
     
  12. 2 ways.

    First:
    Code (Java):
    // If I remember this is the right order, could be wrong
    ((CraftPlayer) player).getHandle().ping;
    Second (Reflected, what I use):
    Code (Java):
    //using this in a method will require a few exceptions to be checked for, I use try/catch.
    Object entityPlayer = target.getClass().getMethod("getHandle").invoke(target);
    int ping = (int) entityPlayer.getClass().getField("ping").get(entityPlayer);
     
  13. There's also Player.getPing()
     
  14. Please read the posts above
     
  15. Well it works for me, also you shouldnt import all the spigot versions
    upload_2021-7-6_12-9-31.png
    upload_2021-7-6_12-9-51.png
    upload_2021-7-6_12-9-58.png
    upload_2021-7-6_12-10-12.png
     
    #16 Maxx_Qc, Jul 6, 2021
    Last edited: Jul 6, 2021
  16. What?
    Did you even try what i sent you? It works.
     
  17. Working
    HTML:
    <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot</artifactId>
                <version>1.17-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
    doesn't working
    HTML:
    <dependency>
                <groupId>org.spigotmc</groupId>
                <artifactId>spigot</artifactId>
                <version>1.16.1-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.spigotmc.</groupId>
                <artifactId>spigot</artifactId>
                <version>1.16.2-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.spigotmc..</groupId>
                <artifactId>spigot</artifactId>
                <version>1.16.3-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.spigotmc...</groupId>
                <artifactId>spigot</artifactId>
                <version>1.16.4-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.spigotmc....</groupId>
                <artifactId>spigot</artifactId>
                <version>1.16.5-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>org.spigotmc.....</groupId>
                <artifactId>spigot</artifactId>
                <version>1.17-R0.1-SNAPSHOT</version>
                <scope>provided</scope>
            </dependency>
     
  18. Not ping field. 1.17 field is e
     
  19. Looks like I have to depend on several versions of spigot and use CraftBukkit 1_17_R1

    CLOSED