From 0335d4d77eabaf7c4dda736921831f9cf0fbac5d Mon Sep 17 00:00:00 2001
From: magenoxx <magenoxx@gmail>
Date: Fri, 25 May 2012 10:01:35 +0400
Subject: [PATCH] Refactored copying permanents

---
 Mage.Sets/src/mage/sets/magic2012/PhantasmalImage.java   | 9 +++++----
 .../src/mage/sets/newphyrexia/PhyrexianMetamorph.java    | 9 +++++----
 Mage/src/mage/abilities/effects/common/CopyEffect.java   | 3 +++
 .../abilities/effects/common/CopyPermanentEffect.java    | 9 +++++----
 Mage/src/mage/game/Game.java                             | 5 +++--
 Mage/src/mage/game/GameImpl.java                         | 8 ++++----
 6 files changed, 25 insertions(+), 18 deletions(-)

diff --git a/Mage.Sets/src/mage/sets/magic2012/PhantasmalImage.java b/Mage.Sets/src/mage/sets/magic2012/PhantasmalImage.java
index 269749a2a8..81fcf0e71f 100644
--- a/Mage.Sets/src/mage/sets/magic2012/PhantasmalImage.java
+++ b/Mage.Sets/src/mage/sets/magic2012/PhantasmalImage.java
@@ -92,16 +92,17 @@ class PhantasmalImageCopyEffect extends OneShotEffect<PhantasmalImageCopyEffect>
     @Override
     public boolean apply(Game game, Ability source) {
 		Player player = game.getPlayer(source.getControllerId());
-		if ( player != null ) {
+        Permanent sourcePermanent = game.getPermanent(source.getSourceId());
+        if (player != null && sourcePermanent != null) {
 			Target target = new TargetPermanent(new FilterCreaturePermanent());
             if (target.canChoose(source.getControllerId(), game)) {
                 target.setRequired(true);
                 target.setNotTarget(true);
                 player.choose(Outcome.Copy, target, source.getSourceId(), game);
                 UUID targetId = target.getFirstTarget();
-                Permanent permanent = game.getPermanent(targetId);
-                if (permanent != null) {
-                    game.copyPermanent(permanent, source, new ApplyToPermanent() {
+                Permanent copyFromPermanent = game.getPermanent(targetId);
+                if (copyFromPermanent != null) {
+                    game.copyPermanent(copyFromPermanent, sourcePermanent, source, new ApplyToPermanent() {
                         @Override
                         public Boolean apply(Game game, Permanent permanent) {
                             permanent.getSubtype().add("Illusion");
diff --git a/Mage.Sets/src/mage/sets/newphyrexia/PhyrexianMetamorph.java b/Mage.Sets/src/mage/sets/newphyrexia/PhyrexianMetamorph.java
index 28a394040d..c3bb831dca 100644
--- a/Mage.Sets/src/mage/sets/newphyrexia/PhyrexianMetamorph.java
+++ b/Mage.Sets/src/mage/sets/newphyrexia/PhyrexianMetamorph.java
@@ -97,13 +97,14 @@ class PhyrexianMetamorphEffect extends OneShotEffect<PhyrexianMetamorphEffect> {
     @Override
     public boolean apply(Game game, Ability source) {
         Player player = game.getPlayer(source.getControllerId());
-        if (player != null) {
+        Permanent sourcePermanent = game.getPermanent(source.getSourceId());
+        if (player != null && sourcePermanent != null) {
             Target target = new TargetPermanent(filter);
             if (target.canChoose(source.getControllerId(), game)) {
                 player.choose(Outcome.Copy, target, source.getSourceId(), game);
-                Permanent perm = game.getPermanent(target.getFirstTarget());
-                if (perm != null) {
-                    game.copyPermanent(perm, source, new ApplyToPermanent() {
+                Permanent copyFromPermanent = game.getPermanent(target.getFirstTarget());
+                if (copyFromPermanent != null) {
+                    game.copyPermanent(copyFromPermanent, sourcePermanent, source, new ApplyToPermanent() {
                         @Override
                         public Boolean apply(Game game, Permanent permanent) {
                             if (!permanent.getCardType().contains(CardType.ARTIFACT)) {
diff --git a/Mage/src/mage/abilities/effects/common/CopyEffect.java b/Mage/src/mage/abilities/effects/common/CopyEffect.java
index 67f71f3cdf..e93ef317df 100644
--- a/Mage/src/mage/abilities/effects/common/CopyEffect.java
+++ b/Mage/src/mage/abilities/effects/common/CopyEffect.java
@@ -47,6 +47,9 @@ import java.util.UUID;
  */
 public class CopyEffect extends ContinuousEffectImpl<CopyEffect> {
 
+    /**
+     * Object we copy from
+     */
     private MageObject target;
     private UUID sourceId;
     
diff --git a/Mage/src/mage/abilities/effects/common/CopyPermanentEffect.java b/Mage/src/mage/abilities/effects/common/CopyPermanentEffect.java
index 41899c2724..a3cb788fb1 100644
--- a/Mage/src/mage/abilities/effects/common/CopyPermanentEffect.java
+++ b/Mage/src/mage/abilities/effects/common/CopyPermanentEffect.java
@@ -66,13 +66,14 @@ public class CopyPermanentEffect extends OneShotEffect<CopyPermanentEffect> {
     @Override
     public boolean apply(Game game, Ability source) {
         Player player = game.getPlayer(source.getControllerId());
-        if (player != null) {
+        Permanent sourcePermanent = game.getPermanent(source.getSourceId());
+        if (player != null && sourcePermanent != null) {
             Target target = new TargetPermanent(filter);
             if (target.canChoose(source.getControllerId(), game)) {
                 player.choose(Outcome.Copy, target, source.getSourceId(), game);
-                Permanent perm = game.getPermanent(target.getFirstTarget());
-                if (perm != null) {
-                    game.copyPermanent(perm, source, new ApplyToPermanent() {
+                Permanent copyFromPermanent = game.getPermanent(target.getFirstTarget());
+                if (copyFromPermanent != null) {
+                    game.copyPermanent(copyFromPermanent, sourcePermanent, source, new ApplyToPermanent() {
                         @Override
                         public Boolean apply(Game game, Permanent permanent) {
                             if (!permanent.getCardType().contains(Constants.CardType.ARTIFACT)) {
diff --git a/Mage/src/mage/game/Game.java b/Mage/src/mage/game/Game.java
index 87157d80b7..db1982f747 100644
--- a/Mage/src/mage/game/Game.java
+++ b/Mage/src/mage/game/Game.java
@@ -164,11 +164,12 @@ public interface Game extends MageItem, Serializable {
     /**
      * This version supports copying of copies of any depth.
      *
-     * @param targetPermanent
+     * @param copyFromPermanent
+     * @param copyToPermanent
      * @param source
      * @param applier
      */
-    public void copyPermanent(Permanent targetPermanent, Ability source, ApplyToPermanent applier);
+    public void copyPermanent(Permanent copyFromPermanent, Permanent copyToPermanent, Ability source, ApplyToPermanent applier);
 
     public void addTriggeredAbility(TriggeredAbility ability);
 	public void addDelayedTriggeredAbility(DelayedTriggeredAbility delayedAbility);
diff --git a/Mage/src/mage/game/GameImpl.java b/Mage/src/mage/game/GameImpl.java
index 0f362f2291..89badd282f 100644
--- a/Mage/src/mage/game/GameImpl.java
+++ b/Mage/src/mage/game/GameImpl.java
@@ -736,8 +736,8 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
 	}
 
     @Override
-    public void copyPermanent(Permanent targetPermanent, Ability source, ApplyToPermanent applier) {
-        Permanent permanent = targetPermanent.copy();
+    public void copyPermanent(Permanent copyFromPermanent, Permanent copyToPermanent, Ability source, ApplyToPermanent applier) {
+        Permanent permanent = copyFromPermanent.copy();
         //getState().addCard(permanent);
         permanent.reset(this);
         permanent.assignNewId();
@@ -745,7 +745,7 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
 
         Ability newAbility = source.copy();
 
-        CopyEffect newEffect = new CopyEffect(permanent, source.getSourceId());
+        CopyEffect newEffect = new CopyEffect(permanent, copyToPermanent.getId());
         newEffect.newId();
         newEffect.setTimestamp();
         newEffect.init(newAbility, this);
@@ -755,7 +755,7 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
             if (effect instanceof CopyEffect) {
                 CopyEffect copyEffect = (CopyEffect) effect;
                 // there is another copy effect that our targetPermanent copies stats from
-                if (copyEffect.getSourceId().equals(targetPermanent.getId())) {
+                if (copyEffect.getSourceId().equals(copyFromPermanent.getId())) {
                     MageObject object = ((CopyEffect) effect).getTarget();
                     if (object instanceof Permanent) {
                         // so we will use original card instead of target