package mage.abilities.common; import mage.MageIdentifier; import mage.MageObjectReference; import mage.abilities.Ability; import mage.abilities.SpellAbility; import mage.abilities.costs.Cost; import mage.abilities.costs.Costs; import mage.abilities.costs.CostsImpl; import mage.abilities.effects.AsThoughEffectImpl; import mage.abilities.effects.common.replacement.MorEnteringTappedEffect; import mage.cards.Card; import mage.constants.*; import mage.filter.FilterCard; import mage.game.Game; import mage.game.events.GameEvent; import mage.game.permanent.Permanent; import mage.game.stack.Spell; import mage.players.Player; import mage.watchers.Watcher; import java.util.HashSet; import java.util.Set; import java.util.UUID; /** * Once during each of your turns, you may cast... from your graveyard *
* See Lurrus of the Dream Den and Rivaz of the Claw
*
* @author weirddan455, Susucr
*/
public class CastFromGraveyardOnceDuringEachOfYourTurnAbility extends SimpleStaticAbility {
public CastFromGraveyardOnceDuringEachOfYourTurnAbility(FilterCard filter) {
this(filter, (Cost) null);
}
public CastFromGraveyardOnceDuringEachOfYourTurnAbility(FilterCard filter, Cost additionalCost) {
this(filter, additionalCost, MageIdentifier.OnceOnYourTurnCastFromGraveyard);
}
public CastFromGraveyardOnceDuringEachOfYourTurnAbility(FilterCard filter, MageIdentifier mageIdentifier) {
this(filter, null, mageIdentifier);
}
public CastFromGraveyardOnceDuringEachOfYourTurnAbility(FilterCard filter, Cost additionalCost, MageIdentifier mageIdentifier) {
super(new CastFromGraveyardOnceEffect(filter, additionalCost, mageIdentifier));
this.addWatcher(new CastFromGraveyardOnceWatcher());
switch (mageIdentifier) {
case OnceOnYourTurnCastFromGraveyard:
case OnceOnYourTurnCastFromGraveyardEntersTapped:
this.setIdentifier(mageIdentifier);
break;
default:
throw new IllegalArgumentException("Wrong code usage: only specific MageIdentifier are currently supported");
}
}
private CastFromGraveyardOnceDuringEachOfYourTurnAbility(final CastFromGraveyardOnceDuringEachOfYourTurnAbility ability) {
super(ability);
}
@Override
public CastFromGraveyardOnceDuringEachOfYourTurnAbility copy() {
return new CastFromGraveyardOnceDuringEachOfYourTurnAbility(this);
}
}
class CastFromGraveyardOnceEffect extends AsThoughEffectImpl {
private final FilterCard filter;
private final Cost additionalCost;
private final MageIdentifier mageIdentifier;
CastFromGraveyardOnceEffect(FilterCard filter, Cost additionalCost, MageIdentifier mageIdentifier) {
super(AsThoughEffectType.CAST_FROM_NOT_OWN_HAND_ZONE, Duration.WhileOnBattlefield, Outcome.Benefit);
this.filter = filter;
this.staticText = "Once during each of your turns, you may cast " + filter.getMessage()
+ (filter.getMessage().contains("your graveyard") ? "" : " from your graveyard")
+ (additionalCost == null ? "" : " by " + additionalCost.getText() + " in addition to paying its other costs.");
this.additionalCost = additionalCost;
this.mageIdentifier = mageIdentifier;
}
private CastFromGraveyardOnceEffect(final CastFromGraveyardOnceEffect effect) {
super(effect);
this.filter = effect.filter;
this.additionalCost = effect.additionalCost;
this.mageIdentifier = effect.mageIdentifier;
}
@Override
public CastFromGraveyardOnceEffect copy() {
return new CastFromGraveyardOnceEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
return true;
}
@Override
public boolean applies(UUID objectId, Ability source, UUID affectedControllerId, Game game) {
throw new IllegalArgumentException("Wrong code usage: can't call applies method on empty affectedAbility");
}
@Override
public boolean applies(UUID objectId, Ability affectedAbility, Ability source, Game game, UUID playerId) {
Player controller = game.getPlayer(source.getControllerId());
Permanent sourcePermanent = source.getSourcePermanentIfItStillExists(game);
CastFromGraveyardOnceWatcher watcher = game.getState().getWatcher(CastFromGraveyardOnceWatcher.class);
Card cardToCast = game.getCard(objectId);
if (controller == null || sourcePermanent == null || watcher == null || cardToCast == null
|| !game.isActivePlayer(playerId) // only during your turn
|| !source.isControlledBy(playerId) // only you may cast
|| !Zone.GRAVEYARD.equals(game.getState().getZone(objectId)) // from graveyard
|| !cardToCast.getOwnerId().equals(playerId) // only your graveyard
|| !(affectedAbility instanceof SpellAbility) // characteristics to check
|| watcher.abilityUsed(new MageObjectReference(sourcePermanent, game)) // once per turn
) {
return false;
}
SpellAbility spellAbility = (SpellAbility) affectedAbility;
Card cardToCheck = spellAbility.getCharacteristics(game);
if (spellAbility.getManaCosts().isEmpty()) {
return false;
}
Set