From fbd016f86de81b8cbfa36896cd227bd9f7c71f6e Mon Sep 17 00:00:00 2001 From: GJ Date: Thu, 28 Feb 2013 11:52:56 -0500 Subject: [PATCH] Cleaning up our UnarmedManager --- .../skills/unarmed/DeflectEventHandler.java | 31 -------- .../skills/unarmed/DisarmEventHandler.java | 48 ------------ .../skills/unarmed/IronGripEventHandler.java | 30 -------- .../UnarmedBonusDamageEventHandler.java | 32 -------- .../skills/unarmed/UnarmedManager.java | 74 ++++++++----------- .../nossr50/skills/utilities/CombatTools.java | 16 +++- 6 files changed, 44 insertions(+), 187 deletions(-) delete mode 100644 src/main/java/com/gmail/nossr50/skills/unarmed/DeflectEventHandler.java delete mode 100644 src/main/java/com/gmail/nossr50/skills/unarmed/DisarmEventHandler.java delete mode 100644 src/main/java/com/gmail/nossr50/skills/unarmed/IronGripEventHandler.java delete mode 100644 src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedBonusDamageEventHandler.java diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/DeflectEventHandler.java b/src/main/java/com/gmail/nossr50/skills/unarmed/DeflectEventHandler.java deleted file mode 100644 index 2fadd16ac..000000000 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/DeflectEventHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.gmail.nossr50.skills.unarmed; - -import org.bukkit.event.entity.EntityDamageEvent; - -import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.skills.utilities.SkillTools; - -public class DeflectEventHandler { - private UnarmedManager manager; - private EntityDamageEvent event; - protected int skillModifier; - - protected DeflectEventHandler(UnarmedManager manager, EntityDamageEvent event) { - this.manager = manager; - this.event = event; - - calculateSkillModifier(); - } - - protected void calculateSkillModifier() { - this.skillModifier = SkillTools.skillCheck(manager.getSkillLevel(), Unarmed.deflectMaxBonusLevel); - } - - protected void sendAbilityMessage() { - manager.getMcMMOPlayer().getPlayer().sendMessage(LocaleLoader.getString("Combat.ArrowDeflect")); - } - - protected void cancelEvent() { - event.setCancelled(true); - } -} diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/DisarmEventHandler.java b/src/main/java/com/gmail/nossr50/skills/unarmed/DisarmEventHandler.java deleted file mode 100644 index 272a88e52..000000000 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/DisarmEventHandler.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.gmail.nossr50.skills.unarmed; - -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.inventory.ItemStack; - -import com.gmail.nossr50.mcMMO; -import com.gmail.nossr50.events.skills.McMMOPlayerDisarmEvent; -import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.skills.utilities.SkillTools; -import com.gmail.nossr50.util.Misc; - -public class DisarmEventHandler { - private UnarmedManager manager; - private Player defender; - private ItemStack inHand; - protected int skillModifier; - - protected DisarmEventHandler(UnarmedManager manager, Player defender) { - this.manager = manager; - this.defender = defender; - this.inHand = defender.getItemInHand(); - } - - protected boolean isHoldingItem() { - return (inHand.getType() != Material.AIR); - } - - protected void calculateSkillModifier() { - this.skillModifier = SkillTools.skillCheck(manager.getSkillLevel(), Unarmed.disarmMaxBonusLevel); - } - - private void sendAbilityMessage() { - defender.sendMessage(LocaleLoader.getString("Skills.Disarmed")); - } - - protected void handleDisarm() { - McMMOPlayerDisarmEvent event = new McMMOPlayerDisarmEvent(defender); - mcMMO.p.getServer().getPluginManager().callEvent(event); - - if(!event.isCancelled()) { - Misc.dropItem(defender.getLocation(), inHand); - defender.setItemInHand(new ItemStack(Material.AIR)); - - sendAbilityMessage(); - } - } -} diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/IronGripEventHandler.java b/src/main/java/com/gmail/nossr50/skills/unarmed/IronGripEventHandler.java deleted file mode 100644 index 554d189ab..000000000 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/IronGripEventHandler.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.gmail.nossr50.skills.unarmed; - -import org.bukkit.entity.Player; - -import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.skills.utilities.SkillTools; -import com.gmail.nossr50.skills.utilities.SkillType; -import com.gmail.nossr50.util.Users; - -public class IronGripEventHandler { - private UnarmedManager manager; - private Player defender; - protected int skillModifier; - - protected IronGripEventHandler(UnarmedManager manager, Player defender) { - this.manager = manager; - this.defender = defender; - - calculateSkillModifier(); - } - - protected void calculateSkillModifier() { - this.skillModifier = SkillTools.skillCheck(Users.getPlayer(defender).getProfile().getSkillLevel(SkillType.UNARMED), Unarmed.ironGripMaxBonusLevel); - } - - protected void sendAbilityMessages() { - defender.sendMessage(LocaleLoader.getString("Unarmed.Ability.IronGrip.Defender")); - manager.getMcMMOPlayer().getPlayer().sendMessage(LocaleLoader.getString("Unarmed.Ability.IronGrip.Attacker")); - } -} diff --git a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedBonusDamageEventHandler.java b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedBonusDamageEventHandler.java deleted file mode 100644 index 62fbe1611..000000000 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedBonusDamageEventHandler.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.gmail.nossr50.skills.unarmed; - -import org.bukkit.event.entity.EntityDamageEvent; - -public class UnarmedBonusDamageEventHandler { - private UnarmedManager manager; - private EntityDamageEvent event; - - protected int damageBonus; - - protected UnarmedBonusDamageEventHandler(UnarmedManager manager, EntityDamageEvent event) { - this.manager = manager; - this.event = event; - } - - protected void calculateDamageBonus() { - int damageBonus = 3 + (manager.getSkillLevel() / Unarmed.ironArmIncreaseLevel); - - if (damageBonus > Unarmed.ironArmMaxBonusDamage) { - damageBonus = Unarmed.ironArmMaxBonusDamage; - } - - this.damageBonus = damageBonus; - } - - protected void modifyEventDamage() { - int damage = event.getDamage(); - int unarmedBonus = damage + damageBonus; - - event.setDamage(damage + unarmedBonus); - } -} 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 2c2b6f8ce..7da7d5c13 100644 --- a/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java +++ b/src/main/java/com/gmail/nossr50/skills/unarmed/UnarmedManager.java @@ -1,12 +1,15 @@ package com.gmail.nossr50.skills.unarmed; -import org.bukkit.entity.LivingEntity; +import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.inventory.ItemStack; +import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.events.skills.McMMOPlayerDisarmEvent; +import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.skills.SkillManager; -import com.gmail.nossr50.skills.utilities.PerksUtils; +import com.gmail.nossr50.skills.utilities.SkillTools; import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Permissions; @@ -21,55 +24,48 @@ public class UnarmedManager extends SkillManager { * * @param defender The defending player */ - public void disarmCheck(LivingEntity defender) { - Player defendingPlayer = (Player) defender; - DisarmEventHandler eventHandler = new DisarmEventHandler(this, defendingPlayer); + public void disarmCheck(Player defender) { + if (SkillTools.activationSuccessful(getPlayer(), skill, Unarmed.disarmMaxChance, Unarmed.disarmMaxBonusLevel) && !hasIronGrip(defender)) { + McMMOPlayerDisarmEvent disarmEvent = new McMMOPlayerDisarmEvent(defender); + mcMMO.p.getServer().getPluginManager().callEvent(disarmEvent); - if (eventHandler.isHoldingItem()) { - eventHandler.calculateSkillModifier(); + if (!disarmEvent.isCancelled()) { + Misc.dropItem(defender.getLocation(), defender.getItemInHand()); - float chance = (float) ((Unarmed.disarmMaxChance / Unarmed.disarmMaxBonusLevel) * getSkillLevel()); - if (chance > Unarmed.disarmMaxChance) chance = (float) Unarmed.disarmMaxChance; - - if (chance > Misc.getRandom().nextInt(activationChance)) { - if (!hasIronGrip(defendingPlayer)) { - eventHandler.handleDisarm(); - } + defender.setItemInHand(new ItemStack(Material.AIR)); + defender.sendMessage(LocaleLoader.getString("Skills.Disarmed")); } } } /** * Check for arrow deflection. - * - * @param event The event to modify */ - public void deflectCheck(EntityDamageEvent event) { - DeflectEventHandler eventHandler = new DeflectEventHandler(this, event); + public boolean deflectCheck() { + Player player = getPlayer(); - float chance = (float) ((Unarmed.deflectMaxChance / Unarmed.deflectMaxBonusLevel) * getSkillLevel()); - if (chance > Unarmed.deflectMaxChance) chance = (float) Unarmed.deflectMaxChance; - - if (chance > Misc.getRandom().nextInt(activationChance)) { - eventHandler.cancelEvent(); - eventHandler.sendAbilityMessage(); + if (SkillTools.activationSuccessful(player, skill, Unarmed.deflectMaxChance, Unarmed.deflectMaxBonusLevel)) { + player.sendMessage(LocaleLoader.getString("Combat.ArrowDeflect")); + return true; } + + return false; } - public void berserkDamage(EntityDamageEvent event) { - event.setDamage((int) (event.getDamage() * Unarmed.berserkDamageModifier)); + public int berserkDamage(int damage) { + return (int) (damage * Unarmed.berserkDamageModifier); } /** - * Handle Unarmed bonus damage. + * Handle the effects of the Iron Arm ability * - * @param event The event to modify. + * @param damage The amount of damage initially dealt by the event + * @return the modified event damage */ - public void bonusDamage(EntityDamageEvent event) { - UnarmedBonusDamageEventHandler eventHandler = new UnarmedBonusDamageEventHandler(this, event); + public int ironArmCheck(int damage) { + int unarmedBonus = Math.min(3 + (getSkillLevel() / Unarmed.ironArmIncreaseLevel), Unarmed.ironArmMaxBonusDamage); - eventHandler.calculateDamageBonus(); - eventHandler.modifyEventDamage(); + return damage + unarmedBonus; } /** @@ -79,16 +75,10 @@ public class UnarmedManager extends SkillManager { * @return true if the defender was not disarmed, false otherwise */ private boolean hasIronGrip(Player defender) { - if (Misc.isNPCEntity(defender) || !Permissions.ironGrip(defender)) { - return false; - } + if (!Misc.isNPCEntity(defender) && Permissions.ironGrip(defender) && SkillTools.activationSuccessful(defender, skill, Unarmed.ironGripMaxChance, Unarmed.ironGripMaxBonusLevel)) { + defender.sendMessage(LocaleLoader.getString("Unarmed.Ability.IronGrip.Defender")); + getPlayer().sendMessage(LocaleLoader.getString("Unarmed.Ability.IronGrip.Attacker")); - IronGripEventHandler eventHandler = new IronGripEventHandler(this, defender); - - double chance = (Unarmed.ironGripMaxChance / Unarmed.ironGripMaxBonusLevel) * eventHandler.skillModifier; - - if (chance > Misc.getRandom().nextInt(PerksUtils.handleLuckyPerks(defender, skill))) { - eventHandler.sendAbilityMessages(); return true; } diff --git a/src/main/java/com/gmail/nossr50/skills/utilities/CombatTools.java b/src/main/java/com/gmail/nossr50/skills/utilities/CombatTools.java index de07ffdf6..e10efc94f 100644 --- a/src/main/java/com/gmail/nossr50/skills/utilities/CombatTools.java +++ b/src/main/java/com/gmail/nossr50/skills/utilities/CombatTools.java @@ -152,15 +152,19 @@ public final class CombatTools { } if (Permissions.bonusDamage(player, SkillType.UNARMED)) { - SkillManagerStore.getInstance().getUnarmedManager(playerName).bonusDamage(event); + event.setDamage(SkillManagerStore.getInstance().getUnarmedManager(playerName).ironArmCheck(event.getDamage())); } if (profile.getAbilityMode(AbilityType.BERSERK) && canBerserk) { - SkillManagerStore.getInstance().getUnarmedManager(playerName).berserkDamage(event); + event.setDamage(SkillManagerStore.getInstance().getUnarmedManager(playerName).berserkDamage(event.getDamage())); } if (target instanceof Player && Permissions.disarm(player)) { - SkillManagerStore.getInstance().getUnarmedManager(playerName).disarmCheck(target); + Player defender = (Player) target; + + if (defender.getItemInHand().getType() != Material.AIR) { + SkillManagerStore.getInstance().getUnarmedManager(playerName).disarmCheck((Player) target); + } } startGainXp(mcMMOPlayer, target, SkillType.UNARMED); @@ -283,7 +287,11 @@ public final class CombatTools { } if (target instanceof Player && SkillType.UNARMED.getPVPEnabled() && ((Player) target).getItemInHand().getType() == Material.AIR && Permissions.arrowDeflect((Player) target)) { - SkillManagerStore.getInstance().getUnarmedManager(((Player) target).getName()).deflectCheck(event); + event.setCancelled(SkillManagerStore.getInstance().getUnarmedManager(((Player) target).getName()).deflectCheck()); + + if (event.isCancelled()) { + return; + } } if (SkillManagerStore.getInstance().getArcheryManager(playerName).canDaze(target)) {