use of mage.abilities.effects.common.continuous.GainControlTargetEffect in project mage by magefree.
the class CulturalExchangeEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player player1 = game.getPlayer(targetPointer.getTargets(game, source).get(0));
Player player2 = game.getPlayer(targetPointer.getTargets(game, source).get(1));
Player controller = game.getPlayer(source.getControllerId());
if (player1 == null || player2 == null || controller == null) {
return false;
}
FilterCreaturePermanent filter1 = new FilterCreaturePermanent("creatures " + player1.getLogName() + " controls");
FilterCreaturePermanent filter2 = new FilterCreaturePermanent("creatures " + player2.getLogName() + " controls");
filter1.add(new ControllerIdPredicate(player1.getId()));
filter2.add(new ControllerIdPredicate(player2.getId()));
int creatureCount1 = game.getBattlefield().count(filter1, source.getSourceId(), source.getControllerId(), game);
int creatureCount2 = game.getBattlefield().count(filter2, source.getSourceId(), source.getControllerId(), game);
int creaturesToSwitch = Math.min(creatureCount1, creatureCount2);
if (creaturesToSwitch == 0) {
return true;
}
TargetCreaturePermanent target1 = new TargetCreaturePermanent(0, creaturesToSwitch, filter1, true);
if (target1.choose(Outcome.Benefit, controller.getId(), source.getSourceId(), game)) {
int otherToSwitch = target1.getTargets().size();
TargetCreaturePermanent target2 = new TargetCreaturePermanent(otherToSwitch, otherToSwitch, filter2, true);
if (target2.choose(Outcome.Benefit, controller.getId(), source.getSourceId(), game)) {
for (UUID creatureId : target1.getTargets()) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
ContinuousEffect effect = new GainControlTargetEffect(Duration.Custom, player2.getId());
game.informPlayers(player2.getLogName() + " gains control of " + creature.getLogName());
effect.setTargetPointer(new FixedTarget(creature, game));
game.addEffect(effect, source);
}
}
for (UUID creatureId : target2.getTargets()) {
Permanent creature = game.getPermanent(creatureId);
if (creature != null) {
ContinuousEffect effect = new GainControlTargetEffect(Duration.Custom, player1.getId());
game.informPlayers(player1.getLogName() + " gains control of " + creature.getLogName());
effect.setTargetPointer(new FixedTarget(creature, game));
game.addEffect(effect, source);
}
}
}
}
return true;
}
use of mage.abilities.effects.common.continuous.GainControlTargetEffect in project mage by magefree.
the class CrownOfDoomEffect method apply.
@Override
public boolean apply(Game game, Ability source) {
Player controller = game.getPlayer(source.getControllerId());
Player newController = game.getPlayer(getTargetPointer().getFirst(game, source));
if (controller != null && newController != null && !Objects.equals(controller.getId(), newController.getId())) {
ContinuousEffect effect = new GainControlTargetEffect(Duration.Custom, newController.getId());
effect.setTargetPointer(new FixedTarget(source.getSourceId(), game));
game.addEffect(effect, source);
return true;
}
return false;
}
Aggregations