mirror of
https://github.com/PlayPro/CoreProtect.git
synced 2026-10-28 09:44:56 +00:00
Added support for item_name (#963)
This commit is contained in:
1 parent
37a67f2594
commit
867fb3d0f3
1 file changed
+24
-1
@@ -2,6 +2,7 @@ package net.coreprotect.utility;
|
||||
|
||||
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 class ItemUtils {
|
||||
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<String> enchantments = ItemMetaHandler.getEnchantments(item, displayName);
|
||||
@@ -713,6 +715,27 @@ public class ItemUtils {
|
||||
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<Integer, Object> serializeItemStackLegacy(ItemStack itemStack, String faceData, int slot) {
|
||||
Map<Integer, Object> result = new HashMap<>();
|
||||
Map<String, Object> itemMap = serializeItemStack(itemStack, faceData, slot);
|
||||
|
||||
Reference in new issue
Block a user