fixed cases that could lead to NPE

This commit is contained in:
North 2013-01-03 15:09:27 +02:00
parent e4349c8b23
commit c083fae37b
9 changed files with 151 additions and 107 deletions

View file

@ -132,7 +132,9 @@ public class DevourEffect extends ReplacementEffectImpl<DevourEffect> {
game.informPlayers(new StringBuilder(creature.getName()).append(" devours ").append(devouredCreatures).append(" creatures").toString());
for (UUID targetId: target.getTargets()) {
Permanent targetCreature = game.getPermanent(targetId);
if (targetCreature != null) {
cardSubtypes.add((ArrayList<String>) targetCreature.getSubtype());
}
if (targetCreature == null || !targetCreature.sacrifice(source.getSourceId(), game)) {
return false;
}

View file

@ -62,8 +62,11 @@ public class RegenerateAttachedEffect extends ReplacementEffectImpl<RegenerateAt
public boolean apply(Game game, Ability source) {
//20110204 - 701.11
Permanent permanent = game.getPermanent(source.getSourceId());
if (permanent == null) {
return false;
}
Permanent equipped = game.getPermanent(permanent.getAttachedTo());
if (permanent != null && equipped.regenerate(this.getId(), game)) {
if (equipped != null && equipped.regenerate(this.getId(), game)) {
this.used = true;
return true;
}

View file

@ -83,8 +83,6 @@ import mage.util.CardUtil;
*
* @author LevelX2
*/
public class ConvokeAbility extends SimpleStaticAbility implements AdjustingSourceCosts {
private static final FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent();
@ -123,11 +121,17 @@ public class ConvokeAbility extends SimpleStaticAbility implements AdjustingSour
int adjCost = 0;
for (UUID creatureId: target.getTargets()) {
Permanent perm = game.getPermanent(creatureId);
if (perm!= null) {
if (perm == null) {
continue;
}
Card card = game.getCard(perm.getId());
if (card == null) {
continue;
}
ManaCosts manaCostsCreature = card.getSpellAbility().getManaCosts();
if (card != null && manaCostsCreature != null && manaCostsCreature.convertedManaCost() > 0) {
if (perm.tap(game)) {
if (manaCostsCreature != null && manaCostsCreature.convertedManaCost() > 0 && perm.tap(game)) {
Choice chooseManaType = buildChoice(manaCostsCreature, ability.getManaCostsToPay());
if (chooseManaType.getChoices().size() > 0) {
if (chooseManaType.getChoices().size() > 1) {
@ -165,8 +169,6 @@ public class ConvokeAbility extends SimpleStaticAbility implements AdjustingSour
}
}
}
}
}
this.getTargets().add(target);
CardUtil.adjustCost((SpellAbility)ability, adjCost);
}
@ -200,4 +202,3 @@ public class ConvokeAbility extends SimpleStaticAbility implements AdjustingSour
return "Convoke <i>(Each creature you tap while casting this spell reduces its cost by {1} or by one mana of that creature's color.)</i>";
}
}

View file

@ -89,8 +89,9 @@ public class TargetCreatureOrPlayer extends TargetImpl<TargetCreatureOrPlayer> {
return filter.match(permanent, game);
}
Player player = game.getPlayer(id);
if (player != null)
if (player != null) {
return filter.match(player, game);
}
return false;
}
@ -101,20 +102,24 @@ public class TargetCreatureOrPlayer extends TargetImpl<TargetCreatureOrPlayer> {
public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) {
Permanent permanent = game.getPermanent(id);
Player player = game.getPlayer(id);
if (source != null) {
MageObject targetSource = game.getObject(source.getSourceId());
if (permanent != null) {
if (source != null)
//TODO: check for replacement effects
return permanent.canBeTargetedBy(game.getObject(source.getSourceId()), controllerId, game) && filter.match(permanent, source.getSourceId(), controllerId, game);
else
return filter.match(permanent, source.getSourceId(), controllerId, game);
return permanent.canBeTargetedBy(targetSource, source.getControllerId(), game) && filter.match(permanent, source.getSourceId(), source.getControllerId(), game);
}
Player player = game.getPlayer(id);
if (player != null)
if (source != null)
if (player != null) {
return player.canBeTargetedBy(targetSource, game) && filter.match(player, game);
else
}
}
if (permanent != null) {
return filter.match(permanent, game);
}
if (player != null) {
return filter.match(player, game);
}
return false;
}
@ -135,17 +140,19 @@ public class TargetCreatureOrPlayer extends TargetImpl<TargetCreatureOrPlayer> {
Player player = game.getPlayer(playerId);
if (player != null && player.canBeTargetedBy(targetSource, game) && filter.match(player, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
for (Permanent permanent: game.getBattlefield().getActivePermanents(new FilterCreaturePermanent(), sourceControllerId, game)) {
if (permanent.canBeTargetedBy(targetSource, sourceControllerId, game) && filter.match(permanent, sourceId, sourceControllerId, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
return false;
}
@ -164,17 +171,19 @@ public class TargetCreatureOrPlayer extends TargetImpl<TargetCreatureOrPlayer> {
Player player = game.getPlayer(playerId);
if (player != null && filter.match(player, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
for (Permanent permanent: game.getBattlefield().getActivePermanents(new FilterCreaturePermanent(), sourceControllerId, game)) {
if (filter.match(permanent, null, sourceControllerId, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
return false;
}

View file

@ -75,27 +75,33 @@ public class TargetCreatureOrPlayerAmount extends TargetAmount<TargetCreatureOrP
return filter.match(permanent, game);
}
Player player = game.getPlayer(id);
if (player != null)
if (player != null) {
return filter.match(player, game);
}
return false;
}
@Override
public boolean canTarget(UUID id, Ability source, Game game) {
Permanent permanent = game.getPermanent(id);
Player player = game.getPlayer(id);
if (source != null) {
MageObject targetSource = game.getObject(source.getSourceId());
if (permanent != null) {
if (source != null)
return permanent.canBeTargetedBy(targetSource, source.getControllerId(), game) && filter.match(permanent, source.getSourceId(), source.getControllerId(), game);
else
}
if (player != null) {
return player.canBeTargetedBy(targetSource, game) && filter.match(player, game);
}
}
if (permanent != null) {
return filter.match(permanent, game);
}
Player player = game.getPlayer(id);
if (player != null)
if (source != null)
return player.canBeTargetedBy(targetSource, game) && filter.match(player, game);
else
if (player != null) {
return filter.match(player, game);
}
return false;
}
@ -107,17 +113,19 @@ public class TargetCreatureOrPlayerAmount extends TargetAmount<TargetCreatureOrP
Player player = game.getPlayer(playerId);
if (player != null && player.canBeTargetedBy(targetSource, game) && filter.match(player, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
for (Permanent permanent: game.getBattlefield().getActivePermanents(new FilterCreaturePermanent(), sourceControllerId, game)) {
if (permanent.canBeTargetedBy(targetSource, sourceControllerId, game) && filter.match(permanent, sourceId, sourceControllerId, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
return false;
}
@ -128,17 +136,19 @@ public class TargetCreatureOrPlayerAmount extends TargetAmount<TargetCreatureOrP
Player player = game.getPlayer(playerId);
if (player != null && filter.match(player, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
for (Permanent permanent: game.getBattlefield().getActivePermanents(new FilterCreaturePermanent(), sourceControllerId, game)) {
if (filter.match(permanent, null, sourceControllerId, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
return false;
}

View file

@ -82,9 +82,9 @@ public class TargetCreaturePermanentAmount extends TargetAmount<TargetCreaturePe
@Override
public boolean canTarget(UUID id, Ability source, Game game) {
Permanent permanent = game.getPermanent(id);
MageObject targetSource = game.getObject(source.getSourceId());
if (permanent != null) {
if (source != null) {
MageObject targetSource = game.getObject(source.getSourceId());
return permanent.canBeTargetedBy(targetSource, source.getControllerId(), game) && filter.match(permanent, source.getSourceId(), source.getControllerId(), game);
} else {
return filter.match(permanent, game);

View file

@ -97,27 +97,33 @@ public class TargetPermanentOrPlayer extends TargetImpl<TargetPermanentOrPlayer>
return filter.match(permanent, game);
}
Player player = game.getPlayer(id);
if (player != null)
if (player != null) {
return filter.match(player, game);
}
return false;
}
@Override
public boolean canTarget(UUID id, Ability source, Game game) {
Permanent permanent = game.getPermanent(id);
Player player = game.getPlayer(id);
if (source != null) {
MageObject targetSource = game.getObject(source.getSourceId());
if (permanent != null) {
if (source != null)
return permanent.canBeTargetedBy(targetSource, source.getControllerId(), game) && filter.match(permanent, source.getSourceId(), source.getControllerId(), game);
else
}
if (player != null) {
return player.canBeTargetedBy(targetSource, game) && filter.match(player, game);
}
}
if (permanent != null) {
return filter.match(permanent, game);
}
Player player = game.getPlayer(id);
if (player != null)
if (source != null)
return player.canBeTargetedBy(targetSource, game) && filter.match(player, game);
else
if (player != null) {
return filter.match(player, game);
}
return false;
}
@ -138,17 +144,19 @@ public class TargetPermanentOrPlayer extends TargetImpl<TargetPermanentOrPlayer>
Player player = game.getPlayer(playerId);
if (player != null && player.canBeTargetedBy(targetSource, game) && filter.match(player, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
for (Permanent permanent: game.getBattlefield().getActivePermanents(new FilterCreaturePermanent(), sourceControllerId, game)) {
if (permanent.canBeTargetedBy(targetSource, sourceControllerId, game) && filter.match(permanent, sourceId, sourceControllerId, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
return false;
}
@ -167,17 +175,19 @@ public class TargetPermanentOrPlayer extends TargetImpl<TargetPermanentOrPlayer>
Player player = game.getPlayer(playerId);
if (player != null && filter.match(player, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
for (Permanent permanent: game.getBattlefield().getActivePermanents(filterPermanent, sourceControllerId, game)) {
if (filter.match(permanent, null, sourceControllerId, game) && filter.match(permanent, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
return false;
}

View file

@ -104,24 +104,27 @@ public class TargetSpellOrPermanent extends TargetImpl<TargetSpellOrPermanent> {
return filter.match(permanent, game);
}
Spell spell = game.getStack().getSpell(id);
if (spell != null)
if (spell != null) {
return filter.match(spell, game);
}
return false;
}
@Override
public boolean canTarget(UUID id, Ability source, Game game) {
Permanent permanent = game.getPermanent(id);
MageObject targetSource = game.getObject(source.getSourceId());
if (permanent != null) {
if (source != null)
if (source != null) {
MageObject targetSource = game.getObject(source.getSourceId());
return permanent.canBeTargetedBy(targetSource, source.getControllerId(), game) && filter.match(permanent, source.getSourceId(), source.getControllerId(), game);
else
} else {
return filter.match(permanent, game);
}
}
Spell spell = game.getStack().getSpell(id);
if (spell != null)
if (spell != null) {
return filter.match(spell, game);
}
return false;
}
@ -142,17 +145,19 @@ public class TargetSpellOrPermanent extends TargetImpl<TargetSpellOrPermanent> {
Spell spell = game.getStack().getSpell(stackObject.getId());
if (spell != null && filter.match(spell, sourceId, sourceControllerId, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
for (Permanent permanent: game.getBattlefield().getActivePermanents(new FilterCreaturePermanent(), sourceControllerId, game)) {
if (permanent.canBeTargetedBy(targetSource, sourceControllerId, game) && filter.match(permanent, sourceId, sourceControllerId, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
return false;
}
@ -171,17 +176,19 @@ public class TargetSpellOrPermanent extends TargetImpl<TargetSpellOrPermanent> {
Spell spell = game.getStack().getSpell(stackObject.getId());
if (spell != null && filter.match(spell, null, sourceControllerId, game) && filter.match(spell, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
for (Permanent permanent: game.getBattlefield().getActivePermanents(filterPermanent, sourceControllerId, game)) {
if (filter.match(permanent, null, sourceControllerId, game) && filter.match(permanent, game)) {
count++;
if (count >= this.minNumberOfTargets)
if (count >= this.minNumberOfTargets) {
return true;
}
}
}
return false;
}

View file

@ -55,8 +55,9 @@ public class CardUtil {
*/
public static boolean shareTypes(Card card1, Card card2) {
if (card1 == null || card2 == null)
if (card1 == null || card2 == null) {
throw new IllegalArgumentException("Params can't be null");
}
for (Constants.CardType type : card1.getCardType()) {
if (card2.getCardType().contains(type)) {
@ -75,8 +76,9 @@ public class CardUtil {
*/
public static boolean shareSubtypes(Card card1, Card card2) {
if (card1 == null || card2 == null)
if (card1 == null || card2 == null) {
throw new IllegalArgumentException("Params can't be null");
}
for (String subtype : card1.getSubtype()) {
if (card2.getSubtype().contains(subtype)) {
@ -108,8 +110,8 @@ public class CardUtil {
boolean reduced = false;
for (ManaCost manaCost : previousCost) {
Mana mana = manaCost.getOptions().get(0);
int colorless = mana.getColorless();
if (!reduced && mana != null && colorless > 0) {
int colorless = mana != null ? mana.getColorless() : 0;
if (!reduced && colorless > 0) {
if ((colorless - reduceCount) > 0) {
int newColorless = colorless - reduceCount;
adjustedCost.add(new GenericManaCost(newColorless));