From ddbf5a559a3402e1a2a6f538e4fc77da96aa2b6a Mon Sep 17 00:00:00 2001 From: GJ Date: Mon, 21 Jan 2013 20:01:33 -0500 Subject: [PATCH] Copy-pasta code is bad. --- .../gmail/nossr50/skills/SkillManager.java | 8 +++++ .../skills/acrobatics/AcrobaticsManager.java | 14 ++------ .../skills/archery/ArcheryManager.java | 14 ++------ .../gmail/nossr50/skills/axes/AxeManager.java | 7 +--- .../skills/axes/ImpactEventHandler.java | 7 +--- .../nossr50/skills/gathering/Excavation.java | 8 ++--- .../nossr50/skills/gathering/Fishing.java | 18 ++++------ .../nossr50/skills/gathering/Herbalism.java | 35 ++++++------------- .../nossr50/skills/gathering/WoodCutting.java | 7 ++-- .../nossr50/skills/mining/MiningManager.java | 7 +--- .../gmail/nossr50/skills/repair/Repair.java | 15 +++----- .../nossr50/skills/swords/SwordsManager.java | 16 ++------- .../nossr50/skills/taming/TamingManager.java | 16 ++------- .../skills/unarmed/UnarmedManager.java | 24 ++----------- .../java/com/gmail/nossr50/util/Misc.java | 16 +++++++++ .../com/gmail/nossr50/util/Permissions.java | 6 ++++ 16 files changed, 69 insertions(+), 149 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/skills/SkillManager.java b/src/main/java/com/gmail/nossr50/skills/SkillManager.java index 31cef8270..246aba979 100644 --- a/src/main/java/com/gmail/nossr50/skills/SkillManager.java +++ b/src/main/java/com/gmail/nossr50/skills/SkillManager.java @@ -4,17 +4,21 @@ import org.bukkit.entity.Player; import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.SkillType; +import com.gmail.nossr50.util.Misc; +import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; public abstract class SkillManager { protected Player player; protected PlayerProfile profile; protected int skillLevel; + protected int activationChance; public SkillManager(Player player, SkillType skill) { this.player = player; this.profile = Users.getProfile(player); this.skillLevel = profile.getSkillLevel(skill); + this.activationChance = Misc.calculateActivationChance(Permissions.lucky(player, skill)); } public Player getPlayer() { @@ -28,4 +32,8 @@ public abstract class SkillManager { public int getSkillLevel() { return skillLevel; } + + public int getActivationChance() { + return activationChance; + } } diff --git a/src/main/java/com/gmail/nossr50/skills/acrobatics/AcrobaticsManager.java b/src/main/java/com/gmail/nossr50/skills/acrobatics/AcrobaticsManager.java index 6ffcae96a..2dabf9d6d 100644 --- a/src/main/java/com/gmail/nossr50/skills/acrobatics/AcrobaticsManager.java +++ b/src/main/java/com/gmail/nossr50/skills/acrobatics/AcrobaticsManager.java @@ -30,11 +30,6 @@ public class AcrobaticsManager extends SkillManager { RollEventHandler eventHandler = new RollEventHandler(this, event); - int randomChance = 100; - if (Permissions.luckyAcrobatics(player)) { - randomChance = 75; - } - double chance; if (eventHandler.isGraceful) { @@ -44,7 +39,7 @@ public class AcrobaticsManager extends SkillManager { chance = (Acrobatics.rollMaxChance / Acrobatics.rollMaxBonusLevel) * eventHandler.skillModifier; } - if (chance > Misc.getRandom().nextInt(randomChance) && !eventHandler.isFatal(eventHandler.modifiedDamage)) { + if (chance > Misc.getRandom().nextInt(activationChance) && !eventHandler.isFatal(eventHandler.modifiedDamage)) { eventHandler.modifyEventDamage(); eventHandler.sendAbilityMessage(); eventHandler.processXPGain(eventHandler.damage * Acrobatics.rollXpModifier); @@ -66,14 +61,9 @@ public class AcrobaticsManager extends SkillManager { DodgeEventHandler eventHandler = new DodgeEventHandler(this, event); - int randomChance = 100; - if (Permissions.luckyAcrobatics(player)) { - randomChance = 75; - } - double chance = (Acrobatics.dodgeMaxChance / Acrobatics.dodgeMaxBonusLevel) * eventHandler.skillModifier; - if (chance > Misc.getRandom().nextInt(randomChance) && !eventHandler.isFatal(eventHandler.modifiedDamage)) { + if (chance > Misc.getRandom().nextInt(activationChance) && !eventHandler.isFatal(eventHandler.modifiedDamage)) { eventHandler.modifyEventDamage(); eventHandler.sendAbilityMessage(); eventHandler.processXPGain(eventHandler.damage * Acrobatics.dodgeXpModifier); diff --git a/src/main/java/com/gmail/nossr50/skills/archery/ArcheryManager.java b/src/main/java/com/gmail/nossr50/skills/archery/ArcheryManager.java index 454031d82..2ac83ecd1 100644 --- a/src/main/java/com/gmail/nossr50/skills/archery/ArcheryManager.java +++ b/src/main/java/com/gmail/nossr50/skills/archery/ArcheryManager.java @@ -26,14 +26,9 @@ public class ArcheryManager extends SkillManager { ArrowTrackingEventHandler eventHandler = new ArrowTrackingEventHandler(this, livingEntity); - int randomChance = 100; - if (Permissions.luckyArchery(player)) { - randomChance = 75; - } - double chance = (Archery.retrieveMaxChance / Archery.retrieveMaxBonusLevel) * eventHandler.skillModifier; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { eventHandler.addToTracker(); } } @@ -51,14 +46,9 @@ public class ArcheryManager extends SkillManager { DazeEventHandler eventHandler = new DazeEventHandler(this, event, defender); - int randomChance = 100; - if (Permissions.luckyArchery(player)) { - randomChance = 75; - } - double chance = (Archery.dazeMaxBonus / Archery.dazeMaxBonusLevel) * eventHandler.skillModifier; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { eventHandler.handleDazeEffect(); eventHandler.sendAbilityMessages(); } diff --git a/src/main/java/com/gmail/nossr50/skills/axes/AxeManager.java b/src/main/java/com/gmail/nossr50/skills/axes/AxeManager.java index 27b7ed3a2..fd1f420ba 100644 --- a/src/main/java/com/gmail/nossr50/skills/axes/AxeManager.java +++ b/src/main/java/com/gmail/nossr50/skills/axes/AxeManager.java @@ -47,14 +47,9 @@ public class AxeManager extends SkillManager { return; } - int randomChance = 100; - if (Permissions.luckyAxes(player)) { - randomChance = 75; - } - double chance = (Axes.criticalHitMaxChance / Axes.criticalHitMaxBonusLevel) * eventHandler.skillModifier; - if (chance > Misc.getRandom().nextInt(randomChance) && !eventHandler.defender.isDead()) { + if (chance > Misc.getRandom().nextInt(activationChance) && !eventHandler.defender.isDead()) { eventHandler.modifyEventDamage(); eventHandler.sendAbilityMessages(); } diff --git a/src/main/java/com/gmail/nossr50/skills/axes/ImpactEventHandler.java b/src/main/java/com/gmail/nossr50/skills/axes/ImpactEventHandler.java index 1a62fdfd9..8572be315 100644 --- a/src/main/java/com/gmail/nossr50/skills/axes/ImpactEventHandler.java +++ b/src/main/java/com/gmail/nossr50/skills/axes/ImpactEventHandler.java @@ -62,12 +62,7 @@ public class ImpactEventHandler { return; } - int randomChance = 100; - if (Permissions.luckyAxes(player)) { - randomChance = 75; - } - - if (Misc.getRandom().nextInt(randomChance) <= Axes.greaterImpactChance) { + if (Misc.getRandom().nextInt(manager.getActivationChance()) <= Axes.greaterImpactChance) { handleGreaterImpactEffect(); sendAbilityMessge(); } diff --git a/src/main/java/com/gmail/nossr50/skills/gathering/Excavation.java b/src/main/java/com/gmail/nossr50/skills/gathering/Excavation.java index 39e6d67f6..fbe2eeca0 100644 --- a/src/main/java/com/gmail/nossr50/skills/gathering/Excavation.java +++ b/src/main/java/com/gmail/nossr50/skills/gathering/Excavation.java @@ -94,13 +94,9 @@ public class Excavation { for (ExcavationTreasure treasure : treasures) { if (skillLevel >= treasure.getDropLevel()) { - int randomChance = 100; + int activationChance = Misc.calculateActivationChance(Permissions.luckyExcavation(player)); - if (Permissions.luckyExcavation(player)) { - randomChance = 75; - } - - if (Misc.getRandom().nextDouble() * randomChance <= treasure.getDropChance()) { + if (Misc.getRandom().nextDouble() * activationChance <= treasure.getDropChance()) { xp += treasure.getXp(); is.add(treasure.getDrop()); } diff --git a/src/main/java/com/gmail/nossr50/skills/gathering/Fishing.java b/src/main/java/com/gmail/nossr50/skills/gathering/Fishing.java index 1bc4923f0..aaacc6ccb 100755 --- a/src/main/java/com/gmail/nossr50/skills/gathering/Fishing.java +++ b/src/main/java/com/gmail/nossr50/skills/gathering/Fishing.java @@ -95,12 +95,9 @@ public class Fishing { FishingTreasure foundTreasure = rewards.get(Misc.getRandom().nextInt(rewards.size())); - int randomChance = 100; - if (Permissions.luckyFishing(player)) { - randomChance = 75; - } + int activationChance = Misc.calculateActivationChance(Permissions.luckyFishing(player)); - if (Misc.getRandom().nextDouble() * randomChance <= foundTreasure.getDropChance()) { + if (Misc.getRandom().nextDouble() * activationChance <= foundTreasure.getDropChance()) { Users.getPlayer(player).addXP(SkillType.FISHING, foundTreasure.getXp()); theCatch.setItemStack(foundTreasure.getDrop()); } @@ -144,14 +141,10 @@ public class Fishing { player.sendMessage(LocaleLoader.getString("Fishing.ItemFound")); if (ItemChecks.isEnchantable(fishingResults)) { - int randomChance = 100; - - if (Permissions.luckyFishing(player)) { - randomChance = 75; - } + int activationChance = Misc.calculateActivationChance(Permissions.luckyFishing(player)); if (player.getWorld().hasStorm()) { - randomChance = (int) (randomChance * 0.909); + activationChance = (int) (activationChance * 0.909); } /* CHANCE OF ITEM BEING ENCHANTED @@ -161,7 +154,7 @@ public class Fishing { * 20% - Tier 4 * 25% - Tier 5 */ - if (Misc.getRandom().nextInt(randomChance) <= (lootTier * magicHunterMultiplier) && Permissions.fishingMagic(player)) { + if (Misc.getRandom().nextInt(activationChance) <= (lootTier * magicHunterMultiplier) && Permissions.fishingMagic(player)) { for (Enchantment newEnchant : Enchantment.values()) { boolean conflicts = false; @@ -218,6 +211,7 @@ public class Fishing { public static void shakeMob(PlayerFishEvent event) { int randomChance = 100; + //TODO: Invert this so it matches the rest of our lucky checks... if (Permissions.luckyFishing(event.getPlayer())) { randomChance = 125; } diff --git a/src/main/java/com/gmail/nossr50/skills/gathering/Herbalism.java b/src/main/java/com/gmail/nossr50/skills/gathering/Herbalism.java index 6b107ab83..8b944c14a 100644 --- a/src/main/java/com/gmail/nossr50/skills/gathering/Herbalism.java +++ b/src/main/java/com/gmail/nossr50/skills/gathering/Herbalism.java @@ -101,11 +101,7 @@ public class Herbalism { boolean customPlant = false; - int randomChance = 100; - - if (Permissions.luckyHerbalism(player)) { - randomChance = 75; - } + int activationChance = Misc.calculateActivationChance(Permissions.luckyHerbalism(player)); float chance = (float) ((MAX_CHANCE / MAX_BONUS_LEVEL) * herbLevel); if (chance > MAX_CHANCE) chance = (float) MAX_CHANCE; @@ -125,7 +121,7 @@ public class Herbalism { if (b.getType().equals(Material.CACTUS)) { mat = Material.CACTUS; if (!mcMMO.placeStore.isTrue(b)) { - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { catciDrops++; } xp += Config.getInstance().getHerbalismXPCactus(); @@ -185,7 +181,7 @@ public class Herbalism { if (b.getType().equals(Material.SUGAR_CANE_BLOCK)) { mat = Material.SUGAR_CANE; if (!mcMMO.placeStore.isTrue(b)) { - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { caneDrops++; } xp += Config.getInstance().getHerbalismXPSugarCane(); @@ -286,7 +282,7 @@ public class Herbalism { } } - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { Config configInstance = Config.getInstance(); switch (type) { @@ -448,16 +444,12 @@ public class Herbalism { break; } - int randomChance = 100; - - if (Permissions.luckyHerbalism(player)) { - randomChance = 75; - } + int activationChance = Misc.calculateActivationChance(Permissions.luckyHerbalism(player)); float chance = (float) (((double) MAX_CHANCE / (double) MAX_BONUS_LEVEL) * herbLevel); if (chance > MAX_CHANCE) chance = MAX_CHANCE; - if (hasSeeds && profile.getAbilityMode(AbilityType.GREEN_TERRA) || hasSeeds && (chance > Misc.getRandom().nextInt(randomChance))) { + if (hasSeeds && profile.getAbilityMode(AbilityType.GREEN_TERRA) || hasSeeds && (chance > Misc.getRandom().nextInt(activationChance))) { event.setCancelled(true); switch(type) { @@ -522,16 +514,12 @@ public class Herbalism { player.setItemInHand(new ItemStack(Material.SEEDS, seeds - 1)); - int randomChance = 100; - - if (Permissions.luckyHerbalism(player)) { - randomChance = 75; - } + int activationChance = Misc.calculateActivationChance(Permissions.luckyHerbalism(player)); float chance = (float) (((double) MAX_CHANCE / (double) MAX_BONUS_LEVEL) * skillLevel); if (chance > MAX_CHANCE) chance = MAX_CHANCE; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { greenTerraConvert(player, block); } else { @@ -542,12 +530,9 @@ public class Herbalism { public static void hylianLuck(Block block, Player player, BlockBreakEvent event) { int chance = Users.getProfile(player).getSkillLevel(SkillType.HERBALISM) / 100; - int randomChance = 100; - if (Permissions.luckyHerbalism(player)) { - randomChance = 75; - } + int activationChance = Misc.calculateActivationChance(Permissions.luckyHerbalism(player)); - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { Location location = block.getLocation(); int dropNumber = Misc.getRandom().nextInt(3); ItemStack item = null; diff --git a/src/main/java/com/gmail/nossr50/skills/gathering/WoodCutting.java b/src/main/java/com/gmail/nossr50/skills/gathering/WoodCutting.java index b6078cf93..07d0ae4b6 100644 --- a/src/main/java/com/gmail/nossr50/skills/gathering/WoodCutting.java +++ b/src/main/java/com/gmail/nossr50/skills/gathering/WoodCutting.java @@ -353,15 +353,12 @@ public class WoodCutting { Material mat = Material.getMaterial(block.getTypeId()); - int randomChance = 100; int chance = (int) (((double) MAX_CHANCE / (double) MAX_BONUS_LEVEL) * skillLevel); if (chance > MAX_CHANCE) chance = MAX_CHANCE; - if (Permissions.luckyWoodcutting(player)) { - randomChance = 75; - } + int activationChance = Misc.calculateActivationChance(Permissions.luckyWoodcutting(player)); - if (chance > Misc.getRandom().nextInt(randomChance) && Permissions.woodcuttingDoubleDrops(player)) { + if (chance > Misc.getRandom().nextInt(activationChance) && Permissions.woodcuttingDoubleDrops(player)) { Config configInstance = Config.getInstance(); ItemStack item; Location location; 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 ce9cfc711..94b6c88a0 100644 --- a/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java +++ b/src/main/java/com/gmail/nossr50/skills/mining/MiningManager.java @@ -132,14 +132,9 @@ public class MiningManager extends SkillManager{ return; } - int randomChance = 100; - if (Permissions.luckyMining(player)) { - randomChance = 75; - } - float chance = ((float) Mining.DOUBLE_DROPS_MAX_CHANCE / Mining.DOUBLE_DROPS_MAX_BONUS_LEVEL) * eventHandler.skillModifier; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { eventHandler.processDrops(); } } diff --git a/src/main/java/com/gmail/nossr50/skills/repair/Repair.java b/src/main/java/com/gmail/nossr50/skills/repair/Repair.java index 9b0d8ade4..9dd8ffc71 100644 --- a/src/main/java/com/gmail/nossr50/skills/repair/Repair.java +++ b/src/main/java/com/gmail/nossr50/skills/repair/Repair.java @@ -110,17 +110,13 @@ public class Repair { for (Entry enchant : enchants.entrySet()) { Enchantment enchantment = enchant.getKey(); - int randomChance = 100; + int activationChance = Misc.calculateActivationChance(Permissions.luckyRepair(player)); - if (Permissions.luckyRepair(player)) { - randomChance = 75; - } - - if (Misc.getRandom().nextInt(randomChance) <= getEnchantChance(rank)) { + if (Misc.getRandom().nextInt(activationChance) <= getEnchantChance(rank)) { int enchantLevel = enchant.getValue(); if (advancedConfig.getArcaneForgingDowngradeEnabled() && enchantLevel > 1) { - if (Misc.getRandom().nextInt(randomChance) < getDowngradeChance(rank)) { + if (Misc.getRandom().nextInt(activationChance) < getDowngradeChance(rank)) { is.addEnchantment(enchantment, --enchantLevel); downgraded = true; } @@ -238,13 +234,12 @@ public class Repair { public static boolean checkPlayerProcRepair(Player player) { int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR); - int randomChance = 100; int chance = (int) (((double) SUPER_REPAIR_CHANCE_MAX / (double) SUPER_REPAIR_MAX_BONUS_LEVEL) * skillLevel); if (skillLevel >= SUPER_REPAIR_MAX_BONUS_LEVEL) chance = SUPER_REPAIR_CHANCE_MAX; - if (Permissions.luckyRepair(player)) randomChance = 75; + int activationChance = Misc.calculateActivationChance(Permissions.luckyRepair(player)); - if (chance > Misc.getRandom().nextInt(randomChance) && Permissions.repairBonus(player)) { + if (chance > Misc.getRandom().nextInt(activationChance) && Permissions.repairBonus(player)) { player.sendMessage(LocaleLoader.getString("Repair.Skills.FeltEasy")); return true; } diff --git a/src/main/java/com/gmail/nossr50/skills/swords/SwordsManager.java b/src/main/java/com/gmail/nossr50/skills/swords/SwordsManager.java index 93a461230..581311173 100644 --- a/src/main/java/com/gmail/nossr50/skills/swords/SwordsManager.java +++ b/src/main/java/com/gmail/nossr50/skills/swords/SwordsManager.java @@ -31,16 +31,10 @@ public class SwordsManager extends SkillManager { if (Combat.shouldBeAffected(player, defender)) { BleedEventHandler eventHandler = new BleedEventHandler(this, defender); - int randomChance = 100; - - if (Permissions.luckySwords(player)) { - randomChance = 75; - } - float chance = (float) (((double) Swords.BLEED_CHANCE_MAX / (double) Swords.BLEED_MAX_BONUS_LEVEL) * skillLevel); if (chance > Swords.BLEED_CHANCE_MAX) chance = Swords.BLEED_CHANCE_MAX; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { eventHandler.addBleedTicks(); eventHandler.sendAbilityMessages(); } @@ -60,16 +54,10 @@ public class SwordsManager extends SkillManager { if (eventHandler.isHoldingSword()) { eventHandler.calculateSkillModifier(); - int randomChance = 100; - - if (Permissions.luckySwords(player)) { - randomChance = 75; - } - float chance = (float) (((double) Swords.COUNTER_ATTACK_CHANCE_MAX / (double) Swords.COUNTER_ATTACK_MAX_BONUS_LEVEL) * skillLevel); if (chance > Swords.COUNTER_ATTACK_CHANCE_MAX) chance = Swords.COUNTER_ATTACK_CHANCE_MAX; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { eventHandler.dealDamage(); eventHandler.sendAbilityMessages(); } diff --git a/src/main/java/com/gmail/nossr50/skills/taming/TamingManager.java b/src/main/java/com/gmail/nossr50/skills/taming/TamingManager.java index c0481a03a..80405e1f9 100644 --- a/src/main/java/com/gmail/nossr50/skills/taming/TamingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/taming/TamingManager.java @@ -37,13 +37,7 @@ public class TamingManager extends SkillManager { } if (skillLevel >= Taming.FAST_FOOD_SERVICE_ACTIVATION_LEVEL) { - int randomChance = 100; - - if (Permissions.luckyTaming(player)) { - randomChance = 75; - } - - if (Misc.getRandom().nextInt(randomChance) < Taming.FAST_FOOD_SERVICE_ACTIVATION_CHANCE) { + if (Misc.getRandom().nextInt(activationChance) < Taming.FAST_FOOD_SERVICE_ACTIVATION_CHANCE) { FastFoodServiceEventHandler eventHandler = new FastFoodServiceEventHandler(wolf); eventHandler.modifyHealth(damage); @@ -86,16 +80,10 @@ public class TamingManager extends SkillManager { GoreEventHandler eventHandler = new GoreEventHandler(this, event); - int randomChance = 100; - - if (Permissions.luckyTaming(player)) { - randomChance = 75; - } - float chance = (float) (((double) Taming.GORE_CHANCE_MAX / (double) Taming.GORE_MAX_BONUS_LEVEL) * skillLevel); if (chance > Taming.GORE_CHANCE_MAX) chance = Taming.GORE_CHANCE_MAX; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { eventHandler.modifyEventDamage(); eventHandler.applyBleed(); eventHandler.sendAbilityMessage(); diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java index 9357d2a6e..0a41d3ad7 100644 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java +++ b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java @@ -28,16 +28,10 @@ public class UnarmedManager extends SkillManager { if (eventHandler.isHoldingItem()) { eventHandler.calculateSkillModifier(); - int randomChance = 100; - - if (Permissions.luckyUnarmed(player)) { - randomChance = 75; - } - float chance = (float) (((double) Unarmed.DISARM_MAX_CHANCE / (double) Unarmed.DISARM_MAX_BONUS_LEVEL) * skillLevel); if (chance > Unarmed.DISARM_MAX_CHANCE) chance = Unarmed.DISARM_MAX_CHANCE; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { if (!hasIronGrip(defender)) { eventHandler.handleDisarm(); } @@ -61,16 +55,10 @@ public class UnarmedManager extends SkillManager { DeflectEventHandler eventHandler = new DeflectEventHandler(this, event); - int randomChance = 100; - - if (Permissions.luckyUnarmed(player)) { - randomChance = 75; - } - float chance = (float) (((double) Unarmed.DEFLECT_MAX_CHANCE / (double) Unarmed.DEFLECT_MAX_BONUS_LEVEL) * skillLevel); if (chance > Unarmed.DEFLECT_MAX_CHANCE) chance = Unarmed.DEFLECT_MAX_CHANCE; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { eventHandler.cancelEvent(); eventHandler.sendAbilityMessage(); } @@ -111,16 +99,10 @@ public class UnarmedManager extends SkillManager { IronGripEventHandler eventHandler = new IronGripEventHandler(this, defender); - int randomChance = 100; - - if (Permissions.luckyUnarmed(defender)) { - randomChance = 75; - } - float chance = (float) (((double) Unarmed.IRON_GRIP_MAX_CHANCE / (double) Unarmed.IRON_GRIP_MAX_BONUS_LEVEL) * skillLevel); if (chance > Unarmed.IRON_GRIP_MAX_CHANCE) chance = Unarmed.IRON_GRIP_MAX_CHANCE; - if (chance > Misc.getRandom().nextInt(randomChance)) { + if (chance > Misc.getRandom().nextInt(activationChance)) { eventHandler.sendAbilityMessages(); return true; } diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index 4135cebdd..0f97a4fa1 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -33,6 +33,22 @@ public class Misc { public static final int PLAYER_RESPAWN_COOLDOWN_SECONDS = 5; public static final int TIME_CONVERSION_FACTOR = 1000; public static final double SKILL_MESSAGE_MAX_SENDING_DISTANCE = 10.0; + public static final int NORMAL_SKILL_ACTIVATION_CHANCE = 100; + public static final int LUCKY_SKILL_ACTIVATION_CHANCE = 75; + + /** + * Calculate activation chance for a skill. + * + * @param isLucky true if the player has the appropriate "lucky" perk, false otherwise + * @return the activation chance + */ + public static int calculateActivationChance(boolean isLucky) { + if (isLucky) { + return LUCKY_SKILL_ACTIVATION_CHANCE; + } + + return NORMAL_SKILL_ACTIVATION_CHANCE; + } /** * Check if a player has armor. diff --git a/src/main/java/com/gmail/nossr50/util/Permissions.java b/src/main/java/com/gmail/nossr50/util/Permissions.java index 8ace4726e..0c1dfdfdc 100644 --- a/src/main/java/com/gmail/nossr50/util/Permissions.java +++ b/src/main/java/com/gmail/nossr50/util/Permissions.java @@ -3,6 +3,8 @@ package com.gmail.nossr50.util; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import com.gmail.nossr50.datatypes.SkillType; + public class Permissions { public static boolean hasPermission(CommandSender sender, String perm) { @@ -75,6 +77,10 @@ public class Permissions { * MCMMO.PERKS.LUCKY* */ + public static boolean lucky(Player player, SkillType skill) { + return hasPermission(player, "mcmmo.perks.lucky." + skill.toString().toLowerCase()); + } + public static boolean luckyAcrobatics(Player player) { return hasPermission(player, "mcmmo.perks.lucky.acrobatics"); }