Browse Source

Cleanup CombatUtils.

Opting to stick with the if-else rather than the switch statement,
because I think it's slightly easier to read.
GJ 11 years ago
parent
commit
caaac232d7

+ 6 - 0
src/main/java/com/gmail/nossr50/datatypes/skills/SkillType.java

@@ -5,7 +5,9 @@ import java.util.Collections;
 import java.util.List;
 
 import org.bukkit.Color;
+import org.bukkit.entity.Entity;
 import org.bukkit.entity.Player;
+import org.bukkit.entity.Tameable;
 
 import com.gmail.nossr50.mcMMO;
 import com.gmail.nossr50.config.Config;
@@ -201,4 +203,8 @@ public enum SkillType {
     public void celebrateLevelUp(Player player) {
         ParticleEffectUtils.fireworkParticleShower(player, runescapeColor);
     }
+
+    public boolean shouldProcess(Entity target) {
+        return (target instanceof Player || (target instanceof Tameable && ((Tameable) target).isTamed())) ? getPVPEnabled() : getPVEEnabled();
+    }
 }

+ 1 - 2
src/main/java/com/gmail/nossr50/skills/acrobatics/AcrobaticsManager.java

@@ -15,7 +15,6 @@ import com.gmail.nossr50.locale.LocaleLoader;
 import com.gmail.nossr50.skills.SkillManager;
 import com.gmail.nossr50.util.Misc;
 import com.gmail.nossr50.util.Permissions;
-import com.gmail.nossr50.util.skills.CombatUtils;
 import com.gmail.nossr50.util.skills.ParticleEffectUtils;
 import com.gmail.nossr50.util.skills.SkillUtils;
 
@@ -37,7 +36,7 @@ public class AcrobaticsManager extends SkillManager {
                 return false;
             }
 
-            return CombatUtils.shouldProcessSkill(damager, skill);
+            return skill.shouldProcess(damager);
         }
 
         return false;

+ 10 - 61
src/main/java/com/gmail/nossr50/util/skills/CombatUtils.java

@@ -193,8 +193,9 @@ public final class CombatUtils {
      */
     public static void processCombatAttack(EntityDamageByEntityEvent event, Entity attacker, LivingEntity target) {
         Entity damager = event.getDamager();
+        EntityType entityType = damager.getType();
 
-        if (attacker instanceof Player && damager.getType() == EntityType.PLAYER) {
+        if (attacker instanceof Player && entityType == EntityType.PLAYER) {
             Player player = (Player) attacker;
 
             if (Misc.isNPCEntity(player)) {
@@ -220,7 +221,7 @@ public final class CombatUtils {
             }
 
             if (ItemUtils.isSword(heldItem)) {
-                if (!shouldProcessSkill(target, SkillType.SWORDS)) {
+                if (!SkillType.SWORDS.shouldProcess(target)) {
                     return;
                 }
 
@@ -229,7 +230,7 @@ public final class CombatUtils {
                 }
             }
             else if (ItemUtils.isAxe(heldItem)) {
-                if (!shouldProcessSkill(target, SkillType.AXES)) {
+                if (!SkillType.AXES.shouldProcess(target)) {
                     return;
                 }
 
@@ -238,7 +239,7 @@ public final class CombatUtils {
                 }
             }
             else if (heldItem.getType() == Material.AIR) {
-                if (!shouldProcessSkill(target, SkillType.UNARMED)) {
+                if (!SkillType.UNARMED.shouldProcess(target)) {
                     return;
                 }
 
@@ -248,16 +249,11 @@ public final class CombatUtils {
             }
         }
 
-        /* Temporary fix for MCPC+
-         * 
-         * This will be reverted back to the switch statement once the fix is addressed on their end.
-         */
-
-        else if (damager.getType() == EntityType.WOLF) {
+        else if (entityType == EntityType.WOLF) {
             Wolf wolf = (Wolf) damager;
             AnimalTamer tamer = wolf.getOwner();
 
-            if (tamer != null && tamer instanceof Player && shouldProcessSkill(target, SkillType.TAMING)) {
+            if (tamer != null && tamer instanceof Player && SkillType.TAMING.shouldProcess(target)) {
                 Player master = (Player) tamer;
 
                 if (!Misc.isNPCEntity(master) && SkillType.TAMING.getPermissions(master)) {
@@ -265,11 +261,11 @@ public final class CombatUtils {
                 }
             }
         }
-        else if (damager.getType() == EntityType.ARROW) {
+        else if (entityType == EntityType.ARROW) {
             Arrow arrow = (Arrow) damager;
             LivingEntity shooter = arrow.getShooter();
 
-            if (shooter != null && shooter instanceof Player && shouldProcessSkill(target, SkillType.ARCHERY)) {
+            if (shooter != null && shooter instanceof Player && SkillType.ARCHERY.shouldProcess(target)) {
                 Player player = (Player) shooter;
 
                 if (!Misc.isNPCEntity(player) && SkillType.ARCHERY.getPermissions(player)) {
@@ -278,49 +274,6 @@ public final class CombatUtils {
             }
         }
 
-//        switch (damager.getType()) {
-//            case WOLF:
-//                Wolf wolf = (Wolf) damager;
-//                AnimalTamer tamer = wolf.getOwner();
-//
-//                if (tamer == null || !(tamer instanceof Player) || !shouldProcessSkill(target, SkillType.TAMING)) {
-//                    break;
-//                }
-//
-//                Player master = (Player) tamer;
-//
-//                if (Misc.isNPCEntity(master)) {
-//                    break;
-//                }
-//
-//                if (Permissions.skillEnabled(master, SkillType.TAMING)) {
-//                    processTamingCombat(target, master, wolf, event);
-//                }
-//
-//                break;
-//
-//            case ARROW:
-//                LivingEntity shooter = ((Arrow) damager).getShooter();
-//
-//                if (shooter == null || !(shooter instanceof Player) || !shouldProcessSkill(target, SkillType.ARCHERY)) {
-//                    break;
-//                }
-//
-//                Player player = (Player) shooter;
-//
-//                if (Misc.isNPCEntity(player)) {
-//                    break;
-//                }
-//
-//                if (Permissions.skillEnabled(player, SkillType.ARCHERY)) {
-//                    processArcheryCombat(target, player, event, damager);
-//                }
-//                break;
-//
-//            default:
-//                break;
-//        }
-
         if (target instanceof Player) {
             if (Misc.isNPCEntity(target)) {
                 return;
@@ -335,7 +288,7 @@ public final class CombatUtils {
             }
 
             if (ItemUtils.isSword(player.getItemInHand())) {
-                if (!shouldProcessSkill(target, SkillType.SWORDS)) {
+                if (!SkillType.SWORDS.shouldProcess(target)) {
                     return;
                 }
 
@@ -610,10 +563,6 @@ public final class CombatUtils {
         return false;
     }
 
-    public static boolean shouldProcessSkill(Entity target, SkillType skill) {
-        return (target instanceof Player || (target instanceof Tameable && ((Tameable) target).isTamed())) ? skill.getPVPEnabled() : skill.getPVEEnabled();
-    }
-
     public static double callFakeDamageEvent(Entity attacker, Entity target, double damage) {
         return callFakeDamageEvent(attacker, target, DamageCause.ENTITY_ATTACK, damage);
     }