Fix custom item code getting generated for items with damage (Fixes #598)

This commit is contained in:
Phoenix616 2024-07-04 23:19:23 +01:00
parent ec8032a688
commit 6a2a3a4b36
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0

View File

@ -268,7 +268,7 @@ public class MaterialUtil {
}
String metaData = "";
if (itemStack.hasItemMeta()) {
if (hasCustomData(itemStack)) {
metaData = "#" + Metadata.getItemCode(itemStack);
}
@ -284,6 +284,29 @@ public class MaterialUtil {
return code + durability + metaData;
}
/**
* Check whether the provided ItemStack has custom data (in the past called "ItemMeta"). This will ignore
* the durability of an item.
*
* @param itemStack The ItemStack to check
* @return Whether the item has custom data
*/
private static boolean hasCustomData(ItemStack itemStack) {
if (!itemStack.hasItemMeta()) {
return false;
}
ItemMeta itemMeta = itemStack.getItemMeta();
if (itemMeta instanceof Damageable) {
if (!((Damageable) itemMeta).hasDamage()) {
return true;
}
}
Map<String, Object> data = itemMeta.serialize();
// if the data map contains more than the metadata type and the damage
// then the item does indeed have custom data set
return data.size() > 2;
}
/**
* Get an item name shortened to a max length that is still reversable by {@link #getMaterial(String)}
*