foul-magics/Mage/src/main/java/mage/abilities/keyword/RecoverAbility.java
Susucre f75b1c9f0a
Code cleanup: protect all copy constructors (#10750)
* apply regex to change public copy constructors to protected
* cleanup code using now protected constructors
* fix manaBuilder weird casting of Mana into ConditionalMana
2023-08-04 19:34:58 -04:00

117 lines
3.9 KiB
Java

package mage.abilities.keyword;
import mage.abilities.Ability;
import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.costs.Cost;
import mage.abilities.costs.mana.ManaCost;
import mage.abilities.effects.OneShotEffect;
import mage.abilities.effects.common.ExileSourceEffect;
import mage.abilities.effects.common.ReturnToHandSourceEffect;
import mage.cards.Card;
import mage.constants.Outcome;
import mage.constants.Zone;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.events.ZoneChangeEvent;
import mage.players.Player;
/**
* 702.58a Recover is a triggered ability that functions only while the card
* with recover is in a player's graveyard. “Recover [cost]” means “When a
* creature is put into your graveyard from the battlefield, you may pay [cost].
* If you do, return this card from your graveyard to your hand. Otherwise,
* exile this card.”
*
* @author LevelX2
*/
public class RecoverAbility extends TriggeredAbilityImpl {
public RecoverAbility(Cost cost, Card card) {
super(Zone.GRAVEYARD, new RecoverEffect(cost, card.isCreature()), false);
}
protected RecoverAbility(final RecoverAbility ability) {
super(ability);
}
@Override
public RecoverAbility copy() {
return new RecoverAbility(this);
}
@Override
public boolean checkEventType(GameEvent event, Game game) {
return event.getType() == GameEvent.EventType.ZONE_CHANGE;
}
@Override
public boolean checkTrigger(GameEvent event, Game game) {
ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
if (zEvent.isDiesEvent()) {
if (zEvent.getTarget().isOwnedBy(getControllerId())
&& zEvent.getTarget().isCreature(game)
&& !zEvent.getTarget().getId().equals(getSourceId())) {
return true;
}
}
return false;
}
@Override
public String getRule() {
return super.getRule();
}
}
class RecoverEffect extends OneShotEffect {
protected Cost cost;
public RecoverEffect(Cost cost, boolean creature) {
super(Outcome.ReturnToHand);
this.cost = cost;
this.staticText = setText(cost, creature);
}
protected RecoverEffect(final RecoverEffect effect) {
super(effect);
this.cost = effect.cost.copy();
}
@Override
public RecoverEffect copy() {
return new RecoverEffect(this);
}
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Card sourceCard = game.getCard(source.getSourceId());
if (controller != null && sourceCard != null
&& game.getState().getZone(source.getSourceId()) == Zone.GRAVEYARD) {
if (controller.chooseUse(Outcome.Damage, "Pay " + cost.getText() + " to recover " + sourceCard.getLogName() + "? (Otherwise the card will be exiled)", source, game)) {
cost.clearPaid();
if (cost.pay(source, game, source, controller.getId(), false, null)) {
return new ReturnToHandSourceEffect().apply(game, source);
}
}
return new ExileSourceEffect().apply(game, source);
}
return false;
}
private String setText(Cost cost, boolean creature) {
StringBuilder sb = new StringBuilder();
sb.append("Recover");
if (cost instanceof ManaCost) {
sb.append(' ').append(cost.getText()).append(' ');
} else {
sb.append("—").append(cost.getText()).append(". ");
}
sb.append("<i>(When ").append(creature ? "another" : "a").append(" creature is put into your graveyard from the battlefield, you may pay ");
sb.append(cost.getText());
sb.append(". If you do, return this card from your graveyard to your hand. Otherwise, exile this card.)</i>");
return sb.toString();
}
}