From 77382152e76ca8ab3ea324f12ee37831544c469c Mon Sep 17 00:00:00 2001
From: theelk801 <theelk801@gmail.com>
Date: Tue, 11 Apr 2023 20:14:20 -0400
Subject: [PATCH] [USG] rework Veiled Sentry

---
 Mage.Sets/src/mage/cards/v/VeiledSentry.java | 51 +++++++++++---------
 1 file changed, 28 insertions(+), 23 deletions(-)

diff --git a/Mage.Sets/src/mage/cards/v/VeiledSentry.java b/Mage.Sets/src/mage/cards/v/VeiledSentry.java
index bef85b0c6c..33e034f9a5 100644
--- a/Mage.Sets/src/mage/cards/v/VeiledSentry.java
+++ b/Mage.Sets/src/mage/cards/v/VeiledSentry.java
@@ -1,15 +1,14 @@
 package mage.cards.v;
 
 import mage.abilities.Ability;
-import mage.abilities.TriggeredAbility;
 import mage.abilities.common.SpellCastOpponentTriggeredAbility;
+import mage.abilities.condition.Condition;
 import mage.abilities.condition.common.SourceMatchesFilterCondition;
 import mage.abilities.decorator.ConditionalInterveningIfTriggeredAbility;
 import mage.abilities.effects.ContinuousEffectImpl;
 import mage.cards.CardImpl;
 import mage.cards.CardSetInfo;
 import mage.constants.*;
-import mage.filter.FilterSpell;
 import mage.filter.StaticFilters;
 import mage.game.Game;
 import mage.game.permanent.Permanent;
@@ -22,14 +21,17 @@ import java.util.UUID;
  */
 public final class VeiledSentry extends CardImpl {
 
+    private static final Condition condition = new SourceMatchesFilterCondition(StaticFilters.FILTER_PERMANENT_ENCHANTMENT);
+
     public VeiledSentry(UUID ownerId, CardSetInfo setInfo) {
         super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{U}");
 
         // When an opponent casts a spell, if Veiled Sentry is an enchantment, Veiled Sentry becomes an Illusion creature with power and toughness each equal to that spell's converted mana cost.
-        TriggeredAbility ability = new SpellCastOpponentTriggeredAbility(Zone.BATTLEFIELD, new VeiledSentryEffect(), new FilterSpell(), false, SetTargetPointer.SPELL);
-        this.addAbility(new ConditionalInterveningIfTriggeredAbility(ability, new SourceMatchesFilterCondition(StaticFilters.FILTER_PERMANENT_ENCHANTMENT),
-                "Whenever an opponent casts a spell, if Veiled Sentry is an enchantment, Veil Sentry becomes an Illusion creature with power and toughness equal to that spell's mana value."));
-
+        this.addAbility(new ConditionalInterveningIfTriggeredAbility(
+                new SpellCastOpponentTriggeredAbility(new VeiledSentryEffect(), false),
+                condition, "Whenever an opponent casts a spell, if {this} is an enchantment, " +
+                "{this} becomes an Illusion creature with power and toughness equal to that spell's mana value."
+        ));
     }
 
     private VeiledSentry(final VeiledSentry card) {
@@ -44,6 +46,8 @@ public final class VeiledSentry extends CardImpl {
 
 class VeiledSentryEffect extends ContinuousEffectImpl {
 
+    private int spellMV = 0;
+
     public VeiledSentryEffect() {
         super(Duration.Custom, Outcome.BecomeCreature);
         staticText = "{this} becomes an Illusion creature with power and toughness equal to that spell's mana value";
@@ -59,30 +63,31 @@ class VeiledSentryEffect extends ContinuousEffectImpl {
     }
 
     @Override
-    public boolean apply(Layer layer, SubLayer sublayer, Ability source, Game game) {
-        Permanent veiledSentry = game.getPermanent(source.getSourceId());
-        Spell spellCast = game.getSpell(targetPointer.getFirst(game, source));
-        if (spellCast != null) {
-            game.getState().setValue(source + "cmcSpell", spellCast.getManaValue());
+    public void init(Ability source, Game game) {
+        Spell spell = (Spell) getValue("spellCast");
+        if (spell != null) {
+            spellMV = spell.getManaValue();
         }
-        if (veiledSentry == null) {
+    }
+
+    @Override
+    public boolean apply(Layer layer, SubLayer sublayer, Ability source, Game game) {
+        Permanent permanent = source.getSourcePermanentIfItStillExists(game);
+        if (permanent == null) {
+            discard();
             return false;
         }
         switch (layer) {
             case TypeChangingEffects_4:
-                veiledSentry.removeAllCardTypes(game);
-                veiledSentry.removeAllSubTypes(game);
-                veiledSentry.addCardType(game, CardType.CREATURE);
-                veiledSentry.addSubType(game, SubType.ILLUSION);
+                permanent.removeAllCardTypes(game);
+                permanent.removeAllSubTypes(game);
+                permanent.addCardType(game, CardType.CREATURE);
+                permanent.addSubType(game, SubType.ILLUSION);
                 break;
-
             case PTChangingEffects_7:
-                if (game.getState().getValue(source + "cmcSpell") != null) {
-                    int cmc = (int) game.getState().getValue(source + "cmcSpell");
-                    if (sublayer == SubLayer.SetPT_7b) {
-                        veiledSentry.addPower(cmc);
-                        veiledSentry.addToughness(cmc);
-                    }
+                if (sublayer == SubLayer.SetPT_7b) {
+                    permanent.getPower().setModifiedBaseValue(spellMV);
+                    permanent.getToughness().setModifiedBaseValue(spellMV);
                 }
         }
         return true;