Consider Unbreakable in DurabilityManager

- The DurabilityManager now handles unbreakable items by always returning a durability of 0 (fully repaired) for items tagged as unbreakable. This should fix the issue where armored elytras could break even when 'unbreakable' is enabled in the config.
This commit is contained in:
Pim van der Loos 2022-03-10 19:24:11 +01:00
parent e2496fa8b6
commit d6bd4feb68
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
2 changed files with 48 additions and 6 deletions

View File

@ -42,6 +42,9 @@ public class DurabilityManager
public int setCombinedDurability(ItemStack armoredElytraOut, ItemStack armoredElytraIn, ItemStack other,
ArmorTier currentTier, ArmorTier targetTier)
{
if (nbtEditor.isUnbreakable(armoredElytraIn))
return 0;
final int combinedDurability = getCombinedDurability(armoredElytraIn, other, currentTier, targetTier);
setDurability(armoredElytraOut, combinedDurability, targetTier);
return combinedDurability;
@ -60,15 +63,17 @@ public class DurabilityManager
public int getCombinedDurability(ItemStack armoredElytra, ItemStack other,
ArmorTier currentTier, ArmorTier targetTier)
{
if (nbtEditor.isUnbreakable(armoredElytra))
return 0;
final ArmorTier otherTier = nbtEditor.getArmorTier(other);
final int currentMaxDurability = getMaxDurability(currentTier);
final int targetMaxDurability = getMaxDurability(targetTier);
final int otherMaxDurability = otherTier != ArmorTier.NONE ?
getMaxDurability(otherTier) : other.getType().getMaxDurability();
//noinspection deprecation
final int otherDurability = other.getType().equals(Material.ELYTRA) ?
getRealDurability(other, null) : other.getDurability();
getRealDurability(other, null) : getItemDurability(other);
final int currentDurability = getRealDurability(armoredElytra, currentTier);
final int combinedDurability = targetMaxDurability -
@ -89,6 +94,9 @@ public class DurabilityManager
*/
public int removeDurability(ItemStack armoredElytra, int durabilityLoss, @Nullable ArmorTier providedTier)
{
if (nbtEditor.isUnbreakable(armoredElytra))
return 0;
final ArmorTier currentTier = providedTier == null ? nbtEditor.getArmorTier(armoredElytra) : providedTier;
final int currentDurability = getRealDurability(armoredElytra, currentTier);
final int newDurability = Util.between(currentDurability + durabilityLoss, 0, getMaxDurability(currentTier));
@ -109,6 +117,9 @@ public class DurabilityManager
*/
public int getFullRepairItemCount(ItemStack armoredElytra, @Nullable ArmorTier providedTier)
{
if (nbtEditor.isUnbreakable(armoredElytra))
return 0;
final ArmorTier currentTier = providedTier == null ? nbtEditor.getArmorTier(armoredElytra) : providedTier;
final int repairableDurability = getMaxDurability(currentTier) - getRealDurability(armoredElytra, currentTier);
return (int) Math.ceil((float) repairableDurability / getRepairAmount(currentTier));
@ -126,6 +137,9 @@ public class DurabilityManager
*/
public int getRepairedDurability(ItemStack armoredElytra, int repairCount, @Nullable ArmorTier providedTier)
{
if (nbtEditor.isUnbreakable(armoredElytra))
return 0;
final ArmorTier currentTier = providedTier == null ? nbtEditor.getArmorTier(armoredElytra) : providedTier;
final int restoredDurability = repairCount * getRepairAmount(currentTier);
final int currentDurability = getRealDurability(armoredElytra, currentTier);
@ -147,8 +161,10 @@ public class DurabilityManager
final ArmorTier currentTier = providedTier == null ? nbtEditor.getArmorTier(item) : providedTier;
if (currentTier == ArmorTier.NONE)
//noinspection deprecation
return item.getDurability();
return getItemDurability(item);
if (nbtEditor.isUnbreakable(item))
return 0;
final int realDurability = nbtEditor.getRealDurability(item, currentTier);
return realDurability == -1 ? upgradeArmoredElytraToDurability(item, currentTier) : realDurability;
@ -164,6 +180,9 @@ public class DurabilityManager
*/
public void setDurability(ItemStack item, int durability, @Nullable ArmorTier providedTier)
{
if (nbtEditor.isUnbreakable(item))
return;
final ArmorTier currentTier = providedTier == null ? nbtEditor.getArmorTier(item) : providedTier;
final int oldMaxDurability = getMaxDurability(currentTier);
final int rawDurability = getRemappedDurability(durability, oldMaxDurability, ELYTRA_MAX_DURABILITY);
@ -183,8 +202,7 @@ public class DurabilityManager
private int upgradeArmoredElytraToDurability(ItemStack armoredElytra, ArmorTier currentTier)
{
final int maxDurability = getMaxDurability(currentTier);
//noinspection deprecation
final int rawDurability = armoredElytra.getDurability();
final int rawDurability = getItemDurability(armoredElytra);
final int realDurability = maxDurability == ELYTRA_MAX_DURABILITY ?
rawDurability :
@ -266,4 +284,16 @@ public class DurabilityManager
repairAmounts[idx] = (int) Math.ceil((float) maxDurability / steps);
}
}
/**
* Gets the durability of an item. See {@link ItemStack#getDurability()}.
*
* @param item The item to analyze.
* @return The durability of the item.
*/
@SuppressWarnings("deprecation")
private int getItemDurability(ItemStack item)
{
return item.getDurability();
}
}

View File

@ -166,6 +166,18 @@ public class NBTEditor
return getArmorTier(item.getItemMeta());
}
/**
* Checks if an item is unbreakable.
*
* @param item The item to check. This may or may not be an armored elytra.
* @return True if the item exists and is unbreakable. Otherwise, false.
*/
public boolean isUnbreakable(@Nullable ItemStack item)
{
final @Nullable ItemMeta meta = item == null ? null : item.getItemMeta();
return meta != null && meta.isUnbreakable();
}
/**
* Gets the Color of an armored elytra.
* <p>