use of mage.MageObjectReference in project mage by magefree.
the class BoseijuWhoSheltersAllCantCounterEffect method applies.
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
BoseijuWhoSheltersAllWatcher watcher = game.getState().getWatcher(BoseijuWhoSheltersAllWatcher.class, source.getSourceId());
Spell spell = game.getStack().getSpell(event.getTargetId());
return spell != null && watcher != null && watcher.spellCantBeCountered(new MageObjectReference(spell, game));
}
use of mage.MageObjectReference in project mage by magefree.
the class ChefsKissApplier method apply.
@Override
public boolean apply(Game game, Ability source) {
Spell spell = game.getSpell(source.getFirstTarget());
if (spell == null) {
return false;
}
spell.setControllerId(source.getControllerId());
List<UUID> possibleTargets = new ArrayList<>(game.getState().getPlayersInRange(source.getControllerId(), game));
possibleTargets.remove(source.getControllerId());
game.getBattlefield().getActivePermanents(source.getControllerId(), game).stream().filter(p -> !p.isControlledBy(source.getControllerId())).map(MageItem::getId).forEach(possibleTargets::add);
possibleTargets.removeIf(uuid -> !spell.canTarget(game, uuid));
StackObjectCopyApplier applier;
MageObjectReferencePredicate predicate;
if (possibleTargets.isEmpty()) {
applier = null;
predicate = null;
} else {
applier = new ChefsKissApplier(possibleTargets.get(RandomUtil.nextInt(possibleTargets.size())), game);
predicate = new MageObjectReferencePredicate(new MageObjectReference(possibleTargets.get(RandomUtil.nextInt(possibleTargets.size())), game));
}
spell.createCopyOnStack(game, source, source.getControllerId(), false, 1, applier);
if (predicate != null) {
spell.chooseNewTargets(game, source.getControllerId(), true, true, predicate);
}
return true;
}
use of mage.MageObjectReference in project mage by magefree.
the class GlyphOfReincarnationEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Permanent targetWall = getTargetPointer().getFirstTargetPermanentOrLKI(game, source);
if (controller != null && targetWall != null) {
BlockedAttackerWatcher watcher = game.getState().getWatcher(BlockedAttackerWatcher.class);
if (watcher != null) {
Map<UUID, Player> destroyed = new HashMap<>();
for (Permanent creature : game.getBattlefield().getActivePermanents(StaticFilters.FILTER_PERMANENT_CREATURE, source.getControllerId(), source.getSourceId(), game)) {
if (!creature.getId().equals(targetWall.getId())) {
if (watcher.creatureHasBlockedAttacker(new MageObjectReference(creature, game), new MageObjectReference(targetWall, game), game)) {
if (creature.destroy(source, game, true) && game.getState().getZone(creature.getId()) == Zone.GRAVEYARD) {
// If a commander is replaced to command zone, the creature does not die
Player permController = game.getPlayer(creature.getControllerId());
if (permController != null) {
destroyed.put(creature.getId(), permController);
}
}
}
}
}
// onto the battlefield under its owner’s control
for (Map.Entry<UUID, Player> entry : destroyed.entrySet()) {
Permanent permanent = (Permanent) game.getLastKnownInformation(entry.getKey(), Zone.BATTLEFIELD);
Player player = entry.getValue();
if (permanent != null && player != null) {
FilterCreatureCard filter = new FilterCreatureCard("a creature card from " + player.getName() + "'s graveyard");
filter.add(new OwnerIdPredicate(player.getId()));
Target targetCreature = new TargetCardInGraveyard(filter);
targetCreature.setNotTarget(true);
if (targetCreature.canChoose(source.getSourceId(), controller.getId(), game) && controller.chooseTarget(outcome, targetCreature, source, game)) {
Card card = game.getCard(targetCreature.getFirstTarget());
if (card != null && game.getState().getZone(card.getId()) == Zone.GRAVEYARD) {
controller.moveCards(card, Zone.BATTLEFIELD, source, game, false, false, true, null);
}
}
}
}
return true;
}
}
return false;
}
use of mage.MageObjectReference in project mage by magefree.
the class HaukensInsightWatcher method applies.
@Override
public boolean applies(UUID objectId, Ability source, UUID affectedControllerId, Game game) {
if (affectedControllerId.equals(source.getControllerId()) && game.isActivePlayer(source.getControllerId())) {
Player controller = game.getPlayer(source.getControllerId());
HaukensInsightWatcher watcher = game.getState().getWatcher(HaukensInsightWatcher.class);
Permanent sourceObject = game.getPermanent(source.getSourceId());
if (controller != null && watcher != null && sourceObject != null && !watcher.isAbilityUsed(new MageObjectReference(sourceObject, game))) {
UUID exileId = CardUtil.getExileZoneId(game, source.getSourceId(), game.getState().getZoneChangeCounter(source.getSourceId()));
ExileZone exileZone = game.getExile().getExileZone(exileId);
if (exileZone != null && exileZone.contains(CardUtil.getMainCardId(game, objectId))) {
allowCardToPlayWithoutMana(objectId, source, affectedControllerId, game);
return true;
}
}
}
return false;
}
use of mage.MageObjectReference in project mage by magefree.
the class LongRoadHomeEntersBattlefieldEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Card card = game.getCard(objectToReturn.getSourceId());
if (card != null && objectToReturn.refersTo(card, game)) {
Player owner = game.getPlayer(card.getOwnerId());
if (owner != null) {
if (card instanceof MeldCard) {
MeldCard meldCard = (MeldCard) card;
game.addEffect(new LongRoadHomeEntersBattlefieldEffect(new MageObjectReference(meldCard.getTopHalfCard(), game)), source);
game.addEffect(new LongRoadHomeEntersBattlefieldEffect(new MageObjectReference(meldCard.getBottomHalfCard(), game)), source);
} else {
game.addEffect(new LongRoadHomeEntersBattlefieldEffect(objectToReturn), source);
}
owner.moveCards(card, Zone.BATTLEFIELD, source, game, false, false, true, null);
}
}
return true;
}
Aggregations