How to catch NoClassDefFoundError for NMS packages?

Discussion in 'Spigot Plugin Development' started by IconByte, Jun 24, 2015.

  1. Hello. Is there any way how to catch NoClassDefFoundError for the bukkit version check or I just have to make all of these classes seperated?

    I have tried this, but it doesn't work:
    Code (Java):
            if (VersionHandler.is1_8_1()) {
                try {
                    net.minecraft.server.v1_8_R1.IChatBaseComponent icbc = net.minecraft.server.v1_8_R1.ChatSerializer.a("{\"text\": \"" + text + "\"}");
                    net.minecraft.server.v1_8_R1.PacketPlayOutChat bar = new net.minecraft.server.v1_8_R1.PacketPlayOutChat(icbc, (byte) 2);
                    ((org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer) player).getHandle().playerConnection.sendPacket(bar);
                } catch (NoClassDefFoundError ex) {}
            } else if (VersionHandler.is1_8_3()) {
                try {
                    net.minecraft.server.v1_8_R2.IChatBaseComponent icbc = net.minecraft.server.v1_8_R2.IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + text + "\"}");
                    net.minecraft.server.v1_8_R2.PacketPlayOutChat bar = new net.minecraft.server.v1_8_R2.PacketPlayOutChat(icbc, (byte) 2);
                    ((org.bukkit.craftbukkit.v1_8_R2.entity.CraftPlayer) player).getHandle().playerConnection.sendPacket(bar);
                } catch (NoClassDefFoundError ex) {}
            } else if (VersionHandler.is1_8_7()) {
                try {
                    net.minecraft.server.v1_8_R3.IChatBaseComponent icbc = net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + text + "\"}");
                    net.minecraft.server.v1_8_R3.PacketPlayOutChat bar = new net.minecraft.server.v1_8_R3.PacketPlayOutChat(icbc, (byte) 2);
                    ((org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer) player).getHandle().playerConnection.sendPacket(bar);
                } catch (NoClassDefFoundError ex) {}
            }
     
     
  2. Simple answer, you don't. It's bad practice to catch errors, as they aren't meant to be catched.
    The only thing you can do is to avoid them.
    If you're looking for cross-version NMS, here's a great wiki article:
    https://www.spigotmc.org/wiki/nms-on-different-versions-without-reflection/