Skip to content

Commit

Permalink
ExprDurability - account for custom durability
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneBeee committed Jun 21, 2024
1 parent 2e0d1cf commit 3a5c8cb
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/main/java/ch/njol/skript/expressions/ExprDurability.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
*/
package ch.njol.skript.expressions;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Kleenean;
import org.bukkit.Material;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
Expand All @@ -37,6 +36,7 @@
import ch.njol.skript.expressions.base.SimplePropertyExpression;
import ch.njol.skript.util.slot.Slot;
import ch.njol.util.coll.CollectionUtils;
import org.jetbrains.annotations.Nullable;

@Name("Damage Value/Durability")
@Description("The damage value/durability of an item.")
Expand All @@ -48,6 +48,9 @@
@Since("1.2, 2.7 (durability reversed)")
public class ExprDurability extends SimplePropertyExpression<Object, Integer> {

// Minecraft 1.20.5+ item component "max_damage" (custom item durability)
private static final boolean HAS_MAX_DAMAGE = Skript.methodExists(Damageable.class, "hasMaxDamage");

private boolean durability;

static {
Expand All @@ -67,7 +70,7 @@ public Integer convert(Object object) {
if (itemType == null)
return null;
ItemMeta meta = itemType.getItemMeta();
return meta instanceof Damageable ? convertToDamage(itemType.getMaterial(), ((Damageable) meta).getDamage()) : 0;
return meta instanceof Damageable ? convertToDamage(itemType, ((Damageable) meta).getDamage()) : 0;
}

@Override
Expand Down Expand Up @@ -99,15 +102,14 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
continue;
Damageable damageable = (Damageable) meta;

Material material = itemType.getMaterial();
switch (mode) {
case ADD:
case REMOVE:
int current = convertToDamage(material, damageable.getDamage());
damageable.setDamage(convertToDamage(material, current + change));
int current = convertToDamage(itemType, damageable.getDamage());
damageable.setDamage(convertToDamage(itemType, current + change));
break;
case SET:
damageable.setDamage(convertToDamage(material, change));
damageable.setDamage(convertToDamage(itemType, change));
break;
case DELETE:
case RESET:
Expand All @@ -120,10 +122,17 @@ public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
}
}

private int convertToDamage(Material material, int value) {
private int convertToDamage(ItemType itemType, int value) {
if (!durability)
return value;
int maxDurability = material.getMaxDurability();
Damageable itemMeta = (Damageable) itemType.getItemMeta();

int maxDurability;
if (HAS_MAX_DAMAGE && itemMeta.hasMaxDamage())
maxDurability = itemMeta.getMaxDamage();
else
maxDurability = itemType.getMaterial().getMaxDurability();

if (maxDurability == 0)
return 0;
return maxDurability - value;
Expand Down

0 comments on commit 3a5c8cb

Please sign in to comment.