From 454acc91d655aaeccadf39163f0d8ab9208c8207 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 19 Jan 2015 10:35:12 +0100 Subject: [PATCH] Break out Territory Shield logic and increase its event priority. --- .../factions/engine/EngineMain.java | 60 ++++++++++++------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/src/com/massivecraft/factions/engine/EngineMain.java b/src/com/massivecraft/factions/engine/EngineMain.java index 2a0efa84..c469f752 100644 --- a/src/com/massivecraft/factions/engine/EngineMain.java +++ b/src/com/massivecraft/factions/engine/EngineMain.java @@ -847,7 +847,7 @@ public class EngineMain extends EngineAbstract Entity edefender = event.getEntity(); if (!(edefender instanceof Player)) return true; Player defender = (Player)edefender; - MPlayer udefender = MPlayer.get(edefender); + MPlayer mdefender = MPlayer.get(edefender); // ... and the attacker is someone else ... Entity eattacker = MUtil.getLiableDamager(event); @@ -907,7 +907,7 @@ public class EngineMain extends EngineAbstract // ... are PVP rules completely ignored in this world? ... if (!MConf.get().worldsPvpRulesEnabled.contains(defenderPs.getWorld())) return true; - Faction defendFaction = udefender.getFaction(); + Faction defendFaction = mdefender.getFaction(); Faction attackFaction = uattacker.getFaction(); if (attackFaction.isNone() && MConf.get().disablePVPForFactionlessPlayers) @@ -934,7 +934,7 @@ public class EngineMain extends EngineAbstract Rel relation = defendFaction.getRelationTo(attackFaction); // Check the relation - if (udefender.hasFaction() && relation.isFriend() && defenderPsFaction.getFlag(MFlag.getFlagFriendlyire()) == false) + if (mdefender.hasFaction() && relation.isFriend() && defenderPsFaction.getFlag(MFlag.getFlagFriendlyire()) == false) { ret = falseUnlessDisallowedPvpEventCancelled(attacker, defender, event); if (!ret && notify) uattacker.msg("You can't hurt %s.", relation.getDescPlayerMany()); @@ -942,37 +942,53 @@ public class EngineMain extends EngineAbstract } // You can not hurt neutrals in their own territory. - boolean ownTerritory = udefender.isInOwnTerritory(); + boolean ownTerritory = mdefender.isInOwnTerritory(); - if (udefender.hasFaction() && ownTerritory && relation == Rel.NEUTRAL) + if (mdefender.hasFaction() && ownTerritory && relation == Rel.NEUTRAL) { ret = falseUnlessDisallowedPvpEventCancelled(attacker, defender, event); if (!ret && notify) { - uattacker.msg("You can't hurt %s in their own territory unless you declare them as an enemy.", udefender.describeTo(uattacker)); - udefender.msg("%s tried to hurt you.", uattacker.describeTo(udefender, true)); + uattacker.msg("You can't hurt %s in their own territory unless you declare them as an enemy.", mdefender.describeTo(uattacker)); + mdefender.msg("%s tried to hurt you.", uattacker.describeTo(mdefender, true)); } return ret; } - // Damage will be dealt. However check if the damage should be reduced. - double damage = event.getDamage(); - if (damage > 0.0 && udefender.hasFaction() && ownTerritory && MConf.get().territoryShieldFactor > 0) - { - double newDamage = damage * (1D - MConf.get().territoryShieldFactor); - MUtil.setDamage(event, newDamage); - - // Send message - if (notify) - { - String perc = MessageFormat.format("{0,number,#%}", (MConf.get().territoryShieldFactor)); // TODO does this display correctly?? - udefender.msg("Enemy damage reduced by %s.", perc); - } - } - return true; } + // -------------------------------------------- // + // TERRITORY SHIELD + // -------------------------------------------- // + + @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) + public void territoryShield(EntityDamageByEntityEvent event) + { + // If the entity is a player ... + Entity entity = event.getEntity(); + if (!(entity instanceof Player)) return; + Player player = (Player)entity; + MPlayer mplayer = MPlayer.get(player); + + // ... and that player has a faction ... + if ( ! mplayer.hasFaction()) return; + + // ... and that player is in their own territory ... + if ( ! mplayer.isInOwnTerritory()) return; + + // ... and a territoryShieldFactor is configured ... + if (MConf.get().territoryShieldFactor <= 0) return; + + // ... then scale the damage ... + double factor = 1D - MConf.get().territoryShieldFactor; + MUtil.scaleDamage(event, factor); + + // ... and inform. + String perc = MessageFormat.format("{0,number,#%}", (MConf.get().territoryShieldFactor)); + mplayer.msg("Enemy damage reduced by %s.", perc); + } + // -------------------------------------------- // // REMOVE PLAYER DATA WHEN BANNED // -------------------------------------------- //