How it works

VG Scoreboard Ping

How it works

Normally you would have to import CraftBukkit, but by using the method below it's possible to avoid that.
By detecting the server version and building the org.bukkit.craftbukkit.??.CraftServer class name the plugin can determine the CraftEntity and CraftPlayer for most versions of bukkit.
With the CraftPlayer.getHandle() it's possible to determine the net.minecraft.server class representing the CraftPlayer, the very class that stores the players ping! Using this method also allows compatibility with builds like MCPC.
As a last verification we try to find the field containing the ping, for Bukkit servers its "ping" for MCPC and alike its "field_71138_i" and if both fail try "lastPing". lastPing exists both on CraftBukkit and NMS, it might just exist on a diferent server.
Finally, we have the ping value in hands!

Stages:

  1. getServer().getClass().getName() -> Get the server's class name, normally something like org.bukkit.craftbukkit.(#_#_#.CraftServer|#_#_R#.CraftServer|CraftServer)
  2. try to find #_#_# or #_#_R# on the name and set it to org.bukkit.craftbukkit.(what ever was found) if nothing was found try org.bukkit.craftbukkit.(nothing here)
  3. try to get the CraftServer className with the newly found class path. If this fails the plugin is disabled
  4. With our CraftBukkit class path ready, try to get CraftEntity and make it accessible, make sure we have access by getting an OfflinePlayer as CraftEntity
  5. We can now cast Player to CraftPlayer and invoke getHandle() to get the NMS player class!
  6. With the NMS player class in hand we attempt to get the "ping" field from the class. If it's not found we try "field_71138_i" the vanilla field name and finally if nothing is found we try the value from "fallback-field" on the config, this is by default "lastPing" if no field is found the plugin is disabled.
  7. Finally if all classes are found, the field is found and readable we update the players objective score!

Comments

Posts Quoted:
Reply
Clear All Quotes