Move more event handling back into the main listener to avoid passing

the event itself.
This commit is contained in:
GJ 2013-02-22 17:55:15 -05:00
parent 6c6ab4c96e
commit 2fee9df625
10 changed files with 153 additions and 232 deletions

View File

@ -1,6 +1,5 @@
package com.gmail.nossr50.listeners; package com.gmail.nossr50.listeners;
import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.AnimalTamer; import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
@ -37,8 +36,10 @@ import com.gmail.nossr50.skills.acrobatics.Acrobatics;
import com.gmail.nossr50.skills.archery.Archery; import com.gmail.nossr50.skills.archery.Archery;
import com.gmail.nossr50.skills.fishing.Fishing; import com.gmail.nossr50.skills.fishing.Fishing;
import com.gmail.nossr50.skills.herbalism.Herbalism; import com.gmail.nossr50.skills.herbalism.Herbalism;
import com.gmail.nossr50.skills.mining.BlastMining;
import com.gmail.nossr50.skills.mining.MiningManager; import com.gmail.nossr50.skills.mining.MiningManager;
import com.gmail.nossr50.skills.runnables.BleedTimer; import com.gmail.nossr50.skills.runnables.BleedTimer;
import com.gmail.nossr50.skills.taming.Taming;
import com.gmail.nossr50.skills.taming.TamingManager; import com.gmail.nossr50.skills.taming.TamingManager;
import com.gmail.nossr50.skills.utilities.CombatTools; import com.gmail.nossr50.skills.utilities.CombatTools;
import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Misc;
@ -156,6 +157,10 @@ public class EntityListener implements Listener {
DamageCause cause = event.getCause(); DamageCause cause = event.getCause();
LivingEntity livingEntity = (LivingEntity) entity; LivingEntity livingEntity = (LivingEntity) entity;
if (CombatTools.isInvincible(livingEntity, event.getDamage())) {
return;
}
if (livingEntity instanceof Player) { if (livingEntity instanceof Player) {
Player player = (Player) entity; Player player = (Player) entity;
@ -173,31 +178,92 @@ public class EntityListener implements Listener {
return; return;
} }
if (!CombatTools.isInvincible(player, event.getDamage())) { switch (cause) {
if (cause == DamageCause.FALL && player.getItemInHand().getType() != Material.ENDER_PEARL && !(Acrobatics.afkLevelingDisabled && player.isInsideVehicle()) && Permissions.roll(player)) { case FALL:
if (Acrobatics.canRoll(player)) {
event.setDamage(Acrobatics.processRoll(player, event.getDamage())); event.setDamage(Acrobatics.processRoll(player, event.getDamage()));
if (event.getDamage() == 0) { if (event.getDamage() == 0) {
event.setCancelled(true); event.setCancelled(true);
return;
} }
} }
else if (cause == DamageCause.BLOCK_EXPLOSION && Permissions.demolitionsExpertise(player)) { break;
MiningManager miningManager = new MiningManager(mcMMOPlayer);
miningManager.demolitionsExpertise(event); case BLOCK_EXPLOSION:
if (Permissions.demolitionsExpertise(player)) {
event.setDamage(BlastMining.processDemolitionsExpertise(player, event.getDamage()));
if (event.getDamage() == 0) {
event.setCancelled(true);
return;
}
}
break;
default:
break;
} }
if (event.getDamage() >= 1) { if (event.getDamage() >= 1) {
profile.actualizeRecentlyHurt(); profile.actualizeRecentlyHurt();
} }
} }
}
else if (livingEntity instanceof Tameable) { else if (livingEntity instanceof Tameable) {
Tameable pet = (Tameable) livingEntity; Tameable pet = (Tameable) livingEntity;
AnimalTamer owner = pet.getOwner(); AnimalTamer owner = pet.getOwner();
if ((!CombatTools.isInvincible(livingEntity, event.getDamage())) && pet.isTamed() && owner instanceof Player && pet instanceof Wolf) { if (Taming.canPreventDamage(pet, owner)) {
TamingManager tamingManager = new TamingManager(Users.getPlayer((Player) owner)); Player player = (Player) owner;
tamingManager.preventDamage(event); Wolf wolf = (Wolf) pet;
switch (cause) {
case CONTACT:
case LAVA:
case FIRE:
if (Taming.canUseEnvironmentallyAware(player)) {
Taming.processEnvironmentallyAware(player, wolf, event.getDamage());
}
return;
case FALL:
if (Taming.canUseEnvironmentallyAware(player)) {
event.setCancelled(true);
}
return;
case ENTITY_ATTACK:
case PROJECTILE:
if (Taming.canUseThickFur(player)) {
event.setDamage(Taming.processThickFur(event.getDamage()));
if (event.getDamage() == 0) {
event.setCancelled(true);
}
}
return;
case FIRE_TICK:
if (Taming.canUseThickFur(player)) {
wolf.setFireTicks(0);
}
return;
case BLOCK_EXPLOSION:
case ENTITY_EXPLOSION:
case LIGHTNING:
if (Taming.canUseShockProof(player)) {
event.setDamage(Taming.processShockProof(event.getDamage()));
if (event.getDamage() == 0) {
event.setCancelled(true);
}
}
return;
default:
return;
}
} }
} }
} }

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.skills.acrobatics; package com.gmail.nossr50.skills.acrobatics;
import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.AdvancedConfig;
@ -33,6 +34,10 @@ public final class Acrobatics {
private Acrobatics() {}; private Acrobatics() {};
public static boolean canRoll(Player player) {
return (player.getItemInHand().getType() != Material.ENDER_PEARL) && !(afkLevelingDisabled && player.isInsideVehicle()) && Permissions.roll(player);
}
public static int processRoll(Player player, int damage) { public static int processRoll(Player player, int damage) {
if (player.isSneaking() && Permissions.gracefulRoll(player)) { if (player.isSneaking() && Permissions.gracefulRoll(player)) {
return processGracefulRoll(player, damage); return processGracefulRoll(player, damage);

View File

@ -1,7 +1,13 @@
package com.gmail.nossr50.skills.mining; package com.gmail.nossr50.skills.mining;
import org.bukkit.entity.Player;
import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.skills.utilities.SkillTools;
import com.gmail.nossr50.skills.utilities.SkillType;
import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.Users;
public class BlastMining { public class BlastMining {
public static int rank1 = AdvancedConfig.getInstance().getBlastMiningRank1(); public static int rank1 = AdvancedConfig.getInstance().getBlastMiningRank1();
@ -16,4 +22,25 @@ public class BlastMining {
public static int detonatorID = Config.getInstance().getDetonatorItemID(); public static int detonatorID = Config.getInstance().getDetonatorItemID();
public final static int MAXIMUM_REMOTE_DETONATION_DISTANCE = 100; public final static int MAXIMUM_REMOTE_DETONATION_DISTANCE = 100;
public static boolean canUseDemolitionsExpertise(Player player) {
return SkillTools.unlockLevelReached(player, SkillType.MINING, rank4) && Permissions.demolitionsExpertise(player);
}
public static int processDemolitionsExpertise(Player player, int damage) {
int skillLevel = Users.getPlayer(player).getProfile().getSkillLevel(SkillType.MINING);
int modifiedDamage;
if (skillLevel >= BlastMining.rank8) {
modifiedDamage = 0;
}
else if (skillLevel >= BlastMining.rank6) {
modifiedDamage = damage / 4;
}
else {
modifiedDamage = damage / 2;
}
return modifiedDamage;
}
} }

View File

@ -1,37 +0,0 @@
package com.gmail.nossr50.skills.mining;
import org.bukkit.event.entity.EntityDamageEvent;
public class DemoltionsExpertiseEventHandler {
private int skillLevel;
private EntityDamageEvent event;
private int damage;
private double damageModifier;
public DemoltionsExpertiseEventHandler(MiningManager manager, EntityDamageEvent event) {
this.skillLevel = manager.getSkillLevel();
this.event = event;
this.damage = event.getDamage();
}
protected void calculateDamageModifier() {
if (skillLevel >= BlastMining.rank8) {
damageModifier = 0.0;
}
else if (skillLevel >= BlastMining.rank6) {
damageModifier = 0.25;
}
else if (skillLevel >= BlastMining.rank4) {
damageModifier = 0.5;
}
else {
damageModifier = 1.0;
}
}
protected void modifyEventDamage() {
damage = (int) (damage * damageModifier);
event.setDamage(damage);
}
}

View File

@ -1,7 +1,6 @@
package com.gmail.nossr50.skills.mining; package com.gmail.nossr50.skills.mining;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent; import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
@ -73,18 +72,6 @@ public class MiningManager extends SkillManager{
eventHandler.processXPGain(); eventHandler.processXPGain();
} }
/**
* Decreases damage dealt by the explosion from TNT activated by Blast Mining.
*
* @param event Event whose explosion damage is being reduced
*/
public void demolitionsExpertise(EntityDamageEvent event) {
DemoltionsExpertiseEventHandler eventHandler = new DemoltionsExpertiseEventHandler(this, event);
eventHandler.calculateDamageModifier();
eventHandler.modifyEventDamage();
}
/** /**
* Increases the blast radius of the explosion. * Increases the blast radius of the explosion.
* *

View File

@ -1,35 +0,0 @@
package com.gmail.nossr50.skills.taming;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;
import org.bukkit.event.entity.EntityDamageEvent;
import com.gmail.nossr50.locale.LocaleLoader;
public class EnvironmentallyAwareEventHandler {
private Player player;
private EntityDamageEvent event;
private Wolf wolf;
protected EnvironmentallyAwareEventHandler(TamingManager manager, EntityDamageEvent event) {
this.player = manager.getMcMMOPlayer().getPlayer();
this.event = event;
this.wolf = (Wolf) event.getEntity();
}
protected void teleportWolf() {
if (event.getDamage() > wolf.getHealth()) {
return;
}
wolf.teleport(player.getLocation());
}
protected void sendAbilityMessage() {
player.sendMessage(LocaleLoader.getString("Taming.Listener.Wolf"));
}
protected void cancelEvent() {
event.setCancelled(true);
}
}

View File

@ -1,15 +0,0 @@
package com.gmail.nossr50.skills.taming;
import org.bukkit.event.entity.EntityDamageEvent;
public class ShockProofEventHandler {
private EntityDamageEvent event;
protected ShockProofEventHandler (EntityDamageEvent event) {
this.event = event;
}
protected void modifyEventDamage() {
event.setDamage(event.getDamage() / Taming.shockProofModifier);
}
}

View File

@ -1,7 +1,16 @@
package com.gmail.nossr50.skills.taming; package com.gmail.nossr50.skills.taming;
import org.bukkit.entity.AnimalTamer;
import org.bukkit.entity.Player;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.Wolf;
import com.gmail.nossr50.config.AdvancedConfig; import com.gmail.nossr50.config.AdvancedConfig;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
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.Permissions;
public class Taming { public class Taming {
public static int environmentallyAwareUnlockLevel = AdvancedConfig.getInstance().getEnviromentallyAwareUnlock(); public static int environmentallyAwareUnlockLevel = AdvancedConfig.getInstance().getEnviromentallyAwareUnlock();
@ -25,4 +34,38 @@ public class Taming {
public static int wolfXp = Config.getInstance().getTamingXPWolf(); public static int wolfXp = Config.getInstance().getTamingXPWolf();
public static int ocelotXp = Config.getInstance().getTamingXPOcelot(); public static int ocelotXp = Config.getInstance().getTamingXPOcelot();
public static boolean canPreventDamage(Tameable pet, AnimalTamer owner) {
return pet.isTamed() && owner instanceof Player && pet instanceof Wolf;
}
public static boolean canUseThickFur(Player player) {
return SkillTools.unlockLevelReached(player, SkillType.TAMING, thickFurUnlockLevel) && Permissions.thickFur(player);
}
public static boolean canUseEnvironmentallyAware(Player player) {
return SkillTools.unlockLevelReached(player, SkillType.TAMING, environmentallyAwareUnlockLevel) && Permissions.environmentallyAware(player);
}
public static boolean canUseShockProof(Player player) {
return SkillTools.unlockLevelReached(player, SkillType.TAMING, shockProofUnlockLevel) && Permissions.shockProof(player);
}
public static int processThickFur(int damage) {
return damage / thickFurModifier;
}
public static void processEnvironmentallyAware(Player player, Wolf wolf, int damage) {
if (damage > wolf.getHealth()) {
return;
}
wolf.teleport(player);
player.sendMessage(LocaleLoader.getString("Taming.Listener.Wolf"));
}
public static int processShockProof(int damage) {
return damage / shockProofModifier;
}
} }

View File

@ -4,7 +4,6 @@ import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity; import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Wolf; import org.bukkit.entity.Wolf;
import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityTameEvent; import org.bukkit.event.entity.EntityTameEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
@ -90,39 +89,6 @@ public class TamingManager extends SkillManager {
} }
} }
/**
* Prevent damage to wolves based on various skills.
*
* @param event The event to modify
*/
public void preventDamage(EntityDamageEvent event) {
DamageCause cause = event.getCause();
switch (cause) {
case CONTACT:
case LAVA:
case FIRE:
case FALL:
environmentallyAware(event, cause);
break;
case ENTITY_ATTACK:
case FIRE_TICK:
case PROJECTILE:
thickFur(event, cause);
break;
case BLOCK_EXPLOSION:
case ENTITY_EXPLOSION:
case LIGHTNING:
shockProof(event);
break;
default:
break;
}
}
/** /**
* Summon an ocelot to your side. * Summon an ocelot to your side.
*/ */
@ -177,57 +143,4 @@ public class TamingManager extends SkillManager {
eventHandler.sendSuccessMessage(); eventHandler.sendSuccessMessage();
} }
} }
/**
* Handle the Environmentally Aware ability.
*
* @param event The event to modify
* @param cause The damage cause of the event
*/
private void environmentallyAware(EntityDamageEvent event, DamageCause cause) {
if (skillLevel >= Taming.environmentallyAwareUnlockLevel && Permissions.environmentallyAware(mcMMOPlayer.getPlayer())) {
EnvironmentallyAwareEventHandler eventHandler = new EnvironmentallyAwareEventHandler(this, event);
switch (cause) {
case CONTACT:
case FIRE:
case LAVA:
eventHandler.teleportWolf();
eventHandler.sendAbilityMessage();
break;
case FALL:
eventHandler.cancelEvent();
break;
default:
break;
}
}
}
/**
* Handle the Thick Fur ability.
*
* @param event The event to modify
* @param cause The damage cause of the event
*/
private void thickFur(EntityDamageEvent event, DamageCause cause) {
if (skillLevel >= Taming.thickFurUnlockLevel && Permissions.thickFur(mcMMOPlayer.getPlayer())) {
ThickFurEventHandler eventHandler = new ThickFurEventHandler(event, cause);
eventHandler.modifyEventDamage();
}
}
/**
* Handle the Shock Proof ability.
*
* @param event The event to modify
*/
private void shockProof(EntityDamageEvent event) {
if (skillLevel >= Taming.shockProofUnlockLevel && Permissions.shockProof(mcMMOPlayer.getPlayer())) {
ShockProofEventHandler eventHandler = new ShockProofEventHandler(event);
eventHandler.modifyEventDamage();
}
}
} }

View File

@ -1,33 +0,0 @@
package com.gmail.nossr50.skills.taming;
import org.bukkit.entity.Wolf;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
public class ThickFurEventHandler {
private DamageCause cause;
private EntityDamageEvent event;
private Wolf wolf;
protected ThickFurEventHandler (EntityDamageEvent event, DamageCause cause) {
this.cause = cause;
this.event = event;
this.wolf = (Wolf) event.getEntity();
}
protected void modifyEventDamage() {
switch (cause) {
case FIRE_TICK:
wolf.setFireTicks(0);
break;
case ENTITY_ATTACK:
case PROJECTILE:
event.setDamage(event.getDamage() / Taming.thickFurModifier);
break;
default:
break;
}
}
}