Is there an easier way to do reflection? Code:java public static Class<?> getBukkitClass(String name) { String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]; try { return Class.forName("org.bukkit.craftbukkit." + version + "." + name); } catch (Exception e) { StackTraceCore.getStackTraceCore().printTrace(e); return null; } } public static Object getName(ItemStack itemStack) { Object craftItemStack = null; Class<?> bukkitClass = getBukkitClass("inventory.CraftItemStack"); try { craftItemStack = bukkitClass.getMethod("asNMSCopy", ItemStack.class).invoke(bukkitClass, itemStack); Class<?> craftitemStackClass = craftItemStack.getClass(); return craftitemStackClass.getDeclaredMethod("getName").invoke(craftItemStack); } catch (Exception e) { StackTraceCore.getStackTraceCore().printTrace(e); } return ""; }
@JanTuck Apart from the fact that you could create a few more util methods to remove the exception catching completely, not really.
@AlvinB Okay. I actually looked at this and thought... If minecraft can get a friendly name why not just ask minecraft for it? https://bukkit.org/threads/itemnames-get-the-friendly-printable-name-for-an-itemstack.284199/
@JanTuck Well, there are a couple possibilities: This method might not have existed when that thread was made. This method is NMS, so it might change in different versions leading to a lot of hassle If the ItemStack has a custom name, it'll return that instead.