Solved ClassNotFoundException - CraftPlayer

Discussion in 'Spigot Plugin Development' started by woarth, Apr 15, 2017.

  1. Hi people, i have been doing my best trying to know why I have this error but I cant make it work. I have checked spigot forums, but the solutions for others havent worked for me. I am making a plugin which uses a Ping system, and in a simple command of getting a players ping i get this error:

    Code (Text):
    [18:16:46 INFO]: woarth issued server command: /ping
    [18:16:46 WARN]: java.lang.ClassNotFoundException: org/bukkit/craftbukkit//entity/CraftPlayer
    [18:16:46 WARN]:        at java.lang.Class.forName0(Native Method)
    [18:16:46 WARN]:        at java.lang.Class.forName(Unknown Source)
    [18:16:46 WARN]:        at me.woarth.PingPlugin.Commands.Ping.getPlayerPing(
    [18:16:46 WARN]:        at me.woarth.PingPlugin.Commands.Ping.onCommand(
    [18:16:46 WARN]:        at org.bukkit.command.PluginCommand.execute(
    [18:16:46 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(
    [18:16:46 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PlayerConnection.a(
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$
    [18:16:46 WARN]:        at java.util.concurrent.Executors$ Source)
    [18:16:46 WARN]:        at Source)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.D(
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.DedicatedServer.D(
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.C(
    [18:16:46 WARN]:        at

    This is the code:
    Code (Text):
            @SuppressWarnings({ "unused", "rawtypes" })
            public static int getPlayerPing(Player player) {
              try {
                int ping = 0;
                Class craftPlayer = Class.forName("org.bukkit.craftbukkit." + getServerVersion() + ".entity.CraftPlayer");
                Object converted = craftPlayer.cast(player);
                Method handle = converted.getClass().getMethod("getHandle", new Class[0]);
                Object entityPlayer = handle.invoke(converted, new Object[0]);
                Field pingField = entityPlayer.getClass().getField("ping");
                return pingField.getInt(entityPlayer);
              catch (Exception ex) {
              return 0;
            public static String getServerVersion() {
              Pattern brand = Pattern.compile("(v|)[0-9][_.][0-9][_.][R0-9]*");
              String pkg = Bukkit.getServer().getClass().getPackage().getName();
              String version = pkg.substring(pkg.lastIndexOf('.') + 1);
              if (!brand.matcher(version).matches()) {
                version = "";
              return version;
    Code (Text):
                Class craftPlayer = Class.forName("org.bukkit.craftbukkit." + getServerVersion() + ".entity.CraftPlayer");

    Code (Text):
                                int ping = getPlayerPing(Bukkit.getPlayer(sender.getName()));

    The plugin is using spigot 1.11 version and in a 1.8 server this ping system works fine.

    Thank you a lot, really need help!
  2. Hey you are my god, thanks a lot for helping me, it works now! I wish it helps others! Thanks, thanks and thanks