avoid casting to Card with dedicated method getSourceCardIfItStillExists

This commit is contained in:
xenohedron 2024-06-02 19:39:58 -04:00
parent a90f226053
commit d226b30592
40 changed files with 150 additions and 178 deletions

View file

@ -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.
*

View file

@ -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);

View file

@ -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

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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());
}

View file

@ -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;
}
}

View file

@ -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);