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):

       @Override
        public void runCommand(Player player, String[] args, boolean perm) {
            if (!perm) {
                player.sendMessage(noPermission);
                return;
            }

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

            else if (args.length == 1)
            {

                Player target = Bukkit.getPlayer(args[0]);
                if (target == null) {
                    player.sendMessage(Main.color(playerNotFound));
                    return;
                }

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

    Moderator

    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) {
      e.printStackTrace();
    }
    Source: https://www.spigotmc.org/conversations/player-ping.254031/
     
    #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(Class.java:2086)
    [14:12:42 WARN]:        at me.waterman1001.MapsCompletedSVG.listeners.PlayerLatencyListener.getPing(PlayerLatencyListener.java:19)
    [14:12:42 WARN]:        at me.waterman1001.MapsCompletedSVG.listeners.PlayerLatencyListener.onPlayerMoveCloseToPressurePlate(PlayerLatencyListener.java:35)
    [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(EventExecutor.java:69)
    [14:12:42 WARN]:        at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80)
    [14:12:42 WARN]:        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70)
    [14:12:42 WARN]:        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:624)
    [14:12:42 WARN]:        at net.minecraft.server.network.PlayerConnection.a(PlayerConnection.java:1440)
    [14:12:42 WARN]:        at net.minecraft.network.protocol.game.PacketPlayInFlying.a(PacketPlayInFlying.java:114)
    [14:12:42 WARN]:        at net.minecraft.network.protocol.game.PacketPlayInFlying$PacketPlayInPosition.a(PacketPlayInFlying.java:42)
    [14:12:42 WARN]:        at net.minecraft.network.protocol.PlayerConnectionUtils.lambda$ensureRunningOnSameThread$1(PlayerConnectionUtils.java:36)
    [14:12:42 WARN]:        at net.minecraft.server.TickTask.run(TickTask.java:18)
    [14:12:42 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:149)
    [14:12:42 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandlerReentrant.executeTask(IAsyncTaskHandlerReentrant.java:23)
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.executeTask(MinecraftServer.java:1340)
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.executeTask(MinecraftServer.java:193)
    [14:12:42 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:122)
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.bg(MinecraftServer.java:1319)
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.executeNext(MinecraftServer.java:1312)
    [14:12:42 WARN]:        at net.minecraft.util.thread.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:132)
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.sleepForTick(MinecraftServer.java:1273)
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.x(MinecraftServer.java:1184)
    [14:12:42 WARN]:        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:320)
    [14:12:42 WARN]:        at java.base/java.lang.Thread.run(Thread.java:831)
    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();
    player.getPing();