From 6c93ce9c27d874a1df36693570318bdeae9c46ff Mon Sep 17 00:00:00 2001
From: LevelX2 <ludwig.hirth@online.de>
Date: Sun, 24 Dec 2017 11:08:49 +0100
Subject: [PATCH] * Mox Diamond - Fixed that the handling of token copys (e.g.
 by Mirrorworks) did not work correctly.

---
 Mage.Sets/src/mage/cards/m/MoxDiamond.java    | 38 +++-----
 .../mage/test/cards/copy/MirrorworksTest.java | 94 +++++++++++++++++++
 2 files changed, 109 insertions(+), 23 deletions(-)
 create mode 100644 Mage.Tests/src/test/java/org/mage/test/cards/copy/MirrorworksTest.java

diff --git a/Mage.Sets/src/mage/cards/m/MoxDiamond.java b/Mage.Sets/src/mage/cards/m/MoxDiamond.java
index 8049ee9423..adc3c8950c 100644
--- a/Mage.Sets/src/mage/cards/m/MoxDiamond.java
+++ b/Mage.Sets/src/mage/cards/m/MoxDiamond.java
@@ -34,7 +34,6 @@ import mage.abilities.costs.Cost;
 import mage.abilities.costs.common.DiscardTargetCost;
 import mage.abilities.effects.ReplacementEffectImpl;
 import mage.abilities.mana.AnyColorManaAbility;
-import mage.cards.Card;
 import mage.cards.CardImpl;
 import mage.cards.CardSetInfo;
 import mage.constants.CardType;
@@ -44,7 +43,7 @@ import mage.constants.Zone;
 import mage.filter.common.FilterLandCard;
 import mage.game.Game;
 import mage.game.events.GameEvent;
-import mage.game.events.ZoneChangeEvent;
+import mage.game.permanent.Permanent;
 import mage.players.Player;
 import mage.target.common.TargetCardInHand;
 
@@ -55,7 +54,7 @@ import mage.target.common.TargetCardInHand;
 public class MoxDiamond extends CardImpl {
 
     public MoxDiamond(UUID ownerId, CardSetInfo setInfo) {
-        super(ownerId,setInfo,new CardType[]{CardType.ARTIFACT},"{0}");
+        super(ownerId, setInfo, new CardType[]{CardType.ARTIFACT}, "{0}");
 
         // If Mox Diamond would enter the battlefield, you may discard a land card instead. If you do, put Mox Diamond onto the battlefield. If you don't, put it into its owner's graveyard.
         this.addAbility(new SimpleStaticAbility(Zone.ALL, new MoxDiamondReplacementEffect()));
@@ -95,43 +94,36 @@ class MoxDiamondReplacementEffect extends ReplacementEffectImpl {
     }
 
     @Override
-    public boolean replaceEvent(GameEvent event, Ability source, Game game) {        
-        Player player = game.getPlayer(source.getControllerId());        
-        if (player != null){
+    public boolean replaceEvent(GameEvent event, Ability source, Game game) {
+        Player player = game.getPlayer(source.getControllerId());
+        if (player != null) {
             TargetCardInHand target = new TargetCardInHand(new FilterLandCard());
             Cost cost = new DiscardTargetCost(target);
-            if (cost.canPay(source, source.getSourceId(), source.getControllerId(), game) &&
-                    player.chooseUse(outcome, "Discard land? (Otherwise Mox Diamond goes to graveyard)", source, game) &&
-                    player.chooseTarget(Outcome.Discard, target, source, game)){
+            if (cost.canPay(source, source.getSourceId(), source.getControllerId(), game)
+                    && player.chooseUse(outcome, "Discard land? (Otherwise Mox Diamond goes to graveyard)", source, game)
+                    && player.chooseTarget(Outcome.Discard, target, source, game)) {
                 player.discard(game.getCard(target.getFirstTarget()), source, game);
                 return false;
-            }
-            else{
-                Card card = game.getCard(event.getTargetId());
-                if (card != null) {
-                    player.moveCards(card, Zone.GRAVEYARD, source, game);
+            } else {
+                Permanent permanent = game.getPermanent(event.getTargetId());
+                if (permanent != null) {
+                    player.moveCards(permanent, Zone.GRAVEYARD, source, game);
                 }
                 return true;
             }
-            
+
         }
         return false;
     }
 
     @Override
     public boolean checksEventType(GameEvent event, Game game) {
-        return event.getType() == GameEvent.EventType.ZONE_CHANGE;
+        return event.getType() == GameEvent.EventType.ENTERS_THE_BATTLEFIELD;
     }
 
     @Override
     public boolean applies(GameEvent event, Ability source, Game game) {
-        if (source.getSourceId().equals(event.getTargetId())) {
-            ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
-            if(zEvent.getToZone() == Zone.BATTLEFIELD){
-                return true;
-            }
-        }
-        return false;
+        return source.getSourceId().equals(event.getTargetId());
     }
 
 }
diff --git a/Mage.Tests/src/test/java/org/mage/test/cards/copy/MirrorworksTest.java b/Mage.Tests/src/test/java/org/mage/test/cards/copy/MirrorworksTest.java
new file mode 100644
index 0000000000..792a371bc9
--- /dev/null
+++ b/Mage.Tests/src/test/java/org/mage/test/cards/copy/MirrorworksTest.java
@@ -0,0 +1,94 @@
+/*
+ *  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.cards.copy;
+
+import mage.constants.PhaseStep;
+import mage.constants.Zone;
+import org.junit.Test;
+import org.mage.test.serverside.base.CardTestPlayerBase;
+
+/**
+ *
+ * @author LevelX2
+ */
+public class MirrorworksTest extends CardTestPlayerBase {
+
+    /**
+     * If you play Mox Diamond, with Mirrorworks in play, and create a token
+     * copy, and you have no lands in hand, the Mox will enter the battlefield
+     * as usual instead of the graveyard.
+     */
+    @Test
+    public void TestCopyWithoutLand() {
+        addCard(Zone.HAND, playerA, "Mountain", 1);
+        addCard(Zone.BATTLEFIELD, playerA, "Island", 2);
+
+        // If Mox Diamond would enter the battlefield, you may discard a land card instead. If you do, put Mox Diamond onto the battlefield. If you don't, put it into its owner's graveyard.
+        // {T}: Add one mana of any color to your mana pool.
+        addCard(Zone.HAND, playerA, "Mox Diamond", 1); // Artifact {0}
+
+        // Whenever another nontoken artifact enters the battlefield under your control, you may pay {2}.
+        // If you do, create a token that's a copy of that artifact.
+        addCard(Zone.BATTLEFIELD, playerA, "Mirrorworks", 1); // Artifact {5}
+
+        castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Mox Diamond");
+        setChoice(playerA, "Yes");
+
+        setStopAt(1, PhaseStep.BEGIN_COMBAT);
+        execute();
+
+        assertPermanentCount(playerA, "Mox Diamond", 1);
+        assertTappedCount("Island", true, 2);
+        assertGraveyardCount(playerA, "Mountain", 1);
+
+    }
+
+    @Test
+    public void TestCorrectCopy() {
+        addCard(Zone.HAND, playerA, "Mountain", 2);
+        addCard(Zone.BATTLEFIELD, playerA, "Island", 2);
+
+        // If Mox Diamond would enter the battlefield, you may discard a land card instead. If you do, put Mox Diamond onto the battlefield. If you don't, put it into its owner's graveyard.
+        // {T}: Add one mana of any color to your mana pool.
+        addCard(Zone.HAND, playerA, "Mox Diamond", 1); // Artifact {0}
+
+        // Whenever another nontoken artifact enters the battlefield under your control, you may pay {2}.
+        // If you do, create a token that's a copy of that artifact.
+        addCard(Zone.BATTLEFIELD, playerA, "Mirrorworks", 1); // Artifact {5}
+
+        castSpell(1, PhaseStep.PRECOMBAT_MAIN, playerA, "Mox Diamond");
+        setChoice(playerA, "Yes");
+
+        setStopAt(1, PhaseStep.BEGIN_COMBAT);
+        execute();
+
+        assertPermanentCount(playerA, "Mox Diamond", 2);
+        assertGraveyardCount(playerA, "Mountain", 2);
+
+    }
+}