From 0587741437d3e9fb85a1bee1c70e59ff8e4202c3 Mon Sep 17 00:00:00 2001 From: GJ Date: Mon, 14 Jan 2013 11:55:43 -0500 Subject: [PATCH] More Axe restructuring, changing some config values from int to double, added function to duplicate code. --- .../gmail/nossr50/config/AdvancedConfig.java | 6 +- .../axes/AxeBonusDamageEventHandler.java | 6 +- .../gmail/nossr50/skills/axes/AxeManager.java | 34 ++++++++- .../com/gmail/nossr50/skills/axes/Axes.java | 74 ++----------------- .../skills/axes/CriticalHitEventHandler.java | 47 ++++++++++++ .../java/com/gmail/nossr50/util/Combat.java | 22 +----- .../java/com/gmail/nossr50/util/Misc.java | 19 +++++ src/main/resources/advanced.yml | 6 +- 8 files changed, 120 insertions(+), 94 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/skills/axes/CriticalHitEventHandler.java diff --git a/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java b/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java index 1b705a5c6..af8a45916 100644 --- a/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java +++ b/src/main/java/com/gmail/nossr50/config/AdvancedConfig.java @@ -51,10 +51,10 @@ public class AdvancedConfig extends ConfigLoader { public int getBonusDamageAxesBonusMax() { return config.getInt("Skills.Axes.DamageIncrease_MaxBonus", 4); } public int getBonusDamageAxesMaxBonusLevel() { return config.getInt("Skills.Axes.DamageIncrease_MaxBonusLevel", 200); } - public double getAxesCriticalChance() { return config.getDouble("Skills.Axes.AxesCritical_MaxChance", 37.50); } + public double getAxesCriticalChance() { return config.getDouble("Skills.Axes.AxesCritical_MaxChance", 37.50D); } public int getAxesCriticalMaxBonusLevel() { return config.getInt("Skills.Axes.AxesCritical_MaxBonusLevel", 750); } - public double getAxesCriticalPVPModifier() { return config.getDouble("Skills.Axes.AxesCritical_PVP_Modifier", 1.5); } - public int getAxesCriticalPVEModifier() { return config.getInt("Skills.Axes.AxesCritical_PVE_Modifier", 2); } + public double getAxesCriticalPVPModifier() { return config.getDouble("Skills.Axes.AxesCritical_PVP_Modifier", 1.5D); } + public double getAxesCriticalPVEModifier() { return config.getDouble("Skills.Axes.AxesCritical_PVE_Modifier", 2.0D); } public int getGreaterImpactChance() { return config.getInt("Skills.Axes.GreaterImpact_Chance", 25); } public double getGreaterImpactModifier() { return config.getDouble("Skills.Axes.GreaterImpact_KnockbackModifier", 1.5); } diff --git a/src/main/java/com/gmail/nossr50/skills/axes/AxeBonusDamageEventHandler.java b/src/main/java/com/gmail/nossr50/skills/axes/AxeBonusDamageEventHandler.java index cac1ac15d..7c53f2529 100644 --- a/src/main/java/com/gmail/nossr50/skills/axes/AxeBonusDamageEventHandler.java +++ b/src/main/java/com/gmail/nossr50/skills/axes/AxeBonusDamageEventHandler.java @@ -13,13 +13,13 @@ public class AxeBonusDamageEventHandler { } protected void calculateDamageBonus() { - int increaseLevel = Axes.maxBonusLevel / Axes.maxBonusDamage; + int increaseLevel = Axes.bonusDamageMaxBonusLevel / Axes.bonusDamageMaxBonus; /* Add 1 DMG for every 50 skill levels (default value) */ damageBonus = skillLevel / increaseLevel; - if (damageBonus > Axes.maxBonusDamage) { - damageBonus = Axes.maxBonusDamage; + if (damageBonus > Axes.bonusDamageMaxBonus) { + damageBonus = Axes.bonusDamageMaxBonus; } } 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 b4b45487d..b9ef45c3c 100644 --- a/src/main/java/com/gmail/nossr50/skills/axes/AxeManager.java +++ b/src/main/java/com/gmail/nossr50/skills/axes/AxeManager.java @@ -1,11 +1,13 @@ package com.gmail.nossr50.skills.axes; import org.bukkit.entity.Player; +import org.bukkit.entity.Tameable; import org.bukkit.event.entity.EntityDamageByEntityEvent; import com.gmail.nossr50.datatypes.SkillType; import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.util.Misc; +import com.gmail.nossr50.util.Permissions; public class AxeManager extends SkillManager { public AxeManager(Player player) { @@ -18,7 +20,7 @@ public class AxeManager extends SkillManager { * @param event The event to modify */ public void bonusDamage(EntityDamageByEntityEvent event) { - if (Misc.isNPC(player)) { + if (Misc.isNPC(player) || !Permissions.axeBonus(player)) { return; } @@ -27,4 +29,34 @@ public class AxeManager extends SkillManager { eventHandler.calculateDamageBonus(); eventHandler.modifyEventDamage(); } + + /** + * Check for critical chances on axe damage. + * + * @param event The event to modify + */ + public void criticalHitCheck(EntityDamageByEntityEvent event) { + if (Misc.isNPC(player) || !Permissions.criticalHit(player)) { + return; + } + + CriticalHitEventHandler eventHandler = new CriticalHitEventHandler(this, event); + + if (eventHandler.defender instanceof Tameable && Misc.isFriendlyPet(player, (Tameable) eventHandler.defender)) { + return; + } + + int randomChance = 100; + if (Permissions.luckyAxes(player)) { + randomChance = (int) (randomChance * 0.75); + } + + double chance = (Axes.criticalHitMaxChance / Axes.criticalHitMaxBonusLevel) * eventHandler.skillModifier; + + if (chance > Misc.getRandom().nextInt(randomChance) && !eventHandler.defender.isDead()) { + eventHandler.modifyEventDamage(); + eventHandler.sendAbilityMessages(); + } + } + } diff --git a/src/main/java/com/gmail/nossr50/skills/axes/Axes.java b/src/main/java/com/gmail/nossr50/skills/axes/Axes.java index b3b179a91..1e4c75ee0 100644 --- a/src/main/java/com/gmail/nossr50/skills/axes/Axes.java +++ b/src/main/java/com/gmail/nossr50/skills/axes/Axes.java @@ -1,88 +1,30 @@ package com.gmail.nossr50.skills.axes; import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.AnimalTamer; -import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; -import org.bukkit.entity.Tameable; import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import com.gmail.nossr50.config.AdvancedConfig; -import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.SkillType; import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users; public class Axes { - public static int maxBonusDamage = AdvancedConfig.getInstance().getBonusDamageAxesBonusMax(); - public static int maxBonusLevel = AdvancedConfig.getInstance().getBonusDamageAxesMaxBonusLevel(); + public static int bonusDamageMaxBonus = AdvancedConfig.getInstance().getBonusDamageAxesBonusMax(); + public static int bonusDamageMaxBonusLevel = AdvancedConfig.getInstance().getBonusDamageAxesMaxBonusLevel(); + + public static int criticalHitMaxBonusLevel = AdvancedConfig.getInstance().getAxesCriticalMaxBonusLevel(); + public static double criticalHitMaxChance = AdvancedConfig.getInstance().getAxesCriticalChance(); + public static double criticalHitPVPModifier = AdvancedConfig.getInstance().getAxesCriticalPVPModifier(); + public static double criticalHitPVEModifier = AdvancedConfig.getInstance().getAxesCriticalPVEModifier(); + static AdvancedConfig advancedConfig = AdvancedConfig.getInstance(); - /** - * Check for critical chances on axe damage. - * - * @param attacker The attacking player - * @param event The event to modify - */ - public static void axeCriticalCheck(Player attacker, EntityDamageByEntityEvent event) { - if (attacker == null) - return; - - Entity entity = event.getEntity(); - - if (entity instanceof Tameable) { - Tameable pet = (Tameable) entity; - - if (pet.isTamed()) { - AnimalTamer tamer = pet.getOwner(); - - if (tamer instanceof Player) { - Player owner = (Player) tamer; - - if (owner == attacker || PartyManager.getInstance().inSameParty(attacker, owner)) { - return; - } - } - } - } - - final int MAX_BONUS_LEVEL = advancedConfig.getAxesCriticalMaxBonusLevel(); - final double MAX_CHANCE = advancedConfig.getAxesCriticalChance(); - final double PVP_MODIFIER = advancedConfig.getAxesCriticalPVPModifier(); - final int PVE_MODIFIER = advancedConfig.getAxesCriticalPVEModifier(); - - PlayerProfile attackerProfile = Users.getProfile(attacker); - int skillLevel = attackerProfile.getSkillLevel(SkillType.AXES); - int skillCheck = Misc.skillCheck(skillLevel, MAX_BONUS_LEVEL); - - int randomChance = 100; - double chance = (MAX_CHANCE / MAX_BONUS_LEVEL) * skillCheck; - if (chance > MAX_CHANCE) chance = MAX_CHANCE; - - if (Permissions.luckyAxes(attacker)) { - randomChance = (int) (randomChance * 0.75); - } - - if (chance > Misc.getRandom().nextInt(randomChance) && !entity.isDead()) { - int damage = event.getDamage(); - - if (entity instanceof Player) { - event.setDamage((int) (damage * PVP_MODIFIER)); - ((Player) entity).sendMessage(LocaleLoader.getString("Axes.Combat.CritStruck")); - } - else { - event.setDamage(damage * PVE_MODIFIER); - } - attacker.sendMessage(LocaleLoader.getString("Axes.Combat.CriticalHit")); - } - } - /** * Check for Impact ability. * diff --git a/src/main/java/com/gmail/nossr50/skills/axes/CriticalHitEventHandler.java b/src/main/java/com/gmail/nossr50/skills/axes/CriticalHitEventHandler.java new file mode 100644 index 000000000..fe60d5ff3 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/skills/axes/CriticalHitEventHandler.java @@ -0,0 +1,47 @@ +package com.gmail.nossr50.skills.axes; + +import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntityDamageByEntityEvent; + +import com.gmail.nossr50.locale.LocaleLoader; +import com.gmail.nossr50.util.Misc; + +public class CriticalHitEventHandler { + private AxeManager manager; + private EntityDamageByEntityEvent event; + private int damage; + + protected Entity defender; + protected int skillModifier; + + public CriticalHitEventHandler(AxeManager manager, EntityDamageByEntityEvent event) { + this.manager = manager; + this.event = event; + this.defender = event.getEntity(); + this.damage = event.getDamage(); + + calculateSkillModifier(); + } + + protected void modifyEventDamage() { + if (defender instanceof Player) { + event.setDamage((int) (damage * Axes.criticalHitPVPModifier)); + } + else { + event.setDamage((int) (damage * Axes.criticalHitPVEModifier)); + } + } + + protected void sendAbilityMessages() { + manager.getPlayer().sendMessage(LocaleLoader.getString("Axes.Combat.CriticalHit")); + + if (defender instanceof Player) { + ((Player) defender).sendMessage(LocaleLoader.getString("Axes.Combat.CritStruck")); + } + } + + private void calculateSkillModifier() { + this.skillModifier = Misc.skillCheck(manager.getSkillLevel(), Axes.criticalHitMaxBonusLevel); + } +} diff --git a/src/main/java/com/gmail/nossr50/util/Combat.java b/src/main/java/com/gmail/nossr50/util/Combat.java index 1e944e6bc..156b98607 100644 --- a/src/main/java/com/gmail/nossr50/util/Combat.java +++ b/src/main/java/com/gmail/nossr50/util/Combat.java @@ -2,7 +2,6 @@ package com.gmail.nossr50.util; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; -import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.Animals; import org.bukkit.entity.Arrow; import org.bukkit.entity.Entity; @@ -100,13 +99,8 @@ public class Combat { Skills.abilityCheck(attacker, SkillType.AXES); AxeManager axeManager = new AxeManager(attacker); - if (Permissions.axeBonus(attacker)) { - axeManager.bonusDamage(event); - } - - if (Permissions.criticalHit(attacker)) { - Axes.axeCriticalCheck(attacker, event); - } + axeManager.bonusDamage(event); + axeManager.criticalHitCheck(event); if (Permissions.impact(attacker)) { Axes.impact(attacker, target, event); @@ -550,16 +544,8 @@ public class Combat { else if (entity instanceof Tameable) { Tameable pet = (Tameable) entity; - if (pet.isTamed()) { - AnimalTamer tamer = pet.getOwner(); - - if (tamer instanceof Player) { - Player owner = (Player) tamer; - - if (owner == player || PartyManager.getInstance().inSameParty(player, owner)) { - return false; - } - } + if (Misc.isFriendlyPet(player, pet)) { + return false; } } diff --git a/src/main/java/com/gmail/nossr50/util/Misc.java b/src/main/java/com/gmail/nossr50/util/Misc.java index 7bf7dfe02..c2080333d 100644 --- a/src/main/java/com/gmail/nossr50/util/Misc.java +++ b/src/main/java/com/gmail/nossr50/util/Misc.java @@ -5,9 +5,11 @@ import java.util.Random; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.Item; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.entity.Tameable; import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.PluginManager; @@ -18,6 +20,7 @@ import com.gmail.nossr50.events.fake.FakeBlockBreakEvent; import com.gmail.nossr50.events.fake.FakeBlockDamageEvent; import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; import com.gmail.nossr50.events.items.McMMOItemSpawnEvent; +import com.gmail.nossr50.party.PartyManager; public class Misc { private static Random random = new Random(); @@ -27,6 +30,22 @@ public class Misc { public static final int TIME_CONVERSION_FACTOR = 1000; public static final double SKILL_MESSAGE_MAX_SENDING_DISTANCE = 10.0; + public static boolean isFriendlyPet(Player attacker, Tameable pet) { + if (pet.isTamed()) { + AnimalTamer tamer = pet.getOwner(); + + if (tamer instanceof Player) { + Player owner = (Player) tamer; + + if (owner == attacker || PartyManager.getInstance().inSameParty(attacker, owner)) { + return true; + } + } + } + + return false; + } + public static boolean isNPC(Player player) { if (player == null || Users.getProfile(player) == null || player.hasMetadata("NPC")) { return true; diff --git a/src/main/resources/advanced.yml b/src/main/resources/advanced.yml index 320f06a33..b2a17f25c 100644 --- a/src/main/resources/advanced.yml +++ b/src/main/resources/advanced.yml @@ -47,8 +47,8 @@ Skills: # SkillShot_IncreasePercentage: This is a percentage value, 0.1 = 10% # SkillShot_MaxBonus: When the SkillShot_MaxBonus has been reached, the bonus percentage will not go up anymore. 2.0 = 200% SkillShot_IncreaseLevel: 50 - SkillShot_IncreasePercentage: 0.1D - SkillShot_MaxBonus: 2.0D + SkillShot_IncreasePercentage: 0.1 + SkillShot_MaxBonus: 2.0 # Daze_MaxChance: Maximum chance of causing daze to opponents # Daze_MaxBonusLevel: Maximum bonus level of Daze, when a player reaches this level his chance of causing a daze will be "Daze_MaxChance" @@ -77,7 +77,7 @@ Skills: # Damage modifier of critical hits for PVP / PVE, when causing a critical hit the damage gets multiplied by the modifier AxesCritical_PVP_Modifier: 1.5 - AxesCritical_PVE_Modifier: 2 + AxesCritical_PVE_Modifier: 2.0 # GreaterImpact_Chance: Chance of hitting with GreaterImpact, knocksbacks mobs # GreaterImpact_KnockbackModifier: Velocity modifier of GreaterImpact hits, this determines how great the knockback is