From 1b461ac96a28807ee885d27ee0f0c3393b641c90 Mon Sep 17 00:00:00 2001 From: GJ Date: Sun, 3 Mar 2013 15:42:56 -0500 Subject: [PATCH] Cleaning up more combat checks. --- .../nossr50/listeners/EntityListener.java | 8 ++-- .../nossr50/util/skills/CombatUtils.java | 37 ++++++++++--------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java index 6538157e5..211348a32 100644 --- a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java @@ -96,6 +96,10 @@ public class EntityListener implements Listener { Entity attacker = event.getDamager(); LivingEntity target = (LivingEntity) defender; + if (CombatUtils.isInvincible(target, event.getDamage())) { + return; + } + if (attacker instanceof Projectile) { attacker = ((Projectile) attacker).getShooter(); } @@ -107,10 +111,6 @@ public class EntityListener implements Listener { } } - if (CombatUtils.isInvincible(target, event.getDamage())) { - return; - } - if (defender instanceof Player && attacker instanceof Player) { Player defendingPlayer = (Player) defender; Player attackingPlayer = (Player) attacker; diff --git a/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java b/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java index bfef02099..f1cedbfaf 100644 --- a/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java +++ b/src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java @@ -51,8 +51,6 @@ public final class CombatUtils { * @param event The event to run the combat checks on. */ public static void combatChecks(EntityDamageByEntityEvent event, Entity attacker, LivingEntity target) { - boolean targetIsPlayer = (target.getType() == EntityType.PLAYER); - boolean targetIsTamedPet = (target instanceof Tameable) ? ((Tameable) target).isTamed() : false; Entity damager = event.getDamager(); if (attacker instanceof Player && damager.getType() == EntityType.PLAYER) { @@ -77,7 +75,7 @@ public final class CombatUtils { } if (ItemUtils.isSword(heldItem)) { - if (((targetIsPlayer || targetIsTamedPet) && !SkillType.SWORDS.getPVPEnabled()) || (!targetIsPlayer && !targetIsTamedPet && !SkillType.SWORDS.getPVEEnabled())) { + if (!shouldProcessSkill(target, SkillType.SWORDS)) { return; } @@ -100,7 +98,7 @@ public final class CombatUtils { } } else if (ItemUtils.isAxe(heldItem)) { - if (((targetIsPlayer || targetIsTamedPet) && !SkillType.AXES.getPVPEnabled()) || (!targetIsPlayer && !targetIsTamedPet && !SkillType.AXES.getPVEEnabled())) { + if (!shouldProcessSkill(target, SkillType.AXES)) { return; } @@ -134,7 +132,7 @@ public final class CombatUtils { } } else if (heldItem.getType() == Material.AIR) { - if (((targetIsPlayer || targetIsTamedPet) && !SkillType.UNARMED.getPVPEnabled()) || (!targetIsPlayer && !targetIsTamedPet && !SkillType.UNARMED.getPVEEnabled())) { + if (!shouldProcessSkill(target, SkillType.UNARMED)) { return; } @@ -180,12 +178,7 @@ public final class CombatUtils { return; } - if (targetIsPlayer || targetIsTamedPet) { - if (!SkillType.TAMING.getPVPEnabled()) { - return; - } - } - else if (!SkillType.TAMING.getPVEEnabled()) { + if (!shouldProcessSkill(target, SkillType.TAMING)) { return; } @@ -220,12 +213,7 @@ public final class CombatUtils { break; } - if (targetIsPlayer || targetIsTamedPet) { - if (!SkillType.ARCHERY.getPVPEnabled()) { - return; - } - } - else if (!SkillType.ARCHERY.getPVEEnabled()) { + if (!shouldProcessSkill(target, SkillType.ARCHERY)) { return; } @@ -236,7 +224,7 @@ public final class CombatUtils { break; } - if (targetIsPlayer) { + if (target instanceof Player) { Player player = (Player) target; if (Misc.isNPCEntity(player)) { @@ -593,4 +581,17 @@ public final class CombatUtils { return false; } + + private static boolean shouldProcessSkill(LivingEntity target, SkillType skill) { + boolean process; + + if (target instanceof Player || (target instanceof Tameable && ((Tameable) target).isTamed())) { + process = skill.getPVPEnabled(); + } + else { + process = skill.getPVEEnabled(); + } + + return process; + } }