From 9aaa6de945e9ced4c28c6b3fda8d9130f30c207a Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 31 Jul 2013 14:50:28 +0200 Subject: [PATCH] Avoid NPE that could arrise due to NPC plugins etc. --- .../massivecraft/factions/entity/UPlayer.java | 10 +++--- .../listeners/FactionsListenerMain.java | 31 ++++++++++--------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/com/massivecraft/factions/entity/UPlayer.java b/src/com/massivecraft/factions/entity/UPlayer.java index 46b0a1af..07a3bd8e 100644 --- a/src/com/massivecraft/factions/entity/UPlayer.java +++ b/src/com/massivecraft/factions/entity/UPlayer.java @@ -587,14 +587,16 @@ public class UPlayer extends SenderEntity implements EconomyParticipato public boolean isInOwnTerritory() { - // TODO: Use Mixin to get this PS instead - return BoardColls.get().getFactionAt(Mixin.getSenderPs(this.getId())) == this.getFaction(); + PS ps = Mixin.getSenderPs(this.getId()); + if (ps == null) return false; + return BoardColls.get().getFactionAt(ps) == this.getFaction(); } public boolean isInEnemyTerritory() { - // TODO: Use Mixin to get this PS instead - return BoardColls.get().getFactionAt(Mixin.getSenderPs(this.getId())).getRelationTo(this) == Rel.ENEMY; + PS ps = Mixin.getSenderPs(this.getId()); + if (ps == null) return false; + return BoardColls.get().getFactionAt(ps).getRelationTo(this) == Rel.ENEMY; } // -------------------------------------------- // diff --git a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java index cc7d7a2c..24ee19ff 100644 --- a/src/com/massivecraft/factions/listeners/FactionsListenerMain.java +++ b/src/com/massivecraft/factions/listeners/FactionsListenerMain.java @@ -271,7 +271,7 @@ public class FactionsListenerMain implements Listener Entity edefender = event.getEntity(); if (!(edefender instanceof Player)) return true; Player defender = (Player)edefender; - UPlayer fdefender = UPlayer.get(edefender); + UPlayer udefender = UPlayer.get(edefender); // Check Disabled if (UConf.isDisabled(defender)) return true; @@ -306,7 +306,7 @@ public class FactionsListenerMain implements Listener // ... and if the attacker is a player ... if (!(eattacker instanceof Player)) return true; Player attacker = (Player)eattacker; - UPlayer fattacker = UPlayer.get(attacker); + UPlayer uattacker = UPlayer.get(attacker); // ... does this player bypass all protection? ... if (MConf.get().playersWhoBypassAllProtection.contains(attacker.getName())) return true; @@ -320,20 +320,20 @@ public class FactionsListenerMain implements Listener // NOTE: This check is probably not that important but we could keep it anyways. if (attackerPsFaction.getFlag(FFlag.PVP) == false) { - if (notify) fattacker.msg("PVP is disabled in %s.", attackerPsFaction.describeTo(fattacker)); + if (notify) uattacker.msg("PVP is disabled in %s.", attackerPsFaction.describeTo(uattacker)); return false; } // ... are PVP rules completely ignored in this world? ... if (MConf.get().worldsIgnorePvP.contains(defenderPs.getWorld())) return true; - Faction defendFaction = fdefender.getFaction(); - Faction attackFaction = fattacker.getFaction(); + Faction defendFaction = udefender.getFaction(); + Faction attackFaction = uattacker.getFaction(); UConf uconf = UConf.get(attackFaction); if (attackFaction.isNone() && uconf.disablePVPForFactionlessPlayers) { - if (notify) fattacker.msg("You can't hurt other players until you join a faction."); + if (notify) uattacker.msg("You can't hurt other players until you join a faction."); return false; } else if (defendFaction.isNone()) @@ -345,7 +345,7 @@ public class FactionsListenerMain implements Listener } else if (uconf.disablePVPForFactionlessPlayers) { - if (notify) fattacker.msg("You can't hurt players who are not currently in a faction."); + if (notify) uattacker.msg("You can't hurt players who are not currently in a faction."); return false; } } @@ -353,27 +353,28 @@ public class FactionsListenerMain implements Listener Rel relation = defendFaction.getRelationTo(attackFaction); // Check the relation - if (fdefender.hasFaction() && relation.isFriend() && defenderPsFaction.getFlag(FFlag.FRIENDLYFIRE) == false) + if (udefender.hasFaction() && relation.isFriend() && defenderPsFaction.getFlag(FFlag.FRIENDLYFIRE) == false) { - if (notify) fattacker.msg("You can't hurt %s.", relation.getDescPlayerMany()); + if (notify) uattacker.msg("You can't hurt %s.", relation.getDescPlayerMany()); return false; } // You can not hurt neutrals in their own territory. - boolean ownTerritory = fdefender.isInOwnTerritory(); - if (fdefender.hasFaction() && ownTerritory && relation == Rel.NEUTRAL) + boolean ownTerritory = udefender.isInOwnTerritory(); + + if (udefender.hasFaction() && ownTerritory && relation == Rel.NEUTRAL) { if (notify) { - fattacker.msg("You can't hurt %s in their own territory unless you declare them as an enemy.", fdefender.describeTo(fattacker)); - fdefender.msg("%s tried to hurt you.", fattacker.describeTo(fdefender, true)); + 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)); } return false; } // Damage will be dealt. However check if the damage should be reduced. double damage = event.getDamage(); - if (damage > 0.0 && fdefender.hasFaction() && ownTerritory && uconf.territoryShieldFactor > 0) + if (damage > 0.0 && udefender.hasFaction() && ownTerritory && uconf.territoryShieldFactor > 0) { int newDamage = (int)Math.ceil(damage * (1D - uconf.territoryShieldFactor)); event.setDamage(newDamage); @@ -382,7 +383,7 @@ public class FactionsListenerMain implements Listener if (notify) { String perc = MessageFormat.format("{0,number,#%}", (uconf.territoryShieldFactor)); // TODO does this display correctly?? - fdefender.msg("Enemy damage reduced by %s.", perc); + udefender.msg("Enemy damage reduced by %s.", perc); } }