From 34298e78f596df77ee314e2eda9cc04485805247 Mon Sep 17 00:00:00 2001
From: LevelX2 <ludwig.hirth@online.de>
Date: Fri, 26 Dec 2014 21:08:05 +0100
Subject: [PATCH] * Fixed a (probably recently unintentionally added) problem
 with activated abilities of tokens.

---
 .../commander2014/FreyaliseLlanowarsFury.java |  4 +-
 .../mage/test/token/ActivatedAbilityTest.java | 62 +++++++++++++++++++
 .../effects/common/CreateTokenEffect.java     |  8 +--
 .../common/RegenerateTargetEffect.java        | 14 ++++-
 .../mage/game/permanent/PermanentToken.java   | 13 ++--
 5 files changed, 85 insertions(+), 16 deletions(-)
 create mode 100644 Mage.Tests/src/test/java/org/mage/test/token/ActivatedAbilityTest.java

diff --git a/Mage.Sets/src/mage/sets/commander2014/FreyaliseLlanowarsFury.java b/Mage.Sets/src/mage/sets/commander2014/FreyaliseLlanowarsFury.java
index dd50d94fad..101392e2fb 100644
--- a/Mage.Sets/src/mage/sets/commander2014/FreyaliseLlanowarsFury.java
+++ b/Mage.Sets/src/mage/sets/commander2014/FreyaliseLlanowarsFury.java
@@ -76,7 +76,7 @@ public class FreyaliseLlanowarsFury extends CardImpl {
 
         this.addAbility(new EntersBattlefieldAbility(new AddCountersSourceEffect(CounterType.LOYALTY.createInstance(3)), false));
         
-        // +2: Put a 1/1 green Elf Druid creature token onto the battlefield with "{tap}: Add {G} to your mana pool."
+        // +2: Put a 1/1 green Elf Druid creature token onto the battlefield with "{T}: Add {G} to your mana pool."
         this.addAbility(new LoyaltyAbility(new CreateTokenEffect(new FreyaliseLlanowarsFuryToken()), 2));
         // -2: Destroy target artifact or enchantment.
         LoyaltyAbility loyaltyAbility = new LoyaltyAbility(new DestroyTargetEffect(), -2);
@@ -103,7 +103,7 @@ public class FreyaliseLlanowarsFury extends CardImpl {
 class FreyaliseLlanowarsFuryToken extends Token {
 
     FreyaliseLlanowarsFuryToken() {
-        super("Elf Druid", "1/1 green Elf Druid creature token onto the battlefield with \"{t}: Add {G} to your mana pool.\"");
+        super("Elf Druid", "1/1 green Elf Druid creature token with \"{t}: Add {G} to your mana pool.\"");
         this.setOriginalExpansionSetCode("C14");
         this.cardType.add(CardType.CREATURE);
         this.color = ObjectColor.GREEN;
diff --git a/Mage.Tests/src/test/java/org/mage/test/token/ActivatedAbilityTest.java b/Mage.Tests/src/test/java/org/mage/test/token/ActivatedAbilityTest.java
new file mode 100644
index 0000000000..f01088166d
--- /dev/null
+++ b/Mage.Tests/src/test/java/org/mage/test/token/ActivatedAbilityTest.java
@@ -0,0 +1,62 @@
+/*
+ *  Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved.
+ *
+ *  Redistribution and use in source and binary forms, with or without modification, are
+ *  permitted provided that the following conditions are met:
+ *
+ *     1. Redistributions of source code must retain the above copyright notice, this list of
+ *        conditions and the following disclaimer.
+ *
+ *     2. Redistributions in binary form must reproduce the above copyright notice, this list
+ *        of conditions and the following disclaimer in the documentation and/or other materials
+ *        provided with the distribution.
+ *
+ *  THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ *  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR
+ *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ *  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *  The views and conclusions contained in the software and documentation are those of the
+ *  authors and should not be interpreted as representing official policies, either expressed
+ *  or implied, of BetaSteward_at_googlemail.com.
+ */
+package org.mage.test.token;
+
+import mage.constants.ManaType;
+import mage.constants.PhaseStep;
+import mage.constants.Zone;
+import org.junit.Assert;
+import org.junit.Test;
+import org.mage.test.serverside.base.CardTestPlayerBase;
+
+/**
+ *
+ * @author LevelX2
+ */
+public class ActivatedAbilityTest extends CardTestPlayerBase {
+    /**
+     * Check that activated ability of created token works
+     */
+    @Test
+    public void testActivatedManaAbility() {
+        // Green mana doesn't empty from your mana pool as steps and phases end.
+        // Omnath, Locus of Mana gets +1/+1 for each green mana in your mana pool.
+        addCard(Zone.BATTLEFIELD, playerA, "Omnath, Locus of Mana", 1);
+
+        addCard(Zone.BATTLEFIELD, playerA, "Freyalise, Llanowar's Fury");
+        activateAbility(1, PhaseStep.PRECOMBAT_MAIN, playerA, "+2: Put a 1/1 green Elf Druid creature token with \"{t}: Add {G} to your mana pool.\" onto the battlefield.");
+        activateAbility(3, PhaseStep.PRECOMBAT_MAIN, playerA, "+2: Put a 1/1 green Elf Druid creature token with \"{t}: Add {G} to your mana pool.\" onto the battlefield.");
+        activateManaAbility(3, PhaseStep.PRECOMBAT_MAIN, playerA, "{T}: Add {G} to your mana pool.");
+        setStopAt(3, PhaseStep.PRECOMBAT_MAIN);
+        execute();
+
+        assertPermanentCount(playerA, "Elf Druid", 2);
+        assertPermanentCount(playerA, "Freyalise, Llanowar's Fury", 1);
+        Assert.assertEquals("one green mana has to be in the mana pool",1,playerA.getManaPool().get(ManaType.GREEN));
+    }
+}
diff --git a/Mage/src/mage/abilities/effects/common/CreateTokenEffect.java b/Mage/src/mage/abilities/effects/common/CreateTokenEffect.java
index 69c00491c6..ee32ab0e86 100644
--- a/Mage/src/mage/abilities/effects/common/CreateTokenEffect.java
+++ b/Mage/src/mage/abilities/effects/common/CreateTokenEffect.java
@@ -95,11 +95,9 @@ public class CreateTokenEffect extends OneShotEffect {
     @Override
     public boolean apply(Game game, Ability source) {
         int value = amount.calculate(game, source, this);
-        Token tokenCopy = token.copy();
-        tokenCopy.getAbilities().newId(); // neccessary if token has ability like DevourAbility()
-        tokenCopy.putOntoBattlefield(value, game, source.getSourceId(), source.getControllerId(), tapped, attacking);
-        this.lastAddedTokenId = tokenCopy.getLastAddedToken();
-        this.lastAddedTokenIds = tokenCopy.getLastAddedTokenIds();
+        token.putOntoBattlefield(value, game, source.getSourceId(), source.getControllerId(), tapped, attacking);
+        this.lastAddedTokenId = token.getLastAddedToken();
+        this.lastAddedTokenIds = token.getLastAddedTokenIds();
         return true;
     }
 
diff --git a/Mage/src/mage/abilities/effects/common/RegenerateTargetEffect.java b/Mage/src/mage/abilities/effects/common/RegenerateTargetEffect.java
index dfd2cf2ffe..2a14b1925a 100644
--- a/Mage/src/mage/abilities/effects/common/RegenerateTargetEffect.java
+++ b/Mage/src/mage/abilities/effects/common/RegenerateTargetEffect.java
@@ -37,6 +37,7 @@ import mage.game.Game;
 import mage.game.events.GameEvent;
 import mage.game.events.GameEvent.EventType;
 import mage.game.permanent.Permanent;
+import mage.target.Target;
 
 /**
  *
@@ -89,10 +90,17 @@ public class RegenerateTargetEffect  extends ReplacementEffectImpl {
 
     @Override
     public String getText(Mode mode) {
+        if (staticText != null && !staticText.isEmpty()) {
+            return staticText;
+        }
         StringBuilder sb = new StringBuilder();
-        sb.append("Regenerate target ");
-        if ( mode != null ) {
-          sb.append(mode.getTargets().get(0).getTargetName());
+        sb.append("Regenerate ");
+        Target target = mode.getTargets().get(0);
+        if (target != null) {
+            if (!target.getTargetName().toLowerCase().startsWith("another")) {
+                sb.append("target ");
+            }
+            sb.append(target.getTargetName());
         }
         return sb.toString();
     }
diff --git a/Mage/src/mage/game/permanent/PermanentToken.java b/Mage/src/mage/game/permanent/PermanentToken.java
index 8351ce0c0d..47cee3f07c 100644
--- a/Mage/src/mage/game/permanent/PermanentToken.java
+++ b/Mage/src/mage/game/permanent/PermanentToken.java
@@ -29,14 +29,13 @@
 package mage.game.permanent;
 
 import java.util.UUID;
-import mage.constants.Zone;
 import mage.abilities.Ability;
 import mage.abilities.costs.mana.ManaCost;
+import mage.constants.Zone;
 import mage.game.Game;
 import mage.game.events.ZoneChangeEvent;
 import mage.game.permanent.token.Token;
 
-
 /**
  *
  * @author BetaSteward_at_googlemail.com
@@ -48,8 +47,10 @@ public class PermanentToken extends PermanentImpl {
     public PermanentToken(Token token, UUID controllerId, String expansionSetCode, Game game) {
         super(controllerId, controllerId, token.getName());
         this.expansionSetCode = expansionSetCode;
-        this.token = token;
-        this.copyFromToken(this.token, game, false); // needed to have e.g. subtypes for entersTheBattlefield replacement effects
+        this.token = token.copy();
+        this.token.getAbilities().newId(); // neccessary if token has ability like DevourAbility()
+        this.token.getAbilities().setSourceId(objectId);
+        this.copyFromToken(this.token, game, false); // needed to have at this time (e.g. for subtypes for entersTheBattlefield replacement effects)
     }
 
     public PermanentToken(final PermanentToken permanent) {
@@ -60,8 +61,7 @@ public class PermanentToken extends PermanentImpl {
 
     @Override
     public void reset(Game game) {
-        Token tokenCopy = token.copy();
-        copyFromToken(tokenCopy, game, true);
+        copyFromToken(token, game, true);
         super.reset(game);
     }
 
@@ -71,6 +71,7 @@ public class PermanentToken extends PermanentImpl {
         if (reset) {
             this.abilities.addAll(token.getAbilities());
         } else {
+            // first time -> create ContinuousEffects only once
             for (Ability ability : token.getAbilities()) {
                 this.addAbility(ability, game);
             }