Slightly simplify DurabilityManager

- Added a simple DurabilityManager#isBroken methods. This allows hiding the getMaxDurability method and move any isBroken logic out of the classes where it was previously used.
This commit is contained in:
Pim van der Loos 2022-03-10 19:42:15 +01:00
parent d6bd4feb68
commit 294f4d1cf3
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8
3 changed files with 31 additions and 4 deletions

View File

@ -96,7 +96,7 @@ public class EventHandlers implements Listener
// Even when we don't subtract durability, we still want to update the durability, so just subtract 0. // Even when we don't subtract durability, we still want to update the durability, so just subtract 0.
final int durabilityLoss = removeDurability ? (int) Math.max(1, e.getDamage() / 4) : 0; final int durabilityLoss = removeDurability ? (int) Math.max(1, e.getDamage() / 4) : 0;
final int newDurability = durabilityManager.removeDurability(elytra, durabilityLoss, armorTier); final int newDurability = durabilityManager.removeDurability(elytra, durabilityLoss, armorTier);
if (newDurability >= durabilityManager.getMaxDurability(armorTier)) if (durabilityManager.isBroken(newDurability, armorTier))
Util.moveChestplateToInventory(p); Util.moveChestplateToInventory(p);
} }

View File

@ -44,7 +44,7 @@ public class FlyDurabilityHandler implements Listener
return; return;
final int newDurability = durabilityManager.removeDurability(e.getItem(), e.getDamage(), armorTier); final int newDurability = durabilityManager.removeDurability(e.getItem(), e.getDamage(), armorTier);
if (newDurability >= durabilityManager.getMaxDurability(armorTier)) if (durabilityManager.isBroken(newDurability, armorTier))
Util.moveChestplateToInventory(e.getPlayer()); Util.moveChestplateToInventory(e.getPlayer());
} }
} }

View File

@ -226,13 +226,40 @@ public class DurabilityManager
return ArmorTier.getMaxDurability(armorTier); return ArmorTier.getMaxDurability(armorTier);
} }
/**
* Checks if an armored elytra should be considered 'broken'.
*
* @param durability The current 'real' durability. See {@link #getRealDurability(ItemStack, ArmorTier)}.
* @param armorTier The armor tier for which to check.
* @return True if the provided durability should be considered 'broken' for the provided armor tier.
*/
public boolean isBroken(int durability, ArmorTier armorTier)
{
return durability >= getMaxDurability(armorTier);
}
/**
* Checks if an armored elytra should be considered 'broken'.
*
* @param armoredElytra The armored elytra to check.
* @param armorTier The armor tier for which to check.
* @return True if the provided armored elytra should be considered 'broken'.
*/
public boolean isBroken(ItemStack armoredElytra, @Nullable ArmorTier armorTier)
{
final int realDurability = getRealDurability(armoredElytra, armorTier);
if (realDurability == 0)
return false;
return isBroken(realDurability, armorTier == null ? nbtEditor.getArmorTier(armoredElytra) : armorTier);
}
/** /**
* Gets the maximum durability for a given armor tier. * Gets the maximum durability for a given armor tier.
* *
* @param armorTier The armor tier for which to get the maximum durability. * @param armorTier The armor tier for which to get the maximum durability.
* @return The maximum durability of the given armor tier. * @return The maximum durability of the given armor tier.
*/ */
public int getMaxDurability(ArmorTier armorTier) private int getMaxDurability(ArmorTier armorTier)
{ {
return maxDurabilities[armorTier.ordinal()]; return maxDurabilities[armorTier.ordinal()];
} }
@ -243,7 +270,7 @@ public class DurabilityManager
* @param armorTier The armor tier. * @param armorTier The armor tier.
* @return The amount of durability restored per repair step for the given armor tier. * @return The amount of durability restored per repair step for the given armor tier.
*/ */
public int getRepairAmount(ArmorTier armorTier) private int getRepairAmount(ArmorTier armorTier)
{ {
return repairAmounts[armorTier.ordinal()]; return repairAmounts[armorTier.ordinal()];
} }