From 5aaec29361d491003fc96b73f2db8c7cb9825ef3 Mon Sep 17 00:00:00 2001 From: jeffwadsworth Date: Wed, 22 Jan 2020 16:57:01 -0600 Subject: [PATCH] - Workaround for #5437. The delayed triggers work correctly now and the game will no longer freeze. TODO: Cards that are copied and then cast do not retain the correct sourceObject. They instead point to the spell on the stack which fails when delayed triggers are used. --- Mage.Sets/src/mage/cards/a/ArcaneDenial.java | 28 +++++++++++++++---- .../src/mage/cards/i/IsochronScepter.java | 9 ++++-- .../DrawCardSourceControllerEffect.java | 14 +++++++--- .../effects/common/DrawCardTargetEffect.java | 7 +++-- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/Mage.Sets/src/mage/cards/a/ArcaneDenial.java b/Mage.Sets/src/mage/cards/a/ArcaneDenial.java index 5609c3c5362..52b43f4cc6e 100644 --- a/Mage.Sets/src/mage/cards/a/ArcaneDenial.java +++ b/Mage.Sets/src/mage/cards/a/ArcaneDenial.java @@ -1,4 +1,3 @@ - package mage.cards.a; import java.util.UUID; @@ -11,6 +10,7 @@ import mage.abilities.effects.OneShotEffect; import mage.abilities.effects.common.CreateDelayedTriggeredAbilityEffect; import mage.abilities.effects.common.DrawCardSourceControllerEffect; import mage.abilities.effects.common.DrawCardTargetEffect; +import mage.cards.Card; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; @@ -27,13 +27,16 @@ import mage.target.targetpointer.FixedTarget; public final class ArcaneDenial extends CardImpl { public ArcaneDenial(UUID ownerId, CardSetInfo setInfo) { - super(ownerId,setInfo,new CardType[]{CardType.INSTANT},"{1}{U}"); + super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{1}{U}"); - // Counter target spell. Its controller may draw up to two cards at the beginning of the next turn's upkeep. + // Counter target spell. Its controller may draw up to two cards at + // the beginning of the next turn's upkeep. this.getSpellAbility().addEffect(new ArcaneDenialEffect()); this.getSpellAbility().addTarget(new TargetSpell()); // You draw a card at the beginning of the next turn's upkeep. - this.getSpellAbility().addEffect(new CreateDelayedTriggeredAbilityEffect(new AtTheBeginOfNextUpkeepDelayedTriggeredAbility(new DrawCardSourceControllerEffect(1)), false)); + this.getSpellAbility().addEffect(new CreateDelayedTriggeredAbilityEffect( + new AtTheBeginOfNextUpkeepDelayedTriggeredAbility( + new DrawCardSourceControllerEffect(1)), false)); } public ArcaneDenial(final ArcaneDenial card) { @@ -50,7 +53,8 @@ class ArcaneDenialEffect extends OneShotEffect { public ArcaneDenialEffect() { super(Outcome.Detriment); - staticText = "Counter target spell. Its controller may draw up to two cards at the beginning of the next turn's upkeep"; + staticText = "Counter target spell. Its controller may draw up to two cards " + + "at the beginning of the next turn's upkeep"; } public ArcaneDenialEffect(final ArcaneDenialEffect effect) { @@ -64,8 +68,22 @@ class ArcaneDenialEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { + Card originalSourceCard; // needed for copies of Arcane Denial See Bug #5437 Player controller = null; boolean countered = false; + // If the source originates from a copy (Isochron Scepter for example), + // then we set the source's sourceId to the original card, otherwise it + // uses the spell on the stack as its sourceObject which won't work + // correctly with any cards that make use of Delayed Triggers. + // see Bug #5437 + Card cardToCheck = game.getCard(source.getSourceId()); + if (cardToCheck != null + && cardToCheck.isCopy()) { // Isochron Scepter imprinted card for example + originalSourceCard = (Card) game.getState().getValue("RememberSourceObject" + source.getSourceId()); + if (originalSourceCard != null) { + source.setSourceId(originalSourceCard.getId()); + } + } UUID targetId = this.getTargetPointer().getFirst(game, source); if (targetId != null) { controller = game.getPlayer(game.getControllerId(targetId)); diff --git a/Mage.Sets/src/mage/cards/i/IsochronScepter.java b/Mage.Sets/src/mage/cards/i/IsochronScepter.java index 42af1d5e340..fbacfe77f71 100644 --- a/Mage.Sets/src/mage/cards/i/IsochronScepter.java +++ b/Mage.Sets/src/mage/cards/i/IsochronScepter.java @@ -88,7 +88,7 @@ class IsochronScepterImprintEffect extends OneShotEffect { && controller.choose(Outcome.Benefit, controller.getHand(), target, game)) { Card card = controller.getHand().get(target.getFirstTarget(), game); if (card != null) { - controller.moveCardsToExile(card, source, game, true, source.getSourceId(), + controller.moveCardsToExile(card, source, game, true, source.getSourceId(), sourcePermanent.getIdName() + " (Imprint)"); Permanent permanent = game.getPermanent(source.getSourceId()); if (permanent != null) { @@ -134,8 +134,8 @@ class IsochronScepterCopyEffect extends OneShotEffect { Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { Permanent scepter = game.getPermanentOrLKIBattlefield(source.getSourceId()); - if (scepter != null - && scepter.getImprinted() != null + if (scepter != null + && scepter.getImprinted() != null && !scepter.getImprinted().isEmpty()) { Card imprintedInstant = game.getCard(scepter.getImprinted().get(0)); if (imprintedInstant != null @@ -143,6 +143,9 @@ class IsochronScepterCopyEffect extends OneShotEffect { if (controller.chooseUse(outcome, "Create a copy of " + imprintedInstant.getName() + '?', source, game)) { Card copiedCard = game.copyCard(imprintedInstant, source, source.getControllerId()); if (copiedCard != null) { + // Need to record the sourceObject info for the copy (example Arcane Denial) + // TODO implement this within the codebase. See Bug #5437 + game.getState().setValue("RememberSourceObject" + copiedCard.getId(), imprintedInstant); game.getExile().add(source.getSourceId(), "", copiedCard); game.getState().setZone(copiedCard.getId(), Zone.EXILED); if (controller.chooseUse(outcome, "Cast the copied card without paying mana cost?", source, game)) { diff --git a/Mage/src/main/java/mage/abilities/effects/common/DrawCardSourceControllerEffect.java b/Mage/src/main/java/mage/abilities/effects/common/DrawCardSourceControllerEffect.java index 8a2533827de..0f22df3aaa4 100644 --- a/Mage/src/main/java/mage/abilities/effects/common/DrawCardSourceControllerEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/common/DrawCardSourceControllerEffect.java @@ -52,7 +52,8 @@ public class DrawCardSourceControllerEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player player = game.getPlayer(source.getControllerId()); - if (player != null) { + if (player != null + && player.isInGame()) { player.drawCards(amount.calculate(game, source, this), game); return true; } @@ -61,9 +62,14 @@ public class DrawCardSourceControllerEffect extends OneShotEffect { private void setText() { StringBuilder sb = new StringBuilder(); - boolean oneCard = (amount instanceof StaticValue && amount.calculate(null, null, this) == 1) - || amount instanceof PermanentsOnBattlefieldCount || amount.toString().equals("1") || amount.toString().equals("a"); - sb.append(whoDrawCard.isEmpty() ? "" : whoDrawCard + " ").append("draw ").append(oneCard ? "a" : CardUtil.numberToText(amount.toString())).append(" card"); + boolean oneCard = (amount instanceof StaticValue + && amount.calculate(null, null, this) == 1) + || amount instanceof PermanentsOnBattlefieldCount + || amount.toString().equals("1") + || amount.toString().equals("a"); + sb.append(whoDrawCard.isEmpty() ? "" : whoDrawCard + + " ").append("draw ").append(oneCard ? "a" + : CardUtil.numberToText(amount.toString())).append(" card"); if (!oneCard) { sb.append('s'); } diff --git a/Mage/src/main/java/mage/abilities/effects/common/DrawCardTargetEffect.java b/Mage/src/main/java/mage/abilities/effects/common/DrawCardTargetEffect.java index 585c3f0341e..64a996298a7 100644 --- a/Mage/src/main/java/mage/abilities/effects/common/DrawCardTargetEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/common/DrawCardTargetEffect.java @@ -1,4 +1,3 @@ - package mage.abilities.effects.common; import java.util.UUID; @@ -63,12 +62,14 @@ public class DrawCardTargetEffect extends OneShotEffect { public boolean apply(Game game, Ability source) { for (UUID playerId : getTargetPointer().getTargets(game, source)) { Player player = game.getPlayer(playerId); - if (player != null) { + if (player != null + && player.isInGame()) { int cardsToDraw = amount.calculate(game, source, this); if (upTo) { cardsToDraw = player.getAmount(0, cardsToDraw, "Draw how many cards?", game); } - if (!optional || player.chooseUse(outcome, "Use draw effect?", source, game)) { + if (!optional + || player.chooseUse(outcome, "Use draw effect?", source, game)) { player.drawCards(cardsToDraw, game); } }