From ec8581a8353fc4cb9463950581364facf739659d Mon Sep 17 00:00:00 2001 From: GJ Date: Tue, 26 Feb 2013 09:41:47 -0500 Subject: [PATCH] Pass less events. --- .../gmail/nossr50/skills/SkillManager.java | 4 - .../gmail/nossr50/skills/archery/Archery.java | 17 ++++ .../skills/archery/ArcheryManager.java | 82 +++++++++++-------- .../archery/ArrowTrackingEventHandler.java | 27 ------ .../skills/archery/DazeEventHandler.java | 55 ------------- .../nossr50/skills/utilities/CombatTools.java | 32 ++++---- 6 files changed, 80 insertions(+), 137 deletions(-) delete mode 100644 src/main/java/com/gmail/nossr50/skills/archery/ArrowTrackingEventHandler.java delete mode 100644 src/main/java/com/gmail/nossr50/skills/archery/DazeEventHandler.java diff --git a/src/main/java/com/gmail/nossr50/skills/SkillManager.java b/src/main/java/com/gmail/nossr50/skills/SkillManager.java index 84ddc8c09..612b7a2dd 100644 --- a/src/main/java/com/gmail/nossr50/skills/SkillManager.java +++ b/src/main/java/com/gmail/nossr50/skills/SkillManager.java @@ -38,10 +38,6 @@ public abstract class SkillManager { return activationChance; } - public SkillType getSkill() { - return skill; - } - public void applyXpGain(int xp) { mcMMOPlayer.beginXpGain(skill, xp); } diff --git a/src/main/java/com/gmail/nossr50/skills/archery/Archery.java b/src/main/java/com/gmail/nossr50/skills/archery/Archery.java index df000e1d8..ce393d1ca 100644 --- a/src/main/java/com/gmail/nossr50/skills/archery/Archery.java +++ b/src/main/java/com/gmail/nossr50/skills/archery/Archery.java @@ -5,11 +5,16 @@ import java.util.Iterator; import java.util.List; 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.config.AdvancedConfig; +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; public class Archery { private static List trackedEntities = new ArrayList(); @@ -27,6 +32,18 @@ public class Archery { public static double distanceXpModifer = 0.025; + public static boolean canDaze(Player player, LivingEntity target) { + return target instanceof Player && Permissions.daze(player); + } + + public static boolean canSkillShot(Player player) { + return SkillTools.unlockLevelReached(player, SkillType.ARCHERY, skillShotIncreaseLevel) && Permissions.bonusDamage(player, SkillType.ARCHERY); + } + + public static boolean canTrackArrows(Player player) { + return !(player.getItemInHand().containsEnchantment(Enchantment.ARROW_INFINITE)) && Permissions.arrowRetrieval(player); + } + protected static void incrementTrackerValue(LivingEntity livingEntity) { for (TrackedEntity trackedEntity : trackedEntities) { if (trackedEntity.getLivingEntity().getEntityId() == livingEntity.getEntityId()) { 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 c5503a1fa..38820c456 100644 --- a/src/main/java/com/gmail/nossr50/skills/archery/ArcheryManager.java +++ b/src/main/java/com/gmail/nossr50/skills/archery/ArcheryManager.java @@ -3,21 +3,29 @@ package com.gmail.nossr50.skills.archery; import org.bukkit.Location; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; -import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; import com.gmail.nossr50.datatypes.McMMOPlayer; +import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.skills.SkillManager; +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; +import com.gmail.nossr50.util.Users; public class ArcheryManager extends SkillManager { public ArcheryManager (McMMOPlayer mcMMOPlayer) { super(mcMMOPlayer, SkillType.ARCHERY); } + /** + * Calculate bonus XP awarded for Archery when hitting a far-away target. + * + * @param target The {@link LivingEntity} damaged by the arrow + */ public void distanceXpBonus(LivingEntity target) { - Player player = mcMMOPlayer.getPlayer(); + Player player = getPlayer(); Location shooterLocation = player.getLocation(); Location targetLocation = target.getLocation(); @@ -25,61 +33,65 @@ public class ArcheryManager extends SkillManager { return; } - double squaredDistance = shooterLocation.distanceSquared(targetLocation); - // Cap distance at 100^2 to prevent teleport exploit. // TODO: Better way to handle this would be great... - if (squaredDistance > 10000) { - squaredDistance = 10000; - } + double squaredDistance = Math.min(shooterLocation.distanceSquared(targetLocation), 10000); - int bonusXp = (int) (squaredDistance * Archery.distanceXpModifer); - mcMMOPlayer.beginXpGain(SkillType.ARCHERY, bonusXp); + applyXpGain((int) (squaredDistance * Archery.distanceXpModifer)); } /** * Track arrows fired for later retrieval. * - * @param livingEntity Entity damaged by the arrow + * @param target The {@link LivingEntity} damaged by the arrow */ - public void trackArrows(LivingEntity livingEntity) { - ArrowTrackingEventHandler eventHandler = new ArrowTrackingEventHandler(this, livingEntity); - - double chance = (Archery.retrieveMaxChance / Archery.retrieveMaxBonusLevel) * eventHandler.skillModifier; - - if (chance > Misc.getRandom().nextInt(activationChance)) { - eventHandler.addToTracker(); + public void trackArrows(LivingEntity target) { + if (SkillTools.activationSuccessful(getPlayer(), skill, Archery.retrieveMaxChance, Archery.retrieveMaxBonusLevel)) { + Archery.incrementTrackerValue(target); } } /** - * Check for Daze. + * Handle the effects of the Daze ability * - * @param defender Defending player - * @param event The event to modify + * @param defender The player being affected by Daze + * @param damage The amount of damage initially dealt by the event + * @return the modified event damage if the dodge was successful, the original event damage otherwise */ - public void dazeCheck(Player defender, EntityDamageEvent event) { - DazeEventHandler eventHandler = new DazeEventHandler(this, event, defender); + public int dazeCheck(Player defender, int damage) { + Player attacker = getPlayer(); - double chance = (Archery.dazeMaxBonus / Archery.dazeMaxBonusLevel) * eventHandler.skillModifier; + if (SkillTools.activationSuccessful(attacker, skill, Archery.dazeMaxBonus, Archery.dazeMaxBonusLevel)) { + Location dazedLocation = defender.getLocation(); + dazedLocation.setPitch(90 - Misc.getRandom().nextInt(181)); - if (chance > Misc.getRandom().nextInt(activationChance)) { - eventHandler.handleDazeEffect(); - eventHandler.sendAbilityMessages(); + defender.teleport(dazedLocation); + defender.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 20 * 10, 10)); + + if (Users.getPlayer(defender).getProfile().useChatNotifications()) { + defender.sendMessage(LocaleLoader.getString("Combat.TouchedFuzzy")); + } + + if (getProfile().useChatNotifications()) { + attacker.sendMessage(LocaleLoader.getString("Combat.TargetDazed")); + } + + return damage + Archery.dazeModifier; } + + return damage; } /** - * Handle archery bonus damage. + * Handle the effects of the Skill Shot 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 skillShot(EntityDamageEvent event) { - if (getSkillLevel() >= Archery.skillShotIncreaseLevel && Permissions.bonusDamage(mcMMOPlayer.getPlayer(), skill)) { - SkillShotEventHandler eventHandler = new SkillShotEventHandler(this, event); + public int skillShotCheck(int damage) { + double damageBonusPercent = Math.min(((getSkillLevel() / Archery.skillShotIncreaseLevel) * Archery.skillShotIncreasePercentage), Archery.skillShotMaxBonusPercentage); + int archeryBonus = (int) (damage * damageBonusPercent); - eventHandler.calculateDamageBonus(); - eventHandler.modifyEventDamage(); - } + return damage + archeryBonus; } } diff --git a/src/main/java/com/gmail/nossr50/skills/archery/ArrowTrackingEventHandler.java b/src/main/java/com/gmail/nossr50/skills/archery/ArrowTrackingEventHandler.java deleted file mode 100644 index 6b662c62d..000000000 --- a/src/main/java/com/gmail/nossr50/skills/archery/ArrowTrackingEventHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.gmail.nossr50.skills.archery; - -import org.bukkit.entity.LivingEntity; - -import com.gmail.nossr50.skills.utilities.SkillTools; - -public class ArrowTrackingEventHandler { - private ArcheryManager manager; - private LivingEntity entity; - - protected int skillModifier; - - protected ArrowTrackingEventHandler (ArcheryManager manager, LivingEntity entity) { - this.manager = manager; - this.entity = entity; - - calculateSkillModifier(); - } - - protected void calculateSkillModifier() { - this.skillModifier = SkillTools.skillCheck(manager.getSkillLevel(), Archery.retrieveMaxBonusLevel); - } - - protected void addToTracker() { - Archery.incrementTrackerValue(entity); - } -} diff --git a/src/main/java/com/gmail/nossr50/skills/archery/DazeEventHandler.java b/src/main/java/com/gmail/nossr50/skills/archery/DazeEventHandler.java deleted file mode 100644 index 6d12652ab..000000000 --- a/src/main/java/com/gmail/nossr50/skills/archery/DazeEventHandler.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.gmail.nossr50.skills.archery; - -import org.bukkit.Location; -import org.bukkit.entity.Player; -import org.bukkit.event.entity.EntityDamageEvent; -import org.bukkit.potion.PotionEffect; -import org.bukkit.potion.PotionEffectType; - -import com.gmail.nossr50.datatypes.McMMOPlayer; -import com.gmail.nossr50.locale.LocaleLoader; -import com.gmail.nossr50.skills.utilities.SkillTools; -import com.gmail.nossr50.util.Misc; -import com.gmail.nossr50.util.Users; - -public class DazeEventHandler { - private ArcheryManager manager; - private McMMOPlayer mcMMOPlayer; - private EntityDamageEvent event; - private Player defender; - - protected int skillModifier; - - protected DazeEventHandler (ArcheryManager manager, EntityDamageEvent event, Player defender) { - this.manager = manager; - this.mcMMOPlayer = manager.getMcMMOPlayer(); - this.event = event; - this.defender = defender; - - calculateSkillModifier(); - } - - protected void calculateSkillModifier() { - this.skillModifier = SkillTools.skillCheck(manager.getSkillLevel(), Archery.dazeMaxBonusLevel); - } - - protected void handleDazeEffect() { - Location location = defender.getLocation(); - location.setPitch(90 - Misc.getRandom().nextInt(181)); - - defender.teleport(location); - defender.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 20 * 10, 10)); - - event.setDamage(event.getDamage() + Archery.dazeModifier); - } - - protected void sendAbilityMessages() { - if (Users.getPlayer(defender).getProfile().useChatNotifications()) { - defender.sendMessage(LocaleLoader.getString("Combat.TouchedFuzzy")); - } - - if (mcMMOPlayer.getProfile().useChatNotifications()) { - mcMMOPlayer.getPlayer().sendMessage(LocaleLoader.getString("Combat.TargetDazed")); - } - } -} 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 d7566b853..7d1199fd2 100644 --- a/src/main/java/com/gmail/nossr50/skills/utilities/CombatTools.java +++ b/src/main/java/com/gmail/nossr50/skills/utilities/CombatTools.java @@ -1,7 +1,6 @@ package com.gmail.nossr50.skills.utilities; import org.bukkit.Material; -import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.Animals; import org.bukkit.entity.Arrow; @@ -30,6 +29,7 @@ import com.gmail.nossr50.mods.ModChecks; import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.skills.SkillManagerStore; import com.gmail.nossr50.skills.acrobatics.Acrobatics; +import com.gmail.nossr50.skills.archery.Archery; import com.gmail.nossr50.skills.runnables.BleedTimer; import com.gmail.nossr50.skills.runnables.CombatXpGiver; import com.gmail.nossr50.skills.swords.Swords; @@ -282,26 +282,26 @@ public final class CombatTools { } if (Permissions.skillEnabled(shooter, SkillType.ARCHERY)) { - McMMOPlayer mcMMOPlayer = Users.getPlayer(shooter); - SkillManagerStore.getInstance().getArcheryManager(shooter.getName()).skillShot(event); + String playerName = shooter.getName(); - if (target instanceof Player) { - if (SkillType.UNARMED.getPVPEnabled() && ((Player) target).getItemInHand().getType() == Material.AIR && Permissions.arrowDeflect((Player) target)) { - SkillManagerStore.getInstance().getUnarmedManager(((Player) target).getName()).deflectCheck(event); - } - - - if (Permissions.daze(shooter)) { - SkillManagerStore.getInstance().getArcheryManager(shooter.getName()).dazeCheck((Player) target, event); - } + if (Archery.canSkillShot(shooter)) { + event.setDamage(SkillManagerStore.getInstance().getArcheryManager(playerName).skillShotCheck(event.getDamage())); } - if (!(shooter.getItemInHand().containsEnchantment(Enchantment.ARROW_INFINITE)) && Permissions.arrowRetrieval(shooter)) { - SkillManagerStore.getInstance().getArcheryManager(shooter.getName()).trackArrows(target); + 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); } - SkillManagerStore.getInstance().getArcheryManager(shooter.getName()).distanceXpBonus(target); - startGainXp(mcMMOPlayer, target, SkillType.ARCHERY); + if (Archery.canDaze(shooter, target)) { + event.setDamage(SkillManagerStore.getInstance().getArcheryManager(playerName).dazeCheck((Player) target, event.getDamage())); + } + + if (Archery.canTrackArrows(shooter)) { + SkillManagerStore.getInstance().getArcheryManager(playerName).trackArrows(target); + } + + SkillManagerStore.getInstance().getArcheryManager(playerName).distanceXpBonus(target); + startGainXp(Users.getPlayer(shooter), target, SkillType.ARCHERY); } }