Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EffHealth - fix max damage on custom items #6646

Merged
merged 2 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/main/java/ch/njol/skript/bukkitutil/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
*/
public class ItemUtils {

public static final boolean HAS_MAX_DAMAGE = Skript.methodExists(Damageable.class, "getMaxDamage");

/**
* Gets damage/durability of an item, or 0 if it does not have damage.
* @param itemStack Item.
Expand All @@ -46,6 +48,18 @@ public static int getDamage(ItemStack itemStack) {
return 0; // Non damageable item
}

/** Gets the max damage/durability of an item
* <p>NOTE: Will account for custom damageable items in MC 1.20.5+</p>
* @param itemStack Item to grab durability from
* @return Max amount of damage this item can take
*/
public static int getMaxDamage(ItemStack itemStack) {
ItemMeta meta = itemStack.getItemMeta();
if (HAS_MAX_DAMAGE && meta instanceof Damageable && ((Damageable) meta).hasMaxDamage())
return ((Damageable) meta).getMaxDamage();
return itemStack.getType().getMaxDurability();
}

/**
* Sets damage/durability of an item if possible.
* @param itemStack Item to modify.
Expand All @@ -72,6 +86,18 @@ public static int getDamage(ItemType itemType) {
return 0; // Non damageable item
}

/** Gets the max damage/durability of an item
* <p>NOTE: Will account for custom damageable items in MC 1.20.5+</p>
* @param itemType Item to grab durability from
* @return Max amount of damage this item can take
*/
public static int getMaxDamage(ItemType itemType) {
ItemMeta meta = itemType.getItemMeta();
if (HAS_MAX_DAMAGE && meta instanceof Damageable && ((Damageable) meta).hasMaxDamage())
return ((Damageable) meta).getMaxDamage();
return itemType.getMaterial().getMaxDurability();
}

/**
* Sets damage/durability of an item if possible.
* @param itemType Item to modify.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ch/njol/skript/effects/EffHealth.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected void execute(Event event) {
if (this.amount == null) {
ItemUtils.setDamage(itemType, 0);
} else {
ItemUtils.setDamage(itemType, (int) Math2.fit(0, (ItemUtils.getDamage(itemType) + (isHealing ? -amount : amount)), itemType.getMaterial().getMaxDurability()));
ItemUtils.setDamage(itemType, (int) Math2.fit(0, (ItemUtils.getDamage(itemType) + (isHealing ? -amount : amount)), ItemUtils.getMaxDamage(itemType)));
}

} else if (obj instanceof Slot) {
Expand All @@ -103,7 +103,7 @@ protected void execute(Event event) {
if (this.amount == null) {
ItemUtils.setDamage(itemStack, 0);
} else {
int damageAmt = (int) Math2.fit(0, (ItemUtils.getDamage(itemStack) + (isHealing ? -amount : amount)), itemStack.getType().getMaxDurability());
int damageAmt = (int) Math2.fit(0, (ItemUtils.getDamage(itemStack) + (isHealing ? -amount : amount)), ItemUtils.getMaxDamage(itemStack));
ItemUtils.setDamage(itemStack, damageAmt);
}

Expand Down