Solved Getting a player's ping

Discussion in 'Spigot Plugin Development' started by Willim, Sep 30, 2017.

  1. I already know on how to do this but my question towards it, how can I go about my plugin so /ping works on every version of Minecraft and not have an issue?
  2. Does this do anything?
    int ping = ((CraftPlayer)p).getHandle().ping; //untested
  3. That's what I thought:
    Code (Text):
    ((CraftPlayer) player).getHandle().ping
    Why would this be a problem in regards to "on every version"
    What's your code, and which version is your native Spigot, and how are you supporting all other versions of MC
  4. Trust me that is what I am using, my code:

    Code (Text):

        public void runCommand(Player player, String[] args, boolean perm) {
            if (!perm) {

            if (!(args.length == 1)) {
                int playerping = ((CraftPlayer) player).getHandle().ping;
                player.sendMessage(Main.color(mc.getMessages().getString("PingMSG").replace("%ping%", String.valueOf(playerping))));

            else if (args.length == 1)

                Player target = Bukkit.getPlayer(args[0]);
                if (target == null) {

                int targetPing = ((CraftPlayer) target).getHandle().ping;
                String targetName = target.getName();
                player.sendMessage(Main.color(mc.getMessages().getString("PingPlayerMSG").replace("%ping%", String.valueOf(targetPing)).replace("%player%", targetName)));
    I ran this plugin on a 1.8.8 server and ping worked fine but then I switched to 1.11.2 and boom no longer working just throwing errors
  5. Whats the error
  6. Choco


    CraftBukkit (as well as NMS) each have package bumps every major codebase change (i.e. 1_11_R1, 1_12_R1, etc.). You have to use reflection to retrieve the field

    ClassNotFoundException for the reason I stated above
  7. How can I go about using reflections to get a player's ping ?
  8. add all minecraft jars and instead of just CraftPlayer use org.bukkit.craftbukkit.<version here>.entity.CraftPlayer or something similar for each version. You will need to see what version the server is using which you find a method on the internet.
  9. Code (Text):
    try {
      Object entityPlayer = player.getClass().getMethod("getHandle").invoke(player);
      int ping = (int) entityPlayer.getClass().getField("ping").get(entityPlayer);
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | NoSuchFieldException e) {
    #9 mrfloris, Sep 30, 2017
    Last edited: Oct 1, 2017
    • Useful Useful x 7
    • Winner Winner x 2
    • Agree Agree x 1
  10. I am trying to use this on my 1.17 server, but I get the following exception?

    Code (Text):
    [14:12:42 WARN]: java.lang.NoSuchFieldException: ping
    [14:12:42 WARN]:        at java.base/java.lang.Class.getField(
    [14:12:42 WARN]:        at me.waterman1001.MapsCompletedSVG.listeners.PlayerLatencyListener.getPing(
    [14:12:42 WARN]:        at me.waterman1001.MapsCompletedSVG.listeners.PlayerLatencyListener.onPlayerMoveCloseToPressurePlate(
    [14:12:42 WARN]:        at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor6135.execute(Unknown Source)
    [14:12:42 WARN]:        at org.bukkit.plugin.EventExecutor.lambda$create$1(
    [14:12:42 WARN]:        at co.aikar.timings.TimedEventExecutor.execute(
    [14:12:42 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(
    [14:12:42 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(
    [14:12:42 WARN]:        at
    [14:12:42 WARN]:        at
    [14:12:42 WARN]:        at$PacketPlayInPosition.a(
    [14:12:42 WARN]:        at$ensureRunningOnSameThread$1(
    [14:12:42 WARN]:        at
    [14:12:42 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.executeTask(
    [14:12:42 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.executeTask(
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.executeTask(
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.executeTask(
    [14:12:42 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.executeNext(
    [14:12:42 WARN]:        at
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.executeNext(
    [14:12:42 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.awaitTasks(
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.sleepForTick(
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.x(
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.lambda$spin$0(
    [14:12:42 WARN]:        at java.base/
    Sorry for the reply to this very old post.
    Would anyone know how I could resolve this? :)
  11. upload_2021-7-8_8-19-46.png
    been quite a bit buddy... maybe it's time for a new thread?
  12. You're right.
    I just thought maybe this would be more usefull for other people as well, as they would have all information at the same place then.
    I'm sorry though, my bad.
  13. On Spigot 1.17.1

    Player player = event.getPlayer();