From d226b30592cf81a61209f489da175cb38aa870b9 Mon Sep 17 00:00:00 2001 From: xenohedron Date: Sun, 2 Jun 2024 19:39:58 -0400 Subject: [PATCH] avoid casting to Card with dedicated method getSourceCardIfItStillExists --- .../mage/cards/a/AclazotzDeepestBetrayal.java | 11 +++--- .../src/mage/cards/b/BloodfeatherPhoenix.java | 9 ++--- Mage.Sets/src/mage/cards/b/Bookwurm.java | 10 ++---- Mage.Sets/src/mage/cards/c/CustodyBattle.java | 9 +++-- .../mage/cards/d/DarigaazReincarnated.java | 16 ++++----- .../src/mage/cards/d/DeathmistRaptor.java | 8 ++--- Mage.Sets/src/mage/cards/d/DragonBreath.java | 2 +- Mage.Sets/src/mage/cards/d/DragonFangs.java | 2 +- Mage.Sets/src/mage/cards/d/DragonScales.java | 2 +- Mage.Sets/src/mage/cards/d/DragonShadow.java | 2 +- Mage.Sets/src/mage/cards/d/DragonWings.java | 2 +- .../src/mage/cards/e/EdgarCharmedGroom.java | 9 ++--- Mage.Sets/src/mage/cards/e/EnigmaSphinx.java | 2 +- Mage.Sets/src/mage/cards/f/Flameskull.java | 6 ++-- Mage.Sets/src/mage/cards/h/HarvestHand.java | 9 ++--- .../src/mage/cards/h/HauntingImitation.java | 6 ++-- .../src/mage/cards/m/MoorlandRescuer.java | 2 +- Mage.Sets/src/mage/cards/n/NextOfKin.java | 2 +- .../mage/cards/o/OjerAxonilDeepestMight.java | 9 ++--- .../mage/cards/o/OjerKaslemDeepestGrowth.java | 9 ++--- .../cards/o/OjerPakpatiqDeepestEpoch.java | 11 +++--- .../cards/o/OjerTaqDeepestFoundation.java | 11 +++--- .../src/mage/cards/r/RetrieverPhoenix.java | 8 ++--- .../src/mage/cards/s/ScreamingSwarm.java | 6 ++-- Mage.Sets/src/mage/cards/s/SmokeShroud.java | 2 +- .../src/mage/cards/s/SproutbackTrudge.java | 5 ++- Mage.Sets/src/mage/cards/s/StartledAwake.java | 5 ++- .../mage/cards/t/TreacherousPitDweller.java | 7 ++-- .../mage/cards/u/UvildaDeanOfPerfection.java | 13 ++++--- .../src/main/java/mage/abilities/Ability.java | 10 ++++++ .../main/java/mage/abilities/AbilityImpl.java | 10 ++++++ .../costs/common/ExileSourceCost.java | 2 +- .../effects/common/ExileSourceEffect.java | 35 ++++++++---------- .../common/PutOnLibrarySourceEffect.java | 8 ++--- ...ReturnSourceFromGraveyardToHandEffect.java | 6 ++-- .../effects/keyword/AdaptEffect.java | 12 +------ .../effects/keyword/CompanionEffect.java | 2 +- .../abilities/keyword/FabricateAbility.java | 6 ++-- .../abilities/keyword/MeditateAbility.java | 36 ++++++++----------- .../java/mage/game/stack/StackAbility.java | 6 ++++ 40 files changed, 150 insertions(+), 178 deletions(-) diff --git a/Mage.Sets/src/mage/cards/a/AclazotzDeepestBetrayal.java b/Mage.Sets/src/mage/cards/a/AclazotzDeepestBetrayal.java index 670107a87da..06c22233f9f 100644 --- a/Mage.Sets/src/mage/cards/a/AclazotzDeepestBetrayal.java +++ b/Mage.Sets/src/mage/cards/a/AclazotzDeepestBetrayal.java @@ -170,15 +170,12 @@ class AclazotzDeepestBetrayalTransformEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller == null) { - return false; - } - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (!(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } game.getState().setValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + source.getSourceId(), Boolean.TRUE); - controller.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game, true, false, true, null); + controller.moveCards(card, Zone.BATTLEFIELD, source, game, true, false, true, null); return true; } -} \ No newline at end of file +} diff --git a/Mage.Sets/src/mage/cards/b/BloodfeatherPhoenix.java b/Mage.Sets/src/mage/cards/b/BloodfeatherPhoenix.java index 9a7aa1cc778..b8b75dcb561 100644 --- a/Mage.Sets/src/mage/cards/b/BloodfeatherPhoenix.java +++ b/Mage.Sets/src/mage/cards/b/BloodfeatherPhoenix.java @@ -11,6 +11,7 @@ import mage.abilities.effects.common.DoIfCostPaid; import mage.abilities.effects.common.continuous.GainAbilityTargetEffect; import mage.abilities.keyword.FlyingAbility; import mage.abilities.keyword.HasteAbility; +import mage.cards.Card; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; @@ -112,12 +113,12 @@ class BloodfeatherPhoenixEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Player player = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (player == null || sourceObject == null) { + Player controller = game.getPlayer(source.getControllerId()); + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } - player.moveCards(game.getCard(sourceObject.getId()), Zone.BATTLEFIELD, source, game); + controller.moveCards(card, Zone.BATTLEFIELD, source, game); if (game.getPermanent(source.getSourceId()) != null) { game.addEffect(new GainAbilityTargetEffect(HasteAbility.getInstance()) .setTargetPointer(new FixedTarget(source.getSourceId(), game)), source); diff --git a/Mage.Sets/src/mage/cards/b/Bookwurm.java b/Mage.Sets/src/mage/cards/b/Bookwurm.java index 4fc276c6680..4d70fc06eb1 100644 --- a/Mage.Sets/src/mage/cards/b/Bookwurm.java +++ b/Mage.Sets/src/mage/cards/b/Bookwurm.java @@ -1,7 +1,6 @@ package mage.cards.b; import mage.MageInt; -import mage.MageObject; import mage.abilities.Ability; import mage.abilities.common.EntersBattlefieldTriggeredAbility; import mage.abilities.common.SimpleActivatedAbility; @@ -75,11 +74,8 @@ class BookwurmEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player player = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - return player != null - && sourceObject instanceof Card - && player.putCardOnTopXOfLibrary( - (Card) sourceObject, game, source, 3, true - ); + Card card = source.getSourceCardIfItStillExists(game); + return player != null && card != null + && player.putCardOnTopXOfLibrary(card, game, source, 3, true); } } diff --git a/Mage.Sets/src/mage/cards/c/CustodyBattle.java b/Mage.Sets/src/mage/cards/c/CustodyBattle.java index 90bb279fdb5..859f072f82e 100644 --- a/Mage.Sets/src/mage/cards/c/CustodyBattle.java +++ b/Mage.Sets/src/mage/cards/c/CustodyBattle.java @@ -92,10 +92,9 @@ class GiveControlEffect extends ContinuousEffectImpl { @Override public boolean apply(Game game, Ability source) { - MageObject mageObject = source.getSourceObjectIfItStillExists(game); - if (mageObject != null - && mageObject instanceof Permanent) { - return ((Permanent) mageObject).changeControllerId(source.getFirstTarget(), game, source); + Permanent permanent = source.getSourcePermanentIfItStillExists(game); + if (permanent != null) { + return permanent.changeControllerId(source.getFirstTarget(), game, source); } else { discard(); } @@ -108,7 +107,7 @@ class CustodyBattleUnlessPaysEffect extends OneShotEffect { protected Cost cost; - public CustodyBattleUnlessPaysEffect(Cost cost) { + CustodyBattleUnlessPaysEffect(Cost cost) { super(Outcome.Sacrifice); staticText = "target opponent gains control of {this} unless you sacrifice a land"; this.cost = cost; diff --git a/Mage.Sets/src/mage/cards/d/DarigaazReincarnated.java b/Mage.Sets/src/mage/cards/d/DarigaazReincarnated.java index 2f84a61d7da..b54f202098b 100644 --- a/Mage.Sets/src/mage/cards/d/DarigaazReincarnated.java +++ b/Mage.Sets/src/mage/cards/d/DarigaazReincarnated.java @@ -147,19 +147,15 @@ class DarigaazReincarnatedReturnEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller == null) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (sourceObject instanceof Card) { - Card card = (Card) sourceObject; - new RemoveCounterSourceEffect(CounterType.EGG.createInstance()).apply(game, source); - if (card.getCounters(game).getCount(CounterType.EGG) == 0) { - controller.moveCards(card, Zone.BATTLEFIELD, source, game); - } - return true; + new RemoveCounterSourceEffect(CounterType.EGG.createInstance()).apply(game, source); + if (card.getCounters(game).getCount(CounterType.EGG) == 0) { + controller.moveCards(card, Zone.BATTLEFIELD, source, game); } - return false; + return true; } } diff --git a/Mage.Sets/src/mage/cards/d/DeathmistRaptor.java b/Mage.Sets/src/mage/cards/d/DeathmistRaptor.java index 80aa5c67bb3..3726d9e2150 100644 --- a/Mage.Sets/src/mage/cards/d/DeathmistRaptor.java +++ b/Mage.Sets/src/mage/cards/d/DeathmistRaptor.java @@ -73,10 +73,10 @@ class DeathmistRaptorEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (controller != null && (sourceObject instanceof Card)) { - return controller.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game, false, - controller.chooseUse(Outcome.Detriment, "Return " + sourceObject.getLogName() + " face down to battlefield (otherwise face up)?", source, game), + Card card = source.getSourceCardIfItStillExists(game); + if (controller != null && card != null) { + return controller.moveCards(card, Zone.BATTLEFIELD, source, game, false, + controller.chooseUse(Outcome.Detriment, "Return " + card.getLogName() + " face down to battlefield (otherwise face up)?", source, game), false, null); } return false; diff --git a/Mage.Sets/src/mage/cards/d/DragonBreath.java b/Mage.Sets/src/mage/cards/d/DragonBreath.java index 6a575692a64..3964d3b2349 100644 --- a/Mage.Sets/src/mage/cards/d/DragonBreath.java +++ b/Mage.Sets/src/mage/cards/d/DragonBreath.java @@ -85,7 +85,7 @@ class DragonBreathEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Card sourceCard = (Card) source.getSourceObjectIfItStillExists(game); + Card sourceCard = source.getSourceCardIfItStillExists(game); Permanent permanent = game.getPermanent(this.getTargetPointer().getFirst(game, source)); Player controller = game.getPlayer(source.getControllerId()); if (sourceCard != null && permanent != null && controller != null) { diff --git a/Mage.Sets/src/mage/cards/d/DragonFangs.java b/Mage.Sets/src/mage/cards/d/DragonFangs.java index 21fc5f98653..5aed726aa94 100644 --- a/Mage.Sets/src/mage/cards/d/DragonFangs.java +++ b/Mage.Sets/src/mage/cards/d/DragonFangs.java @@ -81,7 +81,7 @@ class DragonFangsEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Card sourceCard = (Card) source.getSourceObjectIfItStillExists(game); + Card sourceCard = source.getSourceCardIfItStillExists(game); Permanent permanent = game.getPermanent(this.getTargetPointer().getFirst(game, source)); Player controller = game.getPlayer(source.getControllerId()); if (sourceCard != null && permanent != null && controller != null) { diff --git a/Mage.Sets/src/mage/cards/d/DragonScales.java b/Mage.Sets/src/mage/cards/d/DragonScales.java index a61065ce8e4..4a90cddd5c3 100644 --- a/Mage.Sets/src/mage/cards/d/DragonScales.java +++ b/Mage.Sets/src/mage/cards/d/DragonScales.java @@ -81,7 +81,7 @@ class DragonScalesEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Card sourceCard = (Card) source.getSourceObjectIfItStillExists(game); + Card sourceCard = source.getSourceCardIfItStillExists(game); Permanent permanent = game.getPermanent(this.getTargetPointer().getFirst(game, source)); Player controller = game.getPlayer(source.getControllerId()); if (sourceCard != null && permanent != null && controller != null) { diff --git a/Mage.Sets/src/mage/cards/d/DragonShadow.java b/Mage.Sets/src/mage/cards/d/DragonShadow.java index 28b42a6864c..7f344cb81c4 100644 --- a/Mage.Sets/src/mage/cards/d/DragonShadow.java +++ b/Mage.Sets/src/mage/cards/d/DragonShadow.java @@ -81,7 +81,7 @@ class DragonShadowEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Card sourceCard = (Card) source.getSourceObjectIfItStillExists(game); + Card sourceCard = source.getSourceCardIfItStillExists(game); Permanent permanent = game.getPermanent(this.getTargetPointer().getFirst(game, source)); Player controller = game.getPlayer(source.getControllerId()); if (sourceCard != null && permanent != null && controller != null) { diff --git a/Mage.Sets/src/mage/cards/d/DragonWings.java b/Mage.Sets/src/mage/cards/d/DragonWings.java index 35795e6a119..a41c58ccf14 100644 --- a/Mage.Sets/src/mage/cards/d/DragonWings.java +++ b/Mage.Sets/src/mage/cards/d/DragonWings.java @@ -83,7 +83,7 @@ class DragonWingsEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Card sourceCard = (Card) source.getSourceObjectIfItStillExists(game); + Card sourceCard = source.getSourceCardIfItStillExists(game); Permanent permanent = game.getPermanent(this.getTargetPointer().getFirst(game, source)); Player controller = game.getPlayer(source.getControllerId()); if (sourceCard != null && permanent != null && controller != null) { diff --git a/Mage.Sets/src/mage/cards/e/EdgarCharmedGroom.java b/Mage.Sets/src/mage/cards/e/EdgarCharmedGroom.java index d2a498177bc..8016a66a7f4 100644 --- a/Mage.Sets/src/mage/cards/e/EdgarCharmedGroom.java +++ b/Mage.Sets/src/mage/cards/e/EdgarCharmedGroom.java @@ -74,15 +74,12 @@ class EdgarCharmedGroomEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller == null) { - return false; - } - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (!(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } game.getState().setValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + source.getSourceId(), Boolean.TRUE); - controller.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game, false, false, true, null); + controller.moveCards(card, Zone.BATTLEFIELD, source, game, false, false, true, null); return true; } } diff --git a/Mage.Sets/src/mage/cards/e/EnigmaSphinx.java b/Mage.Sets/src/mage/cards/e/EnigmaSphinx.java index d777b11aac1..797e6e6f567 100644 --- a/Mage.Sets/src/mage/cards/e/EnigmaSphinx.java +++ b/Mage.Sets/src/mage/cards/e/EnigmaSphinx.java @@ -118,7 +118,7 @@ class EnigmaSphinxEffect extends OneShotEffect { if (controller == null) { return false; } - Card card = (Card) source.getSourceObjectIfItStillExists(game); + Card card = source.getSourceCardIfItStillExists(game); if (card != null) { controller.putCardOnTopXOfLibrary(card, game, source, 3, true); } diff --git a/Mage.Sets/src/mage/cards/f/Flameskull.java b/Mage.Sets/src/mage/cards/f/Flameskull.java index a79abfc977f..8e7d5446332 100644 --- a/Mage.Sets/src/mage/cards/f/Flameskull.java +++ b/Mage.Sets/src/mage/cards/f/Flameskull.java @@ -73,12 +73,12 @@ class FlameskullEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player player = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (player == null || !(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (player == null || card == null) { return false; } Cards cards = new CardsImpl(player.getLibrary().getFromTop(game)); - cards.add((Card) sourceObject); + cards.add(card); player.moveCards(cards, Zone.EXILED, source, game); game.addEffect(new FlameskullPlayEffect(cards, game), source); return true; diff --git a/Mage.Sets/src/mage/cards/h/HarvestHand.java b/Mage.Sets/src/mage/cards/h/HarvestHand.java index f1667031b88..4b55e43df98 100644 --- a/Mage.Sets/src/mage/cards/h/HarvestHand.java +++ b/Mage.Sets/src/mage/cards/h/HarvestHand.java @@ -65,15 +65,12 @@ class HarvestHandReturnTransformedEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller == null) { - return false; - } - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (!(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } game.getState().setValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + source.getSourceId(), Boolean.TRUE); - controller.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game); + controller.moveCards(card, Zone.BATTLEFIELD, source, game); return true; } } diff --git a/Mage.Sets/src/mage/cards/h/HauntingImitation.java b/Mage.Sets/src/mage/cards/h/HauntingImitation.java index d8237b1d4ba..551fdc33ab9 100644 --- a/Mage.Sets/src/mage/cards/h/HauntingImitation.java +++ b/Mage.Sets/src/mage/cards/h/HauntingImitation.java @@ -76,9 +76,9 @@ class HauntingImitationEffect extends OneShotEffect { } if (cards.isEmpty()) { Player player = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (player != null && sourceObject instanceof Card) { - player.moveCards((Card) sourceObject, Zone.HAND, source, game); + Card card = source.getSourceCardIfItStillExists(game); + if (player != null && card != null) { + player.moveCards(card, Zone.HAND, source, game); } return true; } diff --git a/Mage.Sets/src/mage/cards/m/MoorlandRescuer.java b/Mage.Sets/src/mage/cards/m/MoorlandRescuer.java index 02a4a60292f..f3e0710dc81 100644 --- a/Mage.Sets/src/mage/cards/m/MoorlandRescuer.java +++ b/Mage.Sets/src/mage/cards/m/MoorlandRescuer.java @@ -78,7 +78,7 @@ class MoorlandRescuerEffect extends OneShotEffect { TargetCard target = new MoorlandRescuerTarget(permanent.getPower().getValue(), source, game); player.choose(outcome, player.getGraveyard(), target, source, game); player.moveCards(new CardsImpl(target.getTargets()), Zone.BATTLEFIELD, source, game); - Card sourceCard = (Card) source.getSourceObjectIfItStillExists(game); + Card sourceCard = source.getSourceCardIfItStillExists(game); if (sourceCard != null) { player.moveCards(sourceCard, Zone.EXILED, source, game); } diff --git a/Mage.Sets/src/mage/cards/n/NextOfKin.java b/Mage.Sets/src/mage/cards/n/NextOfKin.java index 44b875c7cbd..3e2fce5d95a 100644 --- a/Mage.Sets/src/mage/cards/n/NextOfKin.java +++ b/Mage.Sets/src/mage/cards/n/NextOfKin.java @@ -67,7 +67,7 @@ class NextOfKinDiesEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - Card nextOfKinCard = (Card) source.getSourceObjectIfItStillExists(game); + Card nextOfKinCard = source.getSourceCardIfItStillExists(game); Object object = getValue("attachedTo"); if (controller == null || nextOfKinCard == null || !(object instanceof Permanent)) { return false; diff --git a/Mage.Sets/src/mage/cards/o/OjerAxonilDeepestMight.java b/Mage.Sets/src/mage/cards/o/OjerAxonilDeepestMight.java index 9700e7368a6..a4640a9186d 100644 --- a/Mage.Sets/src/mage/cards/o/OjerAxonilDeepestMight.java +++ b/Mage.Sets/src/mage/cards/o/OjerAxonilDeepestMight.java @@ -77,15 +77,12 @@ class OjerAxonilDeepestMightTransformEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller == null) { - return false; - } - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (!(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } game.getState().setValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + source.getSourceId(), Boolean.TRUE); - controller.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game, true, false, true, null); + controller.moveCards(card, Zone.BATTLEFIELD, source, game, true, false, true, null); return true; } } diff --git a/Mage.Sets/src/mage/cards/o/OjerKaslemDeepestGrowth.java b/Mage.Sets/src/mage/cards/o/OjerKaslemDeepestGrowth.java index e405ca69b25..67222a03623 100644 --- a/Mage.Sets/src/mage/cards/o/OjerKaslemDeepestGrowth.java +++ b/Mage.Sets/src/mage/cards/o/OjerKaslemDeepestGrowth.java @@ -71,15 +71,12 @@ class OjerKaslemDeepestGrowthTransformEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller == null) { - return false; - } - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (!(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } game.getState().setValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + source.getSourceId(), Boolean.TRUE); - controller.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game, true, false, true, null); + controller.moveCards(card, Zone.BATTLEFIELD, source, game, true, false, true, null); return true; } } diff --git a/Mage.Sets/src/mage/cards/o/OjerPakpatiqDeepestEpoch.java b/Mage.Sets/src/mage/cards/o/OjerPakpatiqDeepestEpoch.java index c095aee14bd..c302c5a021f 100644 --- a/Mage.Sets/src/mage/cards/o/OjerPakpatiqDeepestEpoch.java +++ b/Mage.Sets/src/mage/cards/o/OjerPakpatiqDeepestEpoch.java @@ -143,16 +143,13 @@ class OjerPakpatiqDeepestEpochTrigger extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller == null) { - return false; - } - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (!(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } game.getState().setValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + source.getSourceId(), Boolean.TRUE); - game.setEnterWithCounters(sourceObject.getId(), new Counters().addCounter(CounterType.TIME.createInstance(3))); - controller.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game, true, false, true, null); + game.setEnterWithCounters(card.getId(), new Counters().addCounter(CounterType.TIME.createInstance(3))); + controller.moveCards(card, Zone.BATTLEFIELD, source, game, true, false, true, null); return true; } } diff --git a/Mage.Sets/src/mage/cards/o/OjerTaqDeepestFoundation.java b/Mage.Sets/src/mage/cards/o/OjerTaqDeepestFoundation.java index ff44fd7ea7b..21ff3486692 100644 --- a/Mage.Sets/src/mage/cards/o/OjerTaqDeepestFoundation.java +++ b/Mage.Sets/src/mage/cards/o/OjerTaqDeepestFoundation.java @@ -74,15 +74,12 @@ class OjerTaqDeepestFoundationTransformEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller == null) { - return false; - } - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (!(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } game.getState().setValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + source.getSourceId(), Boolean.TRUE); - controller.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game, true, false, true, null); + controller.moveCards(card, Zone.BATTLEFIELD, source, game, true, false, true, null); return true; } } @@ -126,4 +123,4 @@ class OjerTaqDeepestFoundationTriplingEffect extends ReplacementEffectImpl { } return false; } -} \ No newline at end of file +} diff --git a/Mage.Sets/src/mage/cards/r/RetrieverPhoenix.java b/Mage.Sets/src/mage/cards/r/RetrieverPhoenix.java index ea75c81b534..46a73bbf66d 100644 --- a/Mage.Sets/src/mage/cards/r/RetrieverPhoenix.java +++ b/Mage.Sets/src/mage/cards/r/RetrieverPhoenix.java @@ -79,12 +79,12 @@ class RetrieverPhoenixEffect extends ReplacementEffectImpl { @Override public boolean replaceEvent(GameEvent event, Ability source, Game game) { - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); + Card card = source.getSourceCardIfItStillExists(game); Player player = game.getPlayer(source.getControllerId()); - return sourceObject instanceof Card + return card != null && player != null - && player.chooseUse(outcome, "Return " + sourceObject.getName() + " to the battlefield instead of learning?", source, game) - && player.moveCards((Card) sourceObject, Zone.BATTLEFIELD, source, game); + && player.chooseUse(outcome, "Return " + card.getName() + " to the battlefield instead of learning?", source, game) + && player.moveCards(card, Zone.BATTLEFIELD, source, game); } @Override diff --git a/Mage.Sets/src/mage/cards/s/ScreamingSwarm.java b/Mage.Sets/src/mage/cards/s/ScreamingSwarm.java index 9ee1720a955..4a3fed21002 100644 --- a/Mage.Sets/src/mage/cards/s/ScreamingSwarm.java +++ b/Mage.Sets/src/mage/cards/s/ScreamingSwarm.java @@ -101,11 +101,11 @@ class ScreamingSwarmEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player player = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); + Card card = source.getSourceCardIfItStillExists(game); return player != null - && sourceObject instanceof Card + && card != null && player.putCardOnTopXOfLibrary( - (Card) sourceObject, game, source, 2, true + card, game, source, 2, true ); } } diff --git a/Mage.Sets/src/mage/cards/s/SmokeShroud.java b/Mage.Sets/src/mage/cards/s/SmokeShroud.java index 69ada956387..bfbef44bb44 100644 --- a/Mage.Sets/src/mage/cards/s/SmokeShroud.java +++ b/Mage.Sets/src/mage/cards/s/SmokeShroud.java @@ -83,7 +83,7 @@ class SmokeShroudEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Card sourceCard = (Card) source.getSourceObjectIfItStillExists(game); + Card sourceCard = source.getSourceCardIfItStillExists(game); Permanent permanent = game.getPermanent(this.getTargetPointer().getFirst(game, source)); Player controller = game.getPlayer(source.getControllerId()); if (sourceCard != null && permanent != null && controller != null) { diff --git a/Mage.Sets/src/mage/cards/s/SproutbackTrudge.java b/Mage.Sets/src/mage/cards/s/SproutbackTrudge.java index 23ea5c78049..719e1ceb946 100644 --- a/Mage.Sets/src/mage/cards/s/SproutbackTrudge.java +++ b/Mage.Sets/src/mage/cards/s/SproutbackTrudge.java @@ -80,11 +80,10 @@ class SproutbackTrudgeEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player player = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (player == null || !(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (player == null || card == null) { return false; } - Card card = (Card) sourceObject; game.getState().setValue("PlayFromNotOwnHandZone" + card.getId(), Boolean.TRUE); player.cast( player.chooseAbilityForCast(card, game, false), diff --git a/Mage.Sets/src/mage/cards/s/StartledAwake.java b/Mage.Sets/src/mage/cards/s/StartledAwake.java index dc84587f48a..d9e1a5b049b 100644 --- a/Mage.Sets/src/mage/cards/s/StartledAwake.java +++ b/Mage.Sets/src/mage/cards/s/StartledAwake.java @@ -69,14 +69,13 @@ class StartledAwakeReturnTransformedEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (controller == null || !(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } if (game.getState().getZone(source.getSourceId()) != Zone.GRAVEYARD) { return true; } - Card card = (Card) sourceObject; game.getState().setValue(TransformAbility.VALUE_KEY_ENTER_TRANSFORMED + source.getSourceId(), true); controller.moveCards(card, Zone.BATTLEFIELD, source, game); return true; diff --git a/Mage.Sets/src/mage/cards/t/TreacherousPitDweller.java b/Mage.Sets/src/mage/cards/t/TreacherousPitDweller.java index 19778843798..fbc40ab2831 100644 --- a/Mage.Sets/src/mage/cards/t/TreacherousPitDweller.java +++ b/Mage.Sets/src/mage/cards/t/TreacherousPitDweller.java @@ -65,11 +65,10 @@ class TreacherousPitDwellerEffect extends ContinuousEffectImpl { @Override public boolean apply(Game game, Ability source) { - MageObject permanent = source.getSourceObjectIfItStillExists(game); // it can also return Card object + Permanent permanent = source.getSourcePermanentIfItStillExists(game); // it can also return Card object Player targetOpponent = game.getPlayer(source.getFirstTarget()); - if ((permanent instanceof Permanent) - && targetOpponent != null) { - return ((Permanent) permanent).changeControllerId(targetOpponent.getId(), game, source); + if (permanent != null && targetOpponent != null) { + return permanent.changeControllerId(targetOpponent.getId(), game, source); } else { discard(); } diff --git a/Mage.Sets/src/mage/cards/u/UvildaDeanOfPerfection.java b/Mage.Sets/src/mage/cards/u/UvildaDeanOfPerfection.java index db07cf33f23..fc20aec0057 100644 --- a/Mage.Sets/src/mage/cards/u/UvildaDeanOfPerfection.java +++ b/Mage.Sets/src/mage/cards/u/UvildaDeanOfPerfection.java @@ -195,10 +195,10 @@ class UvildaDeanOfPerfectionTriggeredAbility extends TriggeredAbilityImpl { @Override public boolean checkTrigger(GameEvent event, Game game) { - MageObject sourceObject = getSourceObjectIfItStillExists(game); + Card card = getSourceCardIfItStillExists(game); return event.getTargetId().equals(this.getSourceId()) - && sourceObject instanceof Card - && ((Card) sourceObject).getCounters(game).getCount(CounterType.HONE) == 0 + && card != null + && card.getCounters(game).getCount(CounterType.HONE) == 0 && event.getAmount() > 0 && event.getData().equals(CounterType.HONE.getName()); } @@ -238,11 +238,10 @@ class UvildaDeanOfPerfectionCastEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player player = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (player == null || !(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (player == null || card == null) { return false; } - Card card = (Card) sourceObject; if (!player.chooseUse(outcome, "Cast " + card.getName() + '?', source, game)) { return false; } @@ -300,4 +299,4 @@ class NassariDeanOfExpressionEffect extends OneShotEffect { } return true; } -} \ No newline at end of file +} diff --git a/Mage/src/main/java/mage/abilities/Ability.java b/Mage/src/main/java/mage/abilities/Ability.java index 165f0538013..95792c42add 100644 --- a/Mage/src/main/java/mage/abilities/Ability.java +++ b/Mage/src/main/java/mage/abilities/Ability.java @@ -12,6 +12,7 @@ import mage.abilities.effects.Effect; import mage.abilities.effects.Effects; import mage.abilities.hint.Hint; import mage.abilities.icon.CardIcon; +import mage.cards.Card; import mage.constants.*; import mage.game.Controllable; import mage.game.Game; @@ -580,6 +581,15 @@ public interface Ability extends Controllable, Serializable { */ MageObject getSourceObjectIfItStillExists(Game game); + + /** + * See getSourceObjectIfItStillExists for details. Works with Card only. + * + * @param game + * @return + */ + Card getSourceCardIfItStillExists(Game game); + /** * See getSourceObjectIfItStillExists for details. Works with Permanent only. * diff --git a/Mage/src/main/java/mage/abilities/AbilityImpl.java b/Mage/src/main/java/mage/abilities/AbilityImpl.java index 21c52ea470d..f504d9e3472 100644 --- a/Mage/src/main/java/mage/abilities/AbilityImpl.java +++ b/Mage/src/main/java/mage/abilities/AbilityImpl.java @@ -39,6 +39,7 @@ import mage.util.GameLog; import mage.util.ThreadLocalStringBuilder; import mage.watchers.Watcher; import org.apache.log4j.Logger; +import org.checkerframework.checker.units.qual.C; import java.util.*; @@ -1356,6 +1357,15 @@ public abstract class AbilityImpl implements Ability { return null; } + @Override + public Card getSourceCardIfItStillExists(Game game) { + MageObject mageObject = getSourceObjectIfItStillExists(game); + if (mageObject instanceof Card) { + return (Card) mageObject; + } + return null; + } + @Override public Permanent getSourcePermanentIfItStillExists(Game game) { MageObject mageObject = getSourceObjectIfItStillExists(game); diff --git a/Mage/src/main/java/mage/abilities/costs/common/ExileSourceCost.java b/Mage/src/main/java/mage/abilities/costs/common/ExileSourceCost.java index 772ab5be3a9..669b76dcecf 100644 --- a/Mage/src/main/java/mage/abilities/costs/common/ExileSourceCost.java +++ b/Mage/src/main/java/mage/abilities/costs/common/ExileSourceCost.java @@ -47,7 +47,7 @@ public class ExileSourceCost extends CostImpl { @Override public boolean canPay(Ability ability, Ability source, UUID controllerId, Game game) { - return source.getSourceObjectIfItStillExists(game) instanceof Card; + return source.getSourceCardIfItStillExists(game) != null; } @Override diff --git a/Mage/src/main/java/mage/abilities/effects/common/ExileSourceEffect.java b/Mage/src/main/java/mage/abilities/effects/common/ExileSourceEffect.java index f9802f92b6b..de5e9aba55a 100644 --- a/Mage/src/main/java/mage/abilities/effects/common/ExileSourceEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/common/ExileSourceEffect.java @@ -1,4 +1,3 @@ - package mage.abilities.effects.common; import mage.MageObject; @@ -18,7 +17,7 @@ import java.util.UUID; */ public class ExileSourceEffect extends OneShotEffect { - private boolean toUniqueExileZone; + private final boolean toUniqueExileZone; public ExileSourceEffect() { this(false); @@ -48,25 +47,21 @@ public class ExileSourceEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (sourceObject instanceof Card) { - if (sourceObject instanceof Permanent) { - if (!((Permanent) sourceObject).isPhasedIn()) { - return false; - } - } - UUID exileZoneId = null; - String exileZoneName = ""; - if (toUniqueExileZone) { - exileZoneId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter()); - exileZoneName = sourceObject.getName(); - } - Card sourceCard = (Card) sourceObject; - return controller.moveCardsToExile(sourceCard, source, game, true, exileZoneId, exileZoneName); - } + Card card = source.getSourceCardIfItStillExists(game); + if (controller == null || card == null) { return false; } - return false; + if (card instanceof Permanent) { + if (!((Permanent) card).isPhasedIn()) { + return false; + } + } + UUID exileZoneId = null; + String exileZoneName = ""; + if (toUniqueExileZone) { + exileZoneId = CardUtil.getExileZoneId(game, source.getSourceId(), source.getSourceObjectZoneChangeCounter()); + exileZoneName = card.getName(); + } + return controller.moveCardsToExile(card, source, game, true, exileZoneId, exileZoneName); } } diff --git a/Mage/src/main/java/mage/abilities/effects/common/PutOnLibrarySourceEffect.java b/Mage/src/main/java/mage/abilities/effects/common/PutOnLibrarySourceEffect.java index a01b5440219..69e70fd9bee 100644 --- a/Mage/src/main/java/mage/abilities/effects/common/PutOnLibrarySourceEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/common/PutOnLibrarySourceEffect.java @@ -39,13 +39,13 @@ public class PutOnLibrarySourceEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player player = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (player == null || !(sourceObject instanceof Card)) { + Card card = source.getSourceCardIfItStillExists(game); + if (player == null || card == null) { return false; } if (onTop) { - return player.putCardsOnTopOfLibrary((Card) sourceObject, game, source, false); + return player.putCardsOnTopOfLibrary(card, game, source, false); } - return player.putCardsOnBottomOfLibrary((Card) sourceObject, game, source, false); + return player.putCardsOnBottomOfLibrary(card, game, source, false); } } diff --git a/Mage/src/main/java/mage/abilities/effects/common/ReturnSourceFromGraveyardToHandEffect.java b/Mage/src/main/java/mage/abilities/effects/common/ReturnSourceFromGraveyardToHandEffect.java index 82e6b0087f2..d522eaa0b17 100644 --- a/Mage/src/main/java/mage/abilities/effects/common/ReturnSourceFromGraveyardToHandEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/common/ReturnSourceFromGraveyardToHandEffect.java @@ -31,9 +31,9 @@ public class ReturnSourceFromGraveyardToHandEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); + Card card = source.getSourceCardIfItStillExists(game); return controller != null - && sourceObject instanceof Card - && controller.moveCards((Card) sourceObject, Zone.HAND, source, game); + && card != null + && controller.moveCards(card, Zone.HAND, source, game); } } diff --git a/Mage/src/main/java/mage/abilities/effects/keyword/AdaptEffect.java b/Mage/src/main/java/mage/abilities/effects/keyword/AdaptEffect.java index 0a2f47b7eee..3b76a50897c 100644 --- a/Mage/src/main/java/mage/abilities/effects/keyword/AdaptEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/keyword/AdaptEffect.java @@ -1,10 +1,8 @@ package mage.abilities.effects.keyword; -import mage.MageObject; import mage.abilities.Ability; import mage.abilities.effects.OneShotEffect; import mage.constants.Outcome; -import mage.constants.Zone; import mage.counters.CounterType; import mage.game.Game; import mage.game.events.GameEvent; @@ -39,15 +37,7 @@ public class AdaptEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - // Verify source object did not change zone and is on the battlefield - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (sourceObject == null) { - if (game.getState().getZone(source.getSourceId()).equals(Zone.BATTLEFIELD) - && source.getSourceObjectZoneChangeCounter() + 1 == game.getState().getZoneChangeCounter(source.getSourceId())) { - sourceObject = game.getPermanent(source.getSourceId()); - } - } - Permanent permanent = ((Permanent) sourceObject); + Permanent permanent = source.getSourcePermanentIfItStillExists(game); if (permanent == null) { return false; } diff --git a/Mage/src/main/java/mage/abilities/effects/keyword/CompanionEffect.java b/Mage/src/main/java/mage/abilities/effects/keyword/CompanionEffect.java index a6c855e2d6e..a1698e3c512 100644 --- a/Mage/src/main/java/mage/abilities/effects/keyword/CompanionEffect.java +++ b/Mage/src/main/java/mage/abilities/effects/keyword/CompanionEffect.java @@ -30,7 +30,7 @@ public class CompanionEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - Card card = (Card) source.getSourceObjectIfItStillExists(game); + Card card = source.getSourceCardIfItStillExists(game); if (controller != null && card != null && game.getState().getZone(card.getId()) == Zone.OUTSIDE) { return controller.moveCards(card, Zone.HAND, source, game); } diff --git a/Mage/src/main/java/mage/abilities/keyword/FabricateAbility.java b/Mage/src/main/java/mage/abilities/keyword/FabricateAbility.java index e857a2adfb2..dedb9b47e84 100644 --- a/Mage/src/main/java/mage/abilities/keyword/FabricateAbility.java +++ b/Mage/src/main/java/mage/abilities/keyword/FabricateAbility.java @@ -67,8 +67,8 @@ class FabricateEffect extends OneShotEffect { public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); if (controller != null) { - MageObject sourceObject = source.getSourceObjectIfItStillExists(game); - if (sourceObject != null && controller.chooseUse( + Card card = source.getSourceCardIfItStillExists(game); + if (card != null && controller.chooseUse( Outcome.BoostCreature, "Fabricate " + value, null, @@ -76,7 +76,7 @@ class FabricateEffect extends OneShotEffect { "Create " + CardUtil.numberToText(value, "a") + " 1/1 token" + (value > 1 ? "s" : ""), source, game)) { - ((Card) sourceObject).addCounters(CounterType.P1P1.createInstance(value), source.getControllerId(), source, game); + card.addCounters(CounterType.P1P1.createInstance(value), source.getControllerId(), source, game); } else { new ServoToken().putOntoBattlefield(value, game, source, controller.getId()); } diff --git a/Mage/src/main/java/mage/abilities/keyword/MeditateAbility.java b/Mage/src/main/java/mage/abilities/keyword/MeditateAbility.java index d0a388539ef..6bbcfec37f8 100644 --- a/Mage/src/main/java/mage/abilities/keyword/MeditateAbility.java +++ b/Mage/src/main/java/mage/abilities/keyword/MeditateAbility.java @@ -1,7 +1,5 @@ - package mage.abilities.keyword; -import mage.MageObject; import mage.abilities.Ability; import mage.abilities.ActivatedAbilityImpl; import mage.abilities.costs.Cost; @@ -21,7 +19,7 @@ import mage.players.Player; public class MeditateAbility extends ActivatedAbilityImpl { public MeditateAbility(Cost cost) { - super(Zone.BATTLEFIELD, new ReturnToHandEffect(), cost); + super(Zone.BATTLEFIELD, new MeditateEffect(), cost); this.timing = TimingRule.SORCERY; } @@ -43,37 +41,33 @@ public class MeditateAbility extends ActivatedAbilityImpl { } -class ReturnToHandEffect extends OneShotEffect { +class MeditateEffect extends OneShotEffect { - public ReturnToHandEffect() { + MeditateEffect() { super(Outcome.ReturnToHand); } - protected ReturnToHandEffect(final ReturnToHandEffect effect) { + protected MeditateEffect(final MeditateEffect effect) { super(effect); } @Override - public ReturnToHandEffect copy() { - return new ReturnToHandEffect(this); + public MeditateEffect copy() { + return new MeditateEffect(this); } @Override public boolean apply(Game game, Ability source) { Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - MageObject mageObject = source.getSourceObjectIfItStillExists(game); - if (mageObject != null) { - Permanent permanent = game.getPermanent(source.getSourceId()); - if (permanent != null) { - boolean ret = controller.moveCards(permanent, Zone.HAND, source, game); - if (ret) { - game.fireEvent(new GameEvent(GameEvent.EventType.MEDITATED, source.getSourceId(), source, controller.getId())); - } - return ret; - } - } + Permanent permanent = source.getSourcePermanentIfItStillExists(game); + if (controller == null || permanent == null) { + return false; } - return false; + boolean ret = controller.moveCards(permanent, Zone.HAND, source, game); + if (ret) { + game.fireEvent(new GameEvent(EventType.MEDITATED, source.getSourceId(), source, controller.getId())); + } + return ret; } + } diff --git a/Mage/src/main/java/mage/game/stack/StackAbility.java b/Mage/src/main/java/mage/game/stack/StackAbility.java index 64273d665b0..67a357a529d 100644 --- a/Mage/src/main/java/mage/game/stack/StackAbility.java +++ b/Mage/src/main/java/mage/game/stack/StackAbility.java @@ -16,6 +16,7 @@ import mage.abilities.effects.Effect; import mage.abilities.effects.Effects; import mage.abilities.hint.Hint; import mage.abilities.icon.CardIcon; +import mage.cards.Card; import mage.cards.FrameStyle; import mage.constants.*; import mage.filter.predicate.mageobject.MageObjectReferencePredicate; @@ -642,6 +643,11 @@ public class StackAbility extends StackObjectImpl implements Ability { return this.ability.getSourceObjectIfItStillExists(game); } + @Override + public Card getSourceCardIfItStillExists(Game game) { + return this.ability.getSourceCardIfItStillExists(game); + } + @Override public Permanent getSourcePermanentIfItStillExists(Game game) { return this.ability.getSourcePermanentIfItStillExists(game);