Fixed custom durability not showing on vanilla durability bar in 1.20.5+

This commit is contained in:
Jules 2024-06-19 17:16:48 -07:00
parent 7c27b76d1c
commit bf5f81a915
2 changed files with 57 additions and 46 deletions

View File

@ -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));

View File

@ -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<ItemTag> 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<ItemTag> 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<ItemTag> 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<ItemTag> storedTags) {
return null;
}
@Override
public String getLegacyTranslationPath() {
return "durability";
}
@Override
public String getLegacyTranslationPath() {
return "durability";
}
}