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(Ping.java:32)
    [18:16:46 WARN]:        at me.woarth.PingPlugin.Commands.Ping.onCommand(Ping.java:66)
    [18:16:46 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44)
    [18:16:46 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141)
    [18:16:46 WARN]:        at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:649)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1337)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1172)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13)
    [18:16:46 WARN]:        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    [18:16:46 WARN]:        at java.util.concurrent.FutureTask.run(Unknown 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(MinecraftServer.java:739)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:675)
    [18:16:46 WARN]:        at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:574)

    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) {
                ex.printStackTrace();
              }
              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