use of mage.filter.predicate.permanent.CanBeEnchantedByPredicate in project mage by magefree.
the class NettlevineBlightEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent nettlevineBlight = game.getPermanent(source.getSourceId());
Player newController = null;
if (controller != null && nettlevineBlight != null) {
Permanent enchantedPermanent = game.getPermanent(nettlevineBlight.getAttachedTo());
if (enchantedPermanent != null) {
newController = game.getPlayer(enchantedPermanent.getControllerId());
enchantedPermanent.sacrifice(source, game);
}
if (newController != null) {
FilterPermanent filter = new FilterPermanent("creature or land permanent you control");
filter.add(Predicates.or(CardType.CREATURE.getPredicate(), CardType.LAND.getPredicate()));
filter.add(new ControllerIdPredicate(newController.getId()));
filter.add(new CanBeEnchantedByPredicate(nettlevineBlight));
Target target = new TargetPermanent(filter);
target.setNotTarget(true);
if (target.canChoose(source.getSourceId(), newController.getId(), game) && newController.choose(outcome, target, source.getSourceId(), game)) {
Permanent chosenPermanent = game.getPermanent(target.getFirstTarget());
if (chosenPermanent != null) {
Card nettlevineBlightCard = game.getCard(source.getSourceId());
if (nettlevineBlightCard != null) {
game.getState().setValue("attachTo:" + nettlevineBlight.getId(), chosenPermanent);
chosenPermanent.addAttachment(nettlevineBlight.getId(), source, game);
return true;
}
}
}
}
}
return false;
}
use of mage.filter.predicate.permanent.CanBeEnchantedByPredicate in project mage by magefree.
the class RetetherEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
if (controller != null) {
Map<Card, Permanent> auraMap = new HashMap<>();
auraCardsInGraveyard: for (Card aura : controller.getGraveyard().getCards(filterAura, source.getSourceId(), source.getControllerId(), game)) {
if (aura != null) {
FilterCreaturePermanent filter = new FilterCreaturePermanent("creature to enchant (" + aura.getLogName() + ')');
filter.add(new CanBeEnchantedByPredicate(aura));
Target target = null;
auraLegalitySearch: for (UUID playerId : game.getState().getPlayersInRange(source.getControllerId(), game)) {
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(filter, playerId, game)) {
if (permanent != null) {
for (Ability ability : aura.getAbilities()) {
if (ability instanceof SpellAbility) {
for (Target abilityTarget : ability.getTargets()) {
if (abilityTarget.possibleTargets(controller.getId(), game).contains(permanent.getId())) {
target = abilityTarget.copy();
break auraLegalitySearch;
}
}
}
}
}
}
}
if (target != null) {
target.getFilter().add(CardType.CREATURE.getPredicate());
target.setNotTarget(true);
if (target.canChoose(source.getSourceId(), controller.getId(), game)) {
target.setTargetName("creature to enchant (" + aura.getLogName() + ')');
if (controller.choose(Outcome.PutCardInPlay, target, source.getSourceId(), game)) {
Permanent permanent = game.getPermanent(target.getFirstTarget());
if (permanent != null && !permanent.cantBeAttachedBy(aura, source, game, true)) {
auraMap.put(aura, permanent);
game.getState().setValue("attachTo:" + aura.getId(), permanent);
continue auraCardsInGraveyard;
}
}
}
}
game.informPlayers("No valid creature targets for " + aura.getLogName());
}
}
controller.moveCards(auraMap.keySet(), Zone.BATTLEFIELD, source, game);
for (Entry<Card, Permanent> entry : auraMap.entrySet()) {
Card aura = entry.getKey();
Permanent permanent = entry.getValue();
if (aura != null && permanent != null) {
permanent.addAttachment(aura.getId(), source, game);
}
}
return true;
}
return false;
}
use of mage.filter.predicate.permanent.CanBeEnchantedByPredicate in project mage by magefree.
the class BreathOfFuryEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Permanent enchantment = game.getPermanent(source.getSourceId());
if (enchantment == null) {
return false;
}
Permanent enchantedCreature = game.getPermanent((UUID) getValue("TriggeringCreatureId"));
Player controller = game.getPlayer(source.getControllerId());
FilterControlledCreaturePermanent filter = new FilterControlledCreaturePermanent("creature you control that could be enchanted by " + enchantment.getName());
filter.add(new CanBeEnchantedByPredicate(enchantment));
Target target = new TargetControlledCreaturePermanent(filter);
target.setNotTarget(true);
// Commanders going to the command zone and Rest in Peace style replacement effects don't make Permanent.sacrifice return false.
if (enchantedCreature != null && controller != null && enchantedCreature.sacrifice(source, game) && target.canChoose(source.getSourceId(), controller.getId(), game)) {
controller.choose(outcome, target, source.getSourceId(), game);
Permanent newCreature = game.getPermanent(target.getFirstTarget());
boolean success = false;
if (newCreature != null) {
Permanent oldCreature = game.getPermanent(enchantment.getAttachedTo());
if (oldCreature != null) {
if (oldCreature.getId().equals(newCreature.getId())) {
success = true;
} else {
if (oldCreature.removeAttachment(enchantment.getId(), source, game) && newCreature.addAttachment(enchantment.getId(), source, game)) {
game.informPlayers(enchantment.getLogName() + " was unattached from " + oldCreature.getLogName() + " and attached to " + newCreature.getLogName());
success = true;
}
}
} else if (newCreature.addAttachment(enchantment.getId(), source, game)) {
game.informPlayers(enchantment.getLogName() + " was attached to " + newCreature.getLogName());
success = true;
}
}
if (success) {
for (Permanent permanent : game.getBattlefield().getAllActivePermanents(new FilterControlledCreaturePermanent(), controller.getId(), game)) {
permanent.untap(game);
}
game.getState().getTurnMods().add(new TurnMod(source.getControllerId(), TurnPhase.COMBAT, null, false));
}
return true;
}
return false;
}
Aggregations