Pass less events.

This commit is contained in:
GJ 2013-02-26 09:41:47 -05:00
parent f5290c3256
commit ec8581a835
6 changed files with 80 additions and 137 deletions

View File

@ -38,10 +38,6 @@ public abstract class SkillManager {
return activationChance; return activationChance;
} }
public SkillType getSkill() {
return skill;
}
public void applyXpGain(int xp) { public void applyXpGain(int xp) {
mcMMOPlayer.beginXpGain(skill, xp); mcMMOPlayer.beginXpGain(skill, xp);
} }

View File

@ -5,11 +5,16 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import org.bukkit.Material; 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.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import com.gmail.nossr50.config.AdvancedConfig; 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.Misc;
import com.gmail.nossr50.util.Permissions;
public class Archery { public class Archery {
private static List<TrackedEntity> trackedEntities = new ArrayList<TrackedEntity>(); private static List<TrackedEntity> trackedEntities = new ArrayList<TrackedEntity>();
@ -27,6 +32,18 @@ public class Archery {
public static double distanceXpModifer = 0.025; 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) { protected static void incrementTrackerValue(LivingEntity livingEntity) {
for (TrackedEntity trackedEntity : trackedEntities) { for (TrackedEntity trackedEntity : trackedEntities) {
if (trackedEntity.getLivingEntity().getEntityId() == livingEntity.getEntityId()) { if (trackedEntity.getLivingEntity().getEntityId() == livingEntity.getEntityId()) {

View File

@ -3,21 +3,29 @@ package com.gmail.nossr50.skills.archery;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player; 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.datatypes.McMMOPlayer;
import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.skills.SkillManager;
import com.gmail.nossr50.skills.utilities.SkillTools;
import com.gmail.nossr50.skills.utilities.SkillType; import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Users;
public class ArcheryManager extends SkillManager { public class ArcheryManager extends SkillManager {
public ArcheryManager (McMMOPlayer mcMMOPlayer) { public ArcheryManager (McMMOPlayer mcMMOPlayer) {
super(mcMMOPlayer, SkillType.ARCHERY); 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) { public void distanceXpBonus(LivingEntity target) {
Player player = mcMMOPlayer.getPlayer(); Player player = getPlayer();
Location shooterLocation = player.getLocation(); Location shooterLocation = player.getLocation();
Location targetLocation = target.getLocation(); Location targetLocation = target.getLocation();
@ -25,61 +33,65 @@ public class ArcheryManager extends SkillManager {
return; return;
} }
double squaredDistance = shooterLocation.distanceSquared(targetLocation);
// Cap distance at 100^2 to prevent teleport exploit. // Cap distance at 100^2 to prevent teleport exploit.
// TODO: Better way to handle this would be great... // TODO: Better way to handle this would be great...
if (squaredDistance > 10000) { double squaredDistance = Math.min(shooterLocation.distanceSquared(targetLocation), 10000);
squaredDistance = 10000;
}
int bonusXp = (int) (squaredDistance * Archery.distanceXpModifer); applyXpGain((int) (squaredDistance * Archery.distanceXpModifer));
mcMMOPlayer.beginXpGain(SkillType.ARCHERY, bonusXp);
} }
/** /**
* Track arrows fired for later retrieval. * 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) { public void trackArrows(LivingEntity target) {
ArrowTrackingEventHandler eventHandler = new ArrowTrackingEventHandler(this, livingEntity); if (SkillTools.activationSuccessful(getPlayer(), skill, Archery.retrieveMaxChance, Archery.retrieveMaxBonusLevel)) {
Archery.incrementTrackerValue(target);
double chance = (Archery.retrieveMaxChance / Archery.retrieveMaxBonusLevel) * eventHandler.skillModifier;
if (chance > Misc.getRandom().nextInt(activationChance)) {
eventHandler.addToTracker();
} }
} }
/** /**
* Check for Daze. * Handle the effects of the Daze ability
* *
* @param defender Defending player * @param defender The player being affected by Daze
* @param event The event to modify * @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) { public int dazeCheck(Player defender, int damage) {
DazeEventHandler eventHandler = new DazeEventHandler(this, event, defender); 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)) { defender.teleport(dazedLocation);
eventHandler.handleDazeEffect(); defender.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 20 * 10, 10));
eventHandler.sendAbilityMessages();
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) { public int skillShotCheck(int damage) {
if (getSkillLevel() >= Archery.skillShotIncreaseLevel && Permissions.bonusDamage(mcMMOPlayer.getPlayer(), skill)) { double damageBonusPercent = Math.min(((getSkillLevel() / Archery.skillShotIncreaseLevel) * Archery.skillShotIncreasePercentage), Archery.skillShotMaxBonusPercentage);
SkillShotEventHandler eventHandler = new SkillShotEventHandler(this, event); int archeryBonus = (int) (damage * damageBonusPercent);
eventHandler.calculateDamageBonus(); return damage + archeryBonus;
eventHandler.modifyEventDamage();
}
} }
} }

View File

@ -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);
}
}

View File

@ -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"));
}
}
}

View File

@ -1,7 +1,6 @@
package com.gmail.nossr50.skills.utilities; package com.gmail.nossr50.skills.utilities;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Animals; import org.bukkit.entity.Animals;
import org.bukkit.entity.Arrow; 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.party.PartyManager;
import com.gmail.nossr50.skills.SkillManagerStore; import com.gmail.nossr50.skills.SkillManagerStore;
import com.gmail.nossr50.skills.acrobatics.Acrobatics; 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.BleedTimer;
import com.gmail.nossr50.skills.runnables.CombatXpGiver; import com.gmail.nossr50.skills.runnables.CombatXpGiver;
import com.gmail.nossr50.skills.swords.Swords; import com.gmail.nossr50.skills.swords.Swords;
@ -282,26 +282,26 @@ public final class CombatTools {
} }
if (Permissions.skillEnabled(shooter, SkillType.ARCHERY)) { if (Permissions.skillEnabled(shooter, SkillType.ARCHERY)) {
McMMOPlayer mcMMOPlayer = Users.getPlayer(shooter); String playerName = shooter.getName();
SkillManagerStore.getInstance().getArcheryManager(shooter.getName()).skillShot(event);
if (target instanceof Player) { if (Archery.canSkillShot(shooter)) {
if (SkillType.UNARMED.getPVPEnabled() && ((Player) target).getItemInHand().getType() == Material.AIR && Permissions.arrowDeflect((Player) target)) { event.setDamage(SkillManagerStore.getInstance().getArcheryManager(playerName).skillShotCheck(event.getDamage()));
SkillManagerStore.getInstance().getUnarmedManager(((Player) target).getName()).deflectCheck(event);
}
if (Permissions.daze(shooter)) {
SkillManagerStore.getInstance().getArcheryManager(shooter.getName()).dazeCheck((Player) target, event);
}
} }
if (!(shooter.getItemInHand().containsEnchantment(Enchantment.ARROW_INFINITE)) && Permissions.arrowRetrieval(shooter)) { if (target instanceof Player && SkillType.UNARMED.getPVPEnabled() && ((Player) target).getItemInHand().getType() == Material.AIR && Permissions.arrowDeflect((Player) target)) {
SkillManagerStore.getInstance().getArcheryManager(shooter.getName()).trackArrows(target); SkillManagerStore.getInstance().getUnarmedManager(((Player) target).getName()).deflectCheck(event);
} }
SkillManagerStore.getInstance().getArcheryManager(shooter.getName()).distanceXpBonus(target); if (Archery.canDaze(shooter, target)) {
startGainXp(mcMMOPlayer, target, SkillType.ARCHERY); 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);
} }
} }