diff --git a/src/main/java/net/coreprotect/utility/ItemUtils.java b/src/main/java/net/coreprotect/utility/ItemUtils.java index f4546bb3c..874abbc04 100644 --- a/src/main/java/net/coreprotect/utility/ItemUtils.java +++ b/src/main/java/net/coreprotect/utility/ItemUtils.java @@ -2,6 +2,7 @@ import java.io.ByteArrayOutputStream; import java.lang.reflect.Array; +import java.lang.reflect.Method; import java.util.*; import java.util.logging.Logger; import java.util.regex.Matcher; @@ -691,7 +692,8 @@ public static String getEnchantments(byte[] metadata, int type, int amount) { var item = getItemStack(metadata, type, amount); if (item == null) return ""; - String displayName = item.hasItemMeta() && item.getItemMeta().hasDisplayName() ? item.getItemMeta().getDisplayName() : ""; + ItemMeta itemMeta = item.hasItemMeta() ? item.getItemMeta() : null; + String displayName = itemMeta == null ? "" : getItemDisplayName(itemMeta); StringBuilder message = new StringBuilder(Color.ITALIC + displayName + Color.GREY); List enchantments = ItemMetaHandler.getEnchantments(item, displayName); @@ -713,6 +715,27 @@ else if (!enchantments.isEmpty()) { return message.toString(); } + private static String getItemDisplayName(ItemMeta itemMeta) { + try { + if (itemMeta.hasDisplayName()) { + return itemMeta.getDisplayName(); + } + + // The item_name component only exists on 1.20.5+, so access it reflectively. + Method hasItemName = itemMeta.getClass().getMethod("hasItemName"); + if (Boolean.TRUE.equals(hasItemName.invoke(itemMeta))) { + Method getItemName = itemMeta.getClass().getMethod("getItemName"); + Object name = getItemName.invoke(itemMeta); + return name == null ? "" : (String) name; + } + } + catch (Exception e) { + // item_name is not supported on this server version + } + + return ""; + } + public static Map serializeItemStackLegacy(ItemStack itemStack, String faceData, int slot) { Map result = new HashMap<>(); Map itemMap = serializeItemStack(itemStack, faceData, slot);