diff --git a/Mage.Sets/src/mage/cards/m/MemoryCrystal.java b/Mage.Sets/src/mage/cards/m/MemoryCrystal.java index 38022dbc38..a1a0c9975c 100644 --- a/Mage.Sets/src/mage/cards/m/MemoryCrystal.java +++ b/Mage.Sets/src/mage/cards/m/MemoryCrystal.java @@ -33,6 +33,8 @@ import mage.abilities.SpellAbility; import mage.abilities.common.SimpleStaticAbility; import mage.abilities.condition.common.BuybackCondition; import mage.abilities.effects.common.cost.CostModificationEffectImpl; +import mage.abilities.keyword.BuybackAbility; +import mage.cards.Card; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; @@ -80,7 +82,18 @@ class MemoryCrystalSpellsCostReductionEffect extends CostModificationEffectImpl @Override public boolean apply(Game game, Ability source, Ability abilityToModify) { - CardUtil.reduceCost(abilityToModify, 2); + + Card card = game.getCard(abilityToModify.getSourceId()); + if (card != null) { + for (Ability ability : card.getAbilities()) { + if (ability instanceof BuybackAbility) { + if (((BuybackAbility) ability).isActivated()) { + int amountToReduce = ((BuybackAbility) ability).reduceCost(2); + CardUtil.reduceCost(abilityToModify, amountToReduce); + } + } + } + } return true; } @@ -90,7 +103,9 @@ class MemoryCrystalSpellsCostReductionEffect extends CostModificationEffectImpl if (abilityToModify.getControllerId().equals(source.getControllerId())) { Spell spell = (Spell) game.getStack().getStackObject(abilityToModify.getId()); if (spell != null) { - return BuybackCondition.instance.apply(game, abilityToModify); + if (BuybackCondition.instance.apply(game, abilityToModify)) { + return true; + } } } } diff --git a/Mage/src/main/java/mage/abilities/keyword/BuybackAbility.java b/Mage/src/main/java/mage/abilities/keyword/BuybackAbility.java index 8bd67d0348..4f3550804e 100644 --- a/Mage/src/main/java/mage/abilities/keyword/BuybackAbility.java +++ b/Mage/src/main/java/mage/abilities/keyword/BuybackAbility.java @@ -28,6 +28,7 @@ package mage.abilities.keyword; import java.util.Iterator; +import java.util.List; import mage.abilities.Ability; import mage.abilities.SpellAbility; import mage.abilities.StaticAbility; @@ -36,6 +37,8 @@ import mage.abilities.costs.Costs; import mage.abilities.costs.OptionalAdditionalCost; import mage.abilities.costs.OptionalAdditionalCostImpl; import mage.abilities.costs.OptionalAdditionalSourceCosts; +import mage.abilities.costs.mana.GenericManaCost; +import mage.abilities.costs.mana.ManaCost; import mage.abilities.costs.mana.ManaCostsImpl; import mage.abilities.effects.ReplacementEffectImpl; import mage.cards.Card; @@ -66,6 +69,7 @@ public class BuybackAbility extends StaticAbility implements OptionalAdditionalS private static final String reminderTextCost = "You may {cost} in addition to any other costs as you cast this spell. If you do, put this card into your hand as it resolves."; private static final String reminderTextMana = "You may pay an additional {cost} as you cast this spell. If you do, put this card into your hand as it resolves."; protected OptionalAdditionalCost buybackCost; + private int amountToReduceBy = 0; public BuybackAbility(String manaString) { super(Zone.STACK, new BuybackEffect()); @@ -94,6 +98,33 @@ public class BuybackAbility extends StaticAbility implements OptionalAdditionalS if (buybackCost != null) { ((Costs) buybackCost).add(cost); } + } + + public void resetReduceCost() { + amountToReduceBy = 0; + } + + // Called by Memory Crystal to reduce mana costs. + public int reduceCost(int genericManaToReduce) { + int amountToReduce = genericManaToReduce; + if (buybackCost != null) { + for (Object cost : ((Costs) buybackCost)) { + if (cost instanceof ManaCostsImpl) { + for (Object c : (ManaCostsImpl) cost) { + if (c instanceof GenericManaCost) { + int newCostCMC = ((GenericManaCost) c).convertedManaCost() - amountToReduceBy - genericManaToReduce; + if (newCostCMC > 0) { + amountToReduceBy += genericManaToReduce; + } else { + amountToReduce = ((GenericManaCost) c).convertedManaCost() - amountToReduceBy; + amountToReduceBy = ((GenericManaCost) c).convertedManaCost(); + } + } + } + } + } + } + return amountToReduce; } @Override @@ -101,12 +132,14 @@ public class BuybackAbility extends StaticAbility implements OptionalAdditionalS if (buybackCost != null) { return buybackCost.isActivated(); } + resetReduceCost(); return false; } public void resetBuyback() { if (buybackCost != null) { buybackCost.reset(); + resetReduceCost(); } }