Cleanup CombatUtils.

Opting to stick with the if-else rather than the switch statement,
because I think it's slightly easier to read.
This commit is contained in:
GJ 2013-10-31 15:03:06 -04:00
parent a9d51dad34
commit caaac232d7
3 changed files with 17 additions and 63 deletions

View File

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

View File

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

View File

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