From cf56ca35ebcc760f404bafe59d9b76ef7eeae5a3 Mon Sep 17 00:00:00 2001 From: GJ Date: Wed, 6 Nov 2013 08:45:20 -0500 Subject: [PATCH] Tweaked durability handling slightly. Now routes everything through one function to handle enchants and max durability. Use max durability from RepairableManager by default to account for mod tools. --- .../nossr50/skills/axes/AxesManager.java | 11 +--------- .../skills/excavation/ExcavationManager.java | 2 +- .../nossr50/skills/mining/MiningManager.java | 2 +- .../skills/woodcutting/Woodcutting.java | 13 ++---------- .../gmail/nossr50/util/skills/SkillUtils.java | 21 ++++++++++--------- 5 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java b/src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java index 30be5de61..da8e588d5 100644 --- a/src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java +++ b/src/main/java/com/gmail/nossr50/skills/axes/AxesManager.java @@ -1,12 +1,9 @@ package com.gmail.nossr50.skills.axes; -import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.SkillType; @@ -98,13 +95,7 @@ public class AxesManager extends SkillManager { for (ItemStack armor : target.getEquipment().getArmorContents()) { if (ItemUtils.isArmor(armor) && Axes.impactChance > Misc.getRandom().nextInt(getActivationChance())) { - Material armorType = armor.getType(); - double durabilityModifier = 1 / (armor.getEnchantmentLevel(Enchantment.DURABILITY) + 1); // Modifier to simulate the durability enchantment behavior - double modifiedDurabilityDamage = durabilityDamage * durabilityModifier; - short maxDurability = mcMMO.getRepairableManager().isRepairable(armorType) ? mcMMO.getRepairableManager().getRepairable(armorType).getMaximumDurability() : armorType.getMaxDurability(); - double maxDurabilityDamage = maxDurability * Axes.impactMaxDurabilityModifier; - - SkillUtils.handleDurabilityChange(armor, (int) Math.min(modifiedDurabilityDamage, maxDurabilityDamage)); + SkillUtils.handleDurabilityChange(armor, durabilityDamage, Axes.impactMaxDurabilityModifier); } } } diff --git a/src/main/java/com/gmail/nossr50/skills/excavation/ExcavationManager.java b/src/main/java/com/gmail/nossr50/skills/excavation/ExcavationManager.java index fe7aba7b7..987e4a309 100644 --- a/src/main/java/com/gmail/nossr50/skills/excavation/ExcavationManager.java +++ b/src/main/java/com/gmail/nossr50/skills/excavation/ExcavationManager.java @@ -55,6 +55,6 @@ public class ExcavationManager extends SkillManager { excavationBlockCheck(blockState); excavationBlockCheck(blockState); - SkillUtils.handleDurabilityChange(getPlayer().getItemInHand(), Config.getInstance().getAbilityToolDamage(), true); + SkillUtils.handleDurabilityChange(getPlayer().getItemInHand(), Config.getInstance().getAbilityToolDamage()); } } diff --git a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java index 7feaaed0e..341fa340a 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java @@ -67,7 +67,7 @@ public class MiningManager extends SkillManager { Material material = blockState.getType(); if (mcMMOPlayer.getAbilityMode(skill.getAbility())) { - SkillUtils.handleDurabilityChange(getPlayer().getItemInHand(), Config.getInstance().getAbilityToolDamage(), true); + SkillUtils.handleDurabilityChange(getPlayer().getItemInHand(), Config.getInstance().getAbilityToolDamage()); } if ((ModUtils.isCustomMiningBlock(blockState) && !ModUtils.getCustomBlock(blockState).isDoubleDropEnabled()) || material != Material.GLOWING_REDSTONE_ORE && !Config.getInstance().getDoubleDropsEnabled(skill, material)) { diff --git a/src/main/java/com/gmail/nossr50/skills/woodcutting/Woodcutting.java b/src/main/java/com/gmail/nossr50/skills/woodcutting/Woodcutting.java index e8a1a450b..0dc636946 100644 --- a/src/main/java/com/gmail/nossr50/skills/woodcutting/Woodcutting.java +++ b/src/main/java/com/gmail/nossr50/skills/woodcutting/Woodcutting.java @@ -5,10 +5,8 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Set; -import org.bukkit.Material; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockState; -import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.material.Tree; @@ -213,23 +211,16 @@ public final class Woodcutting { * @return True if the tool can sustain the durability loss */ protected static boolean handleDurabilityLoss(Set treeFellerBlocks, ItemStack inHand) { - Material inHandMaterial = inHand.getType(); - - if (inHandMaterial == Material.AIR) { - return false; - } - short durabilityLoss = 0; - int unbreakingLevel = inHand.getEnchantmentLevel(Enchantment.DURABILITY); for (BlockState blockState : treeFellerBlocks) { - if (BlockUtils.isLog(blockState) && Misc.getRandom().nextInt(unbreakingLevel + 1) == 0) { + if (BlockUtils.isLog(blockState)) { durabilityLoss += Config.getInstance().getAbilityToolDamage(); } } SkillUtils.handleDurabilityChange(inHand, durabilityLoss); - return (inHand.getDurability() < inHandMaterial.getMaxDurability()); + return (inHand.getDurability() < inHand.getType().getMaxDurability()); } /** diff --git a/src/main/java/com/gmail/nossr50/util/skills/SkillUtils.java b/src/main/java/com/gmail/nossr50/util/skills/SkillUtils.java index 5beeb0252..7251962ab 100644 --- a/src/main/java/com/gmail/nossr50/util/skills/SkillUtils.java +++ b/src/main/java/com/gmail/nossr50/util/skills/SkillUtils.java @@ -12,6 +12,7 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.HiddenConfig; @@ -167,23 +168,23 @@ public class SkillUtils { } } - public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier, boolean handleUnbreaking) { - if (handleUnbreaking) { - double modifier = 1 / (itemStack.getEnchantmentLevel(Enchantment.DURABILITY) + 1); - durabilityModifier = (int) (durabilityModifier * modifier); - } - - handleDurabilityChange(itemStack, durabilityModifier); + public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier) { + handleDurabilityChange(itemStack, durabilityModifier, 1.0); } /** * Modify the durability of an ItemStack. * * @param itemStack The ItemStack which durability should be modified - * @return the itemStack with modified durability + * @param durabilityModifier the amount to modify the durability by + * @param maxDamageModifier the amount to adjust the max damage by */ - public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier) { - itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, itemStack.getType().getMaxDurability())); + public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier, double maxDamageModifier) { + Material type = itemStack.getType(); + short maxDurability = mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability(); + durabilityModifier = (int) Math.min(durabilityModifier / itemStack.getEnchantmentLevel(Enchantment.DURABILITY) + 1, maxDurability * maxDamageModifier); + + itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability)); } public static boolean activationSuccessful(Player player, SkillType skill, double maxChance, int maxLevel) {