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;
}
public SkillType getSkill() {
return skill;
}
public void applyXpGain(int xp) {
mcMMOPlayer.beginXpGain(skill, xp);
}

View File

@ -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<TrackedEntity> trackedEntities = new ArrayList<TrackedEntity>();
@ -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()) {

View File

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

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