From b96d6e12af3e64d70f6f2060d5e9b3356d4b6b21 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Wed, 15 Aug 2018 11:28:13 +0200 Subject: [PATCH] Added some exception handling. --- .../mage/cards/h/HaphazardBombardment.java | 8 ++++--- .../mage/cards/m/MetamorphicAlteration.java | 12 ++++++---- .../mage/abilities/keyword/KickerAbility.java | 4 ++-- .../mage/abilities/keyword/StormAbility.java | 24 +++++++++++-------- .../target/common/TargetSpellOrPermanent.java | 10 ++++---- 5 files changed, 34 insertions(+), 24 deletions(-) diff --git a/Mage.Sets/src/mage/cards/h/HaphazardBombardment.java b/Mage.Sets/src/mage/cards/h/HaphazardBombardment.java index 74c6fdb110..f41fd10dbe 100644 --- a/Mage.Sets/src/mage/cards/h/HaphazardBombardment.java +++ b/Mage.Sets/src/mage/cards/h/HaphazardBombardment.java @@ -131,9 +131,11 @@ class HaphazardBombardmentEndOfTurnEffect extends OneShotEffect { filter.add(new CounterPredicate(CounterType.AIM)); filter.add(Predicates.not(new AbilityPredicate(IndestructibleAbility.class))); List permanents = game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source.getSourceId(), game); - Permanent permanent = permanents.get(RandomUtil.nextInt(permanents.size())); - if (permanent != null) { - permanent.destroy(source.getSourceId(), game, false); + if (!permanents.isEmpty()) { + Permanent permanent = permanents.get(RandomUtil.nextInt(permanents.size())); + if (permanent != null) { + permanent.destroy(source.getSourceId(), game, false); + } } return true; } diff --git a/Mage.Sets/src/mage/cards/m/MetamorphicAlteration.java b/Mage.Sets/src/mage/cards/m/MetamorphicAlteration.java index 8b09566c29..07c2c8a20a 100644 --- a/Mage.Sets/src/mage/cards/m/MetamorphicAlteration.java +++ b/Mage.Sets/src/mage/cards/m/MetamorphicAlteration.java @@ -1,5 +1,6 @@ package mage.cards.m; +import java.util.UUID; import mage.MageObject; import mage.abilities.Ability; import mage.abilities.common.AsEntersBattlefieldAbility; @@ -18,8 +19,6 @@ import mage.target.Target; import mage.target.TargetPermanent; import mage.target.common.TargetCreaturePermanent; -import java.util.UUID; - /** * * @author noahg @@ -28,7 +27,7 @@ public final class MetamorphicAlteration extends CardImpl { public MetamorphicAlteration(UUID ownerId, CardSetInfo setInfo) { super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{1}{U}"); - + this.subtype.add(SubType.AURA); // Enchant creature @@ -81,8 +80,11 @@ class ChooseACreature extends OneShotEffect { if (target.canChoose(source.getSourceId(), controller.getId(), game)) { controller.choose(Outcome.Copy, target, source.getSourceId(), game); Permanent chosenPermanent = game.getPermanent(target.getFirstTarget()); - game.getState().setValue(source.getSourceId().toString() + INFO_KEY, chosenPermanent.copy()); + if (chosenPermanent != null) { + game.getState().setValue(source.getSourceId().toString() + INFO_KEY, chosenPermanent.copy()); + } } + return true; } return false; } @@ -150,4 +152,4 @@ class MetamorphicAlterationEffect extends ContinuousEffectImpl { public MetamorphicAlterationEffect copy() { return new MetamorphicAlterationEffect(this); } -} \ No newline at end of file +} diff --git a/Mage/src/main/java/mage/abilities/keyword/KickerAbility.java b/Mage/src/main/java/mage/abilities/keyword/KickerAbility.java index fdf305c532..a3ea9163ad 100644 --- a/Mage/src/main/java/mage/abilities/keyword/KickerAbility.java +++ b/Mage/src/main/java/mage/abilities/keyword/KickerAbility.java @@ -1,7 +1,7 @@ - package mage.abilities.keyword; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import mage.abilities.Ability; import mage.abilities.SpellAbility; import mage.abilities.StaticAbility; @@ -52,7 +52,7 @@ public class KickerAbility extends StaticAbility implements OptionalAdditionalSo protected static final String KICKER_REMINDER_MANA = "You may pay an additional {cost} as you cast this spell."; protected static final String KICKER_REMINDER_COST = "You may {cost} in addition to any other costs as you cast this spell."; - protected Map activations = new HashMap<>(); // zoneChangeCounter, activations + protected Map activations = new ConcurrentHashMap<>(); // zoneChangeCounter, activations protected String keywordText; protected String reminderText; diff --git a/Mage/src/main/java/mage/abilities/keyword/StormAbility.java b/Mage/src/main/java/mage/abilities/keyword/StormAbility.java index fd4f4f5791..45b99a4ab6 100644 --- a/Mage/src/main/java/mage/abilities/keyword/StormAbility.java +++ b/Mage/src/main/java/mage/abilities/keyword/StormAbility.java @@ -1,4 +1,3 @@ - package mage.abilities.keyword; import mage.MageObjectReference; @@ -14,6 +13,7 @@ import mage.game.events.GameEvent.EventType; import mage.game.stack.Spell; import mage.game.stack.StackObject; import mage.watchers.common.CastSpellLastTurnWatcher; +import org.apache.log4j.Logger; /** * @@ -75,17 +75,21 @@ class StormEffect extends OneShotEffect { MageObjectReference spellRef = (MageObjectReference) this.getValue("StormSpellRef"); if (spellRef != null) { CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get(CastSpellLastTurnWatcher.class.getSimpleName()); - int stormCount = watcher.getSpellOrder(spellRef, game) - 1; - if (stormCount > 0) { - Spell spell = (Spell) this.getValue("StormSpell"); - if (spell != null) { - if (!game.isSimulation()) { - game.informPlayers("Storm: " + spell.getLogName() + " will be copied " + stormCount + " time" + (stormCount > 1 ? "s" : "")); - } - for (int i = 0; i < stormCount; i++) { - spell.createCopyOnStack(game, source, source.getControllerId(), true); + if (watcher != null) { + int stormCount = watcher.getSpellOrder(spellRef, game) - 1; + if (stormCount > 0) { + Spell spell = (Spell) this.getValue("StormSpell"); + if (spell != null) { + if (!game.isSimulation()) { + game.informPlayers("Storm: " + spell.getLogName() + " will be copied " + stormCount + " time" + (stormCount > 1 ? "s" : "")); + } + for (int i = 0; i < stormCount; i++) { + spell.createCopyOnStack(game, source, source.getControllerId(), true); + } } } + } else { + Logger.getLogger(StormEffect.class).fatal("CastSpellLastTurnWatcher not found. game = " + game == null ? "NULL" : game.getGameType().toString()); } return true; } diff --git a/Mage/src/main/java/mage/target/common/TargetSpellOrPermanent.java b/Mage/src/main/java/mage/target/common/TargetSpellOrPermanent.java index 50ced1b860..f2a846ba37 100644 --- a/Mage/src/main/java/mage/target/common/TargetSpellOrPermanent.java +++ b/Mage/src/main/java/mage/target/common/TargetSpellOrPermanent.java @@ -248,10 +248,12 @@ public class TargetSpellOrPermanent extends TargetImpl { sb.append(permanent.getLogName()).append(' '); } else { Spell spell = game.getStack().getSpell(targetId); - if (spell.isFaceDown(game)) { - sb.append(GameLog.getNeutralColoredText("face down spell")); - } else { - sb.append(spell.getLogName()).append(' '); + if (spell != null) { + if (spell.isFaceDown(game)) { + sb.append(GameLog.getNeutralColoredText("face down spell")); + } else { + sb.append(spell.getLogName()).append(' '); + } } } }