From bf5f81a915727c7c24474c4232c0fa7dda0c603e Mon Sep 17 00:00:00 2001 From: Jules Date: Wed, 19 Jun 2024 17:16:48 -0700 Subject: [PATCH] Fixed custom durability not showing on vanilla durability bar in 1.20.5+ --- .../api/interaction/util/DurabilityItem.java | 20 +++-- .../net/Indyuce/mmoitems/stat/ItemDamage.java | 83 ++++++++++--------- 2 files changed, 57 insertions(+), 46 deletions(-) diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/util/DurabilityItem.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/util/DurabilityItem.java index 35a9bc73..78aa3050 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/util/DurabilityItem.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/api/interaction/util/DurabilityItem.java @@ -22,6 +22,8 @@ import org.bukkit.GameMode; import org.bukkit.Sound; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.Damageable; +import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -221,6 +223,11 @@ public class DurabilityItem { if (durability == initialDurability) return nbtItem.getItem(); + nbtItem.addTag(new ItemTag("MMOITEMS_DURABILITY", durability)); + + // Apply the NBT tags + ItemStack item = nbtItem.toItem(); + /* * Cross multiplication to display the current item durability on the * item durability bar. (1 - ratio) because minecraft works with item @@ -231,16 +238,13 @@ public class DurabilityItem { * issues. Also makes sure the item can be mended using the vanilla * enchant. */ - if (!barHidden) { - int damage = (durability == maxDurability) ? 0 : Math.max(1, (int) ((1. - ((double) durability / maxDurability)) * nbtItem.getItem().getType().getMaxDurability())); - nbtItem.addTag(new ItemTag("Damage", damage)); + if (!barHidden && item.getType().getMaxDurability() > 0) { + final int damage = (durability == maxDurability) ? 0 : Math.max(1, (int) ((1. - ((double) durability / maxDurability)) * nbtItem.getItem().getType().getMaxDurability())); + final ItemMeta meta = item.getItemMeta(); + ((Damageable) meta).setDamage(damage); + item.setItemMeta(meta); } - nbtItem.addTag(new ItemTag("MMOITEMS_DURABILITY", durability)); - - // Apply the NBT tags - ItemStack item = nbtItem.toItem(); - // Item lore update final String format = MythicLib.inst().parseColors(ItemStats.ITEM_DAMAGE.getGeneralStatFormat().replace("{max}", String.valueOf(maxDurability))); final String old = format.replace("{current}", String.valueOf(initialDurability)); diff --git a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/ItemDamage.java b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/ItemDamage.java index b719f7a4..8d9255b3 100644 --- a/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/ItemDamage.java +++ b/MMOItems-API/src/main/java/net/Indyuce/mmoitems/stat/ItemDamage.java @@ -18,52 +18,59 @@ import org.jetbrains.annotations.Nullable; import java.util.ArrayList; /** - * Item damage is the item's vanilla durability, it matches - * the one saved for Damageable items in the "Damage" tag. - * - * Must not be mistaken for the item's current durability which is handled in + * Item damage is the item's vanilla damage. This is not the item's current + * durability, these two values are linked by the following formula: + * `max_durability = durability + damage` * * @author indyuce + * @implNote Due to 1.20.5+ modifying the location of many vanilla tags, + * all version-friendly implementations must use ItemMeta methods. */ public class ItemDamage extends DoubleStat implements GemStoneStat { - public ItemDamage() { - super("ITEM_DAMAGE", Material.FISHING_ROD, "Item Damage", - new String[]{"Default item damage. This does &cNOT", "impact the item's max durability."}, new String[]{"!block", "all"}); - } + public ItemDamage() { + super("ITEM_DAMAGE", Material.FISHING_ROD, "Item Damage", + new String[]{"Default item damage. This does &cNOT", "impact the item's max durability."}, new String[]{"!block", "all"}); + } - @Override - public void whenApplied(@NotNull ItemStackBuilder item, @NotNull DoubleData data) { - if (item.getMeta() instanceof Damageable) - ((Damageable) item.getMeta()).setDamage((int) data.getValue()); - } + @Override + public void whenApplied(@NotNull ItemStackBuilder item, @NotNull DoubleData data) { + if (item.getMeta() instanceof Damageable) + ((Damageable) item.getMeta()).setDamage((int) data.getValue()); + } - @Override - public void whenPreviewed(@NotNull ItemStackBuilder item, @NotNull DoubleData currentData, @NotNull NumericStatFormula templateData) throws IllegalArgumentException { whenApplied(item, currentData);} + @Override + public void whenPreviewed(@NotNull ItemStackBuilder item, @NotNull DoubleData currentData, @NotNull NumericStatFormula templateData) throws IllegalArgumentException { + whenApplied(item, currentData); + } - /** - * This stat is saved not as a custom tag, but as the vanilla HideFlag itself. - * Alas this is an empty array - */ - @NotNull - @Override - public ArrayList getAppliedNBT(@NotNull DoubleData data) { return new ArrayList<>(); } + /** + * This stat is saved not as a custom tag, but as the vanilla HideFlag itself. + * Alas this is an empty array + */ + @NotNull + @Override + public ArrayList getAppliedNBT(@NotNull DoubleData data) { + return new ArrayList<>(); + } - @Override - public void whenLoaded(@NotNull ReadMMOItem mmoitem) { - if (mmoitem.getNBT().getItem().getItemMeta() instanceof Damageable) - mmoitem.setData(ItemStats.ITEM_DAMAGE, new DoubleData(((Damageable) mmoitem.getNBT().getItem().getItemMeta()).getDamage())); - } + @Override + public void whenLoaded(@NotNull ReadMMOItem mmoitem) { + if (mmoitem.getNBT().getItem().getItemMeta() instanceof Damageable) + mmoitem.setData(ItemStats.ITEM_DAMAGE, new DoubleData(((Damageable) mmoitem.getNBT().getItem().getItemMeta()).getDamage())); + } - /** - * This stat is saved not as a custom tag, but as the vanilla HideFlag itself. - * Alas this method returns null. - */ - @Nullable - @Override - public DoubleData getLoadedNBT(@NotNull ArrayList storedTags) { return null; } + /** + * This stat is saved not as a custom tag, but as the vanilla HideFlag itself. + * Alas this method returns null. + */ + @Nullable + @Override + public DoubleData getLoadedNBT(@NotNull ArrayList storedTags) { + return null; + } - @Override - public String getLegacyTranslationPath() { - return "durability"; - } + @Override + public String getLegacyTranslationPath() { + return "durability"; + } }