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.
This commit is contained in:
GJ 2013-11-06 08:45:20 -05:00
parent c31f5b5782
commit cf56ca35eb
5 changed files with 16 additions and 33 deletions

View File

@ -1,12 +1,9 @@
package com.gmail.nossr50.skills.axes; package com.gmail.nossr50.skills.axes;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.skills.AbilityType; import com.gmail.nossr50.datatypes.skills.AbilityType;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
@ -98,13 +95,7 @@ public class AxesManager extends SkillManager {
for (ItemStack armor : target.getEquipment().getArmorContents()) { for (ItemStack armor : target.getEquipment().getArmorContents()) {
if (ItemUtils.isArmor(armor) && Axes.impactChance > Misc.getRandom().nextInt(getActivationChance())) { if (ItemUtils.isArmor(armor) && Axes.impactChance > Misc.getRandom().nextInt(getActivationChance())) {
Material armorType = armor.getType(); SkillUtils.handleDurabilityChange(armor, durabilityDamage, Axes.impactMaxDurabilityModifier);
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));
} }
} }
} }

View File

@ -55,6 +55,6 @@ public class ExcavationManager extends SkillManager {
excavationBlockCheck(blockState); excavationBlockCheck(blockState);
excavationBlockCheck(blockState); excavationBlockCheck(blockState);
SkillUtils.handleDurabilityChange(getPlayer().getItemInHand(), Config.getInstance().getAbilityToolDamage(), true); SkillUtils.handleDurabilityChange(getPlayer().getItemInHand(), Config.getInstance().getAbilityToolDamage());
} }
} }

View File

@ -67,7 +67,7 @@ public class MiningManager extends SkillManager {
Material material = blockState.getType(); Material material = blockState.getType();
if (mcMMOPlayer.getAbilityMode(skill.getAbility())) { 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)) { if ((ModUtils.isCustomMiningBlock(blockState) && !ModUtils.getCustomBlock(blockState).isDoubleDropEnabled()) || material != Material.GLOWING_REDSTONE_ORE && !Config.getInstance().getDoubleDropsEnabled(skill, material)) {

View File

@ -5,10 +5,8 @@ import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.bukkit.Material;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState; import org.bukkit.block.BlockState;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Tree; import org.bukkit.material.Tree;
@ -213,23 +211,16 @@ public final class Woodcutting {
* @return True if the tool can sustain the durability loss * @return True if the tool can sustain the durability loss
*/ */
protected static boolean handleDurabilityLoss(Set<BlockState> treeFellerBlocks, ItemStack inHand) { protected static boolean handleDurabilityLoss(Set<BlockState> treeFellerBlocks, ItemStack inHand) {
Material inHandMaterial = inHand.getType();
if (inHandMaterial == Material.AIR) {
return false;
}
short durabilityLoss = 0; short durabilityLoss = 0;
int unbreakingLevel = inHand.getEnchantmentLevel(Enchantment.DURABILITY);
for (BlockState blockState : treeFellerBlocks) { for (BlockState blockState : treeFellerBlocks) {
if (BlockUtils.isLog(blockState) && Misc.getRandom().nextInt(unbreakingLevel + 1) == 0) { if (BlockUtils.isLog(blockState)) {
durabilityLoss += Config.getInstance().getAbilityToolDamage(); durabilityLoss += Config.getInstance().getAbilityToolDamage();
} }
} }
SkillUtils.handleDurabilityChange(inHand, durabilityLoss); SkillUtils.handleDurabilityChange(inHand, durabilityLoss);
return (inHand.getDurability() < inHandMaterial.getMaxDurability()); return (inHand.getDurability() < inHand.getType().getMaxDurability());
} }
/** /**

View File

@ -12,6 +12,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.config.HiddenConfig; import com.gmail.nossr50.config.HiddenConfig;
@ -167,23 +168,23 @@ public class SkillUtils {
} }
} }
public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier, boolean handleUnbreaking) { public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier) {
if (handleUnbreaking) { handleDurabilityChange(itemStack, durabilityModifier, 1.0);
double modifier = 1 / (itemStack.getEnchantmentLevel(Enchantment.DURABILITY) + 1);
durabilityModifier = (int) (durabilityModifier * modifier);
}
handleDurabilityChange(itemStack, durabilityModifier);
} }
/** /**
* Modify the durability of an ItemStack. * Modify the durability of an ItemStack.
* *
* @param itemStack The ItemStack which durability should be modified * @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) { public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier, double maxDamageModifier) {
itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, itemStack.getType().getMaxDurability())); 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) { public static boolean activationSuccessful(Player player, SkillType skill, double maxChance, int maxLevel) {