use of mage.abilities.effects.OneShotEffect in project mage by magefree.
the class DoUnlessControllerPaysEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
MageObject sourceObject = game.getObject(source.getSourceId());
if (controller != null && sourceObject != null) {
String message;
if (chooseUseText == null) {
String effectText = executingEffects.getText(source.getModes().getMode());
message = "Pay " + cost.getText() + " to prevent (" + effectText.substring(0, effectText.length() - 1) + ")?";
} else {
message = chooseUseText;
}
message = CardUtil.replaceSourceName(message, sourceObject.getName());
boolean result = true;
boolean doEffect = true;
// check if controller is willing to pay
if (cost.canPay(source, source, controller.getId(), game) && controller.chooseUse(Outcome.Detriment, message, source, game)) {
cost.clearPaid();
if (cost.pay(source, game, source, controller.getId(), false, null)) {
if (!game.isSimulation()) {
game.informPlayers(controller.getLogName() + " pays the cost to prevent the effect");
}
doEffect = false;
}
}
// do the effects if not paid
if (doEffect) {
for (Effect effect : executingEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
}
return result;
}
return false;
}
use of mage.abilities.effects.OneShotEffect in project mage by magefree.
the class DashAddDelayedTriggeredAbilityEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
if (game.getPermanentEntering(source.getSourceId()) != null) {
OneShotEffect returnToHandEffect = new ReturnToHandTargetEffect();
ConditionalOneShotEffect mustBeOnBattlefieldToReturn = new ConditionalOneShotEffect(returnToHandEffect, DashAddDelayedTriggeredAbilityEffect::check);
mustBeOnBattlefieldToReturn.setText("return the dashed creature from the battlefield to its owner's hand");
// init target pointer now because the dashed creature will only be returned from battlefield zone (now in entering state so zone change counter is not raised yet)
mustBeOnBattlefieldToReturn.setTargetPointer(new FixedTarget(source.getSourceId(), game.getState().getZoneChangeCounter(source.getSourceId()) + 1));
DelayedTriggeredAbility delayedAbility = new AtTheBeginOfNextEndStepDelayedTriggeredAbility(mustBeOnBattlefieldToReturn);
game.addDelayedTriggeredAbility(delayedAbility, source);
return true;
}
return false;
}
use of mage.abilities.effects.OneShotEffect in project mage by magefree.
the class KrovikanVampireDelayedTriggeredAbility method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent krovikanVampire = game.getPermanent(source.getSourceId());
creaturesAffected = (Set<UUID>) game.getState().getValue(source.getSourceId() + "creatureToGainControl");
if (creaturesAffected != null && controller != null && krovikanVampire != null) {
creaturesAffected.stream().map((creatureId) -> {
controller.moveCards(game.getCard(creatureId), Zone.BATTLEFIELD, source, game, false, false, false, null);
return creatureId;
}).map((creatureId) -> {
OneShotEffect effect = new SacrificeTargetEffect();
effect.setText("Sacrifice this if Krovikan Vampire leaves the battlefield or its current controller loses control of it.");
effect.setTargetPointer(new FixedTarget(creatureId, game));
return effect;
}).map((effect) -> new KrovikanVampireDelayedTriggeredAbility(effect, krovikanVampire.getId())).forEachOrdered((dTA) -> {
game.addDelayedTriggeredAbility(dTA, source);
});
creaturesAffected.clear();
return true;
}
return false;
}
use of mage.abilities.effects.OneShotEffect in project mage by magefree.
the class PlaneswalkersMischiefCondition method apply.
@Override
public boolean apply(Game game, Ability source) {
Player opponent = game.getPlayer(source.getFirstTarget());
if (opponent != null && opponent.getHand().size() > 0) {
Card revealedCard = opponent.getHand().getRandom(game);
if (revealedCard == null) {
return false;
}
Cards cards = new CardsImpl(revealedCard);
opponent.revealCards(source, cards, game);
if (revealedCard.isInstant(game) || revealedCard.isSorcery(game)) {
opponent.moveCardToExileWithInfo(revealedCard, source.getSourceId(), "Planeswalker's Mischief", source, game, Zone.HAND, true);
AsThoughEffect effect = new PlaneswalkersMischiefCastFromExileEffect();
effect.setTargetPointer(new FixedTarget(revealedCard.getId()));
game.addEffect(effect, source);
OneShotEffect effect2 = new ReturnFromExileEffect(Zone.HAND);
Condition condition = new PlaneswalkersMischiefCondition(source.getSourceId(), revealedCard.getId());
ConditionalOneShotEffect effect3 = new ConditionalOneShotEffect(effect2, condition, "if you haven't cast it, return it to its owner's hand.");
DelayedTriggeredAbility delayedAbility = new AtTheBeginOfNextEndStepDelayedTriggeredAbility(effect3);
delayedAbility.addWatcher(new SpellsCastWatcher());
game.addDelayedTriggeredAbility(delayedAbility, source);
return true;
}
}
return false;
}
use of mage.abilities.effects.OneShotEffect in project mage by magefree.
the class DoUnlessAttachedControllerPaysEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent aura = game.getPermanentOrLKIBattlefield(source.getSourceId());
if (aura == null) {
return false;
}
Permanent attachedTo = game.getPermanentOrLKIBattlefield(aura.getAttachedTo());
if (attachedTo == null) {
return false;
}
Player controllerOfAttachedTo = game.getPlayer(attachedTo.getControllerId());
if (controllerOfAttachedTo != null) {
String message;
if (chooseUseText == null) {
String effectText = executingEffects.getText(source.getModes().getMode());
message = "Pay " + cost.getText() + " to prevent (" + effectText.substring(0, effectText.length() - 1) + ")?";
} else {
message = chooseUseText;
}
message = CardUtil.replaceSourceName(message, aura.getName());
boolean result = true;
boolean doEffect = true;
// check if controller is willing to pay
if (cost.canPay(source, source, controllerOfAttachedTo.getId(), game) && controllerOfAttachedTo.chooseUse(Outcome.Neutral, message, source, game)) {
cost.clearPaid();
if (cost.pay(source, game, source, controllerOfAttachedTo.getId(), false, null)) {
if (!game.isSimulation()) {
game.informPlayers(controllerOfAttachedTo.getLogName() + " pays the cost to prevent the effect");
}
doEffect = false;
}
}
// do the effects if not paid
if (doEffect) {
for (Effect effect : executingEffects) {
effect.setTargetPointer(this.targetPointer);
if (effect instanceof OneShotEffect) {
result &= effect.apply(game, source);
} else {
game.addEffect((ContinuousEffect) effect, source);
}
}
}
return result;
}
return false;
}
Aggregations