From 83be13a68b2eaf40aaa40ae55c08da399fac862a Mon Sep 17 00:00:00 2001
From: LevelX2 <ludwig.hirth@online.de>
Date: Thu, 29 May 2014 16:42:27 +0200
Subject: [PATCH] * Flashback - Fixed that cost midification effects was
 applied twice for flashbacked spells.

---
 Mage/src/mage/abilities/Ability.java          | 24 ++++++++++-------
 Mage/src/mage/abilities/AbilityImpl.java      | 27 ++++++++++++++-----
 .../abilities/keyword/FlashbackAbility.java   |  1 +
 3 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/Mage/src/mage/abilities/Ability.java b/Mage/src/mage/abilities/Ability.java
index 502e39c6e3..ab7fa73a00 100644
--- a/Mage/src/mage/abilities/Ability.java
+++ b/Mage/src/mage/abilities/Ability.java
@@ -28,9 +28,9 @@
 
 package mage.abilities;
 
-import mage.constants.AbilityType;
-import mage.constants.EffectType;
-import mage.constants.Zone;
+import java.io.Serializable;
+import java.util.List;
+import java.util.UUID;
 import mage.MageObject;
 import mage.abilities.costs.AlternativeCost;
 import mage.abilities.costs.Cost;
@@ -41,16 +41,15 @@ import mage.abilities.effects.Effect;
 import mage.abilities.effects.Effects;
 import mage.choices.Choice;
 import mage.choices.Choices;
+import mage.constants.AbilityType;
+import mage.constants.AbilityWord;
+import mage.constants.EffectType;
+import mage.constants.Zone;
 import mage.game.Controllable;
 import mage.game.Game;
 import mage.target.Target;
 import mage.target.Targets;
 
-import java.io.Serializable;
-import java.util.List;
-import java.util.UUID;
-import mage.constants.AbilityWord;
-
 /**
  * Practically everything in the game is started from an Ability.  This
  * interface describes what an Ability is composed of at the highest level.
@@ -446,5 +445,12 @@ public interface Ability extends Controllable, Serializable {
      * @return
      */
     String getGameLogMessage(Game game);
-    
+
+    /**
+     * Used to deactivate cost modification logic of ability activation for some special handling
+     * (e.g. FlashbackAbility gets cost modifiaction twice because of how it#s ahndled now)
+     *
+     * @param active execute no cost modification
+     */
+    void setCostModificationActive(boolean active);
 }
diff --git a/Mage/src/mage/abilities/AbilityImpl.java b/Mage/src/mage/abilities/AbilityImpl.java
index 3ee2235b12..09798babc9 100644
--- a/Mage/src/mage/abilities/AbilityImpl.java
+++ b/Mage/src/mage/abilities/AbilityImpl.java
@@ -97,6 +97,7 @@ public abstract class AbilityImpl<T extends AbilityImpl<T>> implements Ability {
     protected boolean ruleAtTheTop = false;
     protected boolean ruleVisible = true;
     protected boolean ruleAdditionalCostsVisible = true;
+    protected boolean costModificationActive = true;
 
     @Override
     public abstract T copy();
@@ -133,6 +134,7 @@ public abstract class AbilityImpl<T extends AbilityImpl<T>> implements Ability {
         this.ruleAtTheTop = ability.ruleAtTheTop;
         this.ruleVisible = ability.ruleVisible;
         this.ruleAdditionalCostsVisible = ability.ruleAdditionalCostsVisible;
+        this.costModificationActive = ability.costModificationActive;
     }
 
     @Override
@@ -221,11 +223,15 @@ public abstract class AbilityImpl<T extends AbilityImpl<T>> implements Ability {
         // if ability can be cast for no mana, clear the mana costs now, because additional mana costs must be paid.
         // For Flashback ability can be set X before, so the X costs have to be restored for the flashbacked ability
         if (noMana) {
-            int xValue = this.getManaCostsToPay().getX();
-            this.getManaCostsToPay().clear();
-            VariableManaCost xCosts = new VariableManaCost();
-            xCosts.setAmount(xValue);
-            this.getManaCostsToPay().add(xCosts);
+            if (this.getManaCostsToPay().getVariableCosts().size() > 0) {
+                int xValue = this.getManaCostsToPay().getX();
+                this.getManaCostsToPay().clear();
+                VariableManaCost xCosts = new VariableManaCost();
+                xCosts.setAmount(xValue);
+                this.getManaCostsToPay().add(xCosts);
+            } else {
+                this.getManaCostsToPay().clear();
+            }
         }
         // 20130201 - 601.2b
         // If the spell has alternative or additional costs that will be paid as it's being cast such
@@ -329,7 +335,11 @@ public abstract class AbilityImpl<T extends AbilityImpl<T>> implements Ability {
         }
 
         //20101001 - 601.2e
-        game.getContinuousEffects().costModification(this, game);
+        if (costModificationActive) {
+            game.getContinuousEffects().costModification(this, game);
+        } else {
+            costModificationActive = true;
+        }
         
         UUID activatorId = controllerId;
         if ((this instanceof ActivatedAbilityImpl) && ((ActivatedAbilityImpl)this).getActivatorId()!= null) {
@@ -927,5 +937,10 @@ public abstract class AbilityImpl<T extends AbilityImpl<T>> implements Ability {
         return sb.toString();
     }
 
+    @Override
+    public void setCostModificationActive(boolean active) {
+        this.costModificationActive = active;
+    }
+
 }
 
diff --git a/Mage/src/mage/abilities/keyword/FlashbackAbility.java b/Mage/src/mage/abilities/keyword/FlashbackAbility.java
index 550454781e..fb6b4c5957 100644
--- a/Mage/src/mage/abilities/keyword/FlashbackAbility.java
+++ b/Mage/src/mage/abilities/keyword/FlashbackAbility.java
@@ -198,6 +198,7 @@ class FlashbackEffect extends OneShotEffect<FlashbackEffect> {
                     target.setRequired(true);
                 }
                 game.informPlayers(new StringBuilder(controller.getName()).append(" flashbacks ").append(card.getName()).toString());
+                spellAbility.setCostModificationActive(false); // prevents to apply cost modification twice for flashbacked spells
                 return controller.cast(spellAbility, game, true);
             }
         }