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,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;
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.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 static int environmentallyAwareUnlockLevel = AdvancedConfig.getInstance().getEnviromentallyAwareUnlock();
@ -25,4 +34,38 @@ public class Taming {
public static int wolfXp = Config.getInstance().getTamingXPWolf();
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.Wolf;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityTameEvent;
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.
*/
@ -177,57 +143,4 @@ public class TamingManager extends SkillManager {
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;
}
}
}