use of mage.game.turn.TurnMod 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