From 11efdcad789545fa0ecb3e46bc48c4e8a13d2b7f Mon Sep 17 00:00:00 2001 From: LoneFox Date: Wed, 14 Oct 2015 22:32:38 +0300 Subject: [PATCH] Add TargetsPermanentPredicate and use it for existing cards. Implement card: Hydromorph Gull --- .../sets/riseoftheeldrazi/NotOfThisWorld.java | 110 ++------------ .../mage/sets/scarsofmirrodin/TurnAside.java | 134 ++---------------- .../mage/sets/torment/HydromorphGuardian.java | 40 +----- .../src/mage/sets/torment/HydromorphGull.java | 85 +++++++++++ .../src/mage/sets/urzaslegacy/Intervene.java | 116 ++------------- .../other/TargetsPermanentPredicate.java | 76 ++++++++++ 6 files changed, 200 insertions(+), 361 deletions(-) create mode 100644 Mage.Sets/src/mage/sets/torment/HydromorphGull.java create mode 100644 Mage/src/mage/filter/predicate/other/TargetsPermanentPredicate.java diff --git a/Mage.Sets/src/mage/sets/riseoftheeldrazi/NotOfThisWorld.java b/Mage.Sets/src/mage/sets/riseoftheeldrazi/NotOfThisWorld.java index 558d240f739..1ee4a1ce987 100644 --- a/Mage.Sets/src/mage/sets/riseoftheeldrazi/NotOfThisWorld.java +++ b/Mage.Sets/src/mage/sets/riseoftheeldrazi/NotOfThisWorld.java @@ -27,8 +27,6 @@ */ package mage.sets.riseoftheeldrazi; -import java.util.HashSet; -import java.util.Set; import java.util.UUID; import mage.abilities.Ability; import mage.abilities.common.SimpleStaticAbility; @@ -40,18 +38,17 @@ import mage.constants.CardType; import mage.constants.Rarity; import mage.constants.TargetController; import mage.constants.Zone; -import mage.filter.Filter; +import mage.filter.FilterSpell; +import mage.filter.common.FilterControlledPermanent; import mage.filter.common.FilterCreaturePermanent; import mage.filter.predicate.mageobject.PowerPredicate; +import mage.filter.predicate.other.TargetsPermanentPredicate; import mage.filter.predicate.permanent.ControllerPredicate; import mage.game.Game; import mage.game.permanent.Permanent; -import mage.game.stack.Spell; -import mage.game.stack.StackAbility; import mage.game.stack.StackObject; import mage.target.Target; -import mage.target.TargetObject; -import mage.target.Targets; +import mage.target.TargetSpell; /** * @@ -59,15 +56,19 @@ import mage.target.Targets; */ public class NotOfThisWorld extends CardImpl { + private final static FilterSpell filter = new FilterSpell("spell that targets a permanent you control"); + + static { + filter.add(new TargetsPermanentPredicate(new FilterControlledPermanent())); + } + public NotOfThisWorld(UUID ownerId) { - super(ownerId, 8, "Not of This World", Rarity.UNCOMMON, - new CardType[]{CardType.TRIBAL, CardType.INSTANT}, "{7}"); + super(ownerId, 8, "Not of This World", Rarity.UNCOMMON, new CardType[]{CardType.TRIBAL, CardType.INSTANT}, "{7}"); this.expansionSetCode = "ROE"; this.subtype.add("Eldrazi"); // Counter target spell or ability that targets a permanent you control. - this.getSpellAbility().addTarget( - new TargetStackObjectTargetingControlledPermanent()); + this.getSpellAbility().addTarget(new TargetSpell(filter)); this.getSpellAbility().addEffect(new CounterTargetEffect()); // Not of This World costs {7} less to cast if it targets a spell or ability that targets a creature you control with power 7 or greater. this.addAbility(new SimpleStaticAbility(Zone.STACK, new SpellCostReductionSourceEffect(7, NotOfThisWorldCondition.getInstance()))); @@ -83,93 +84,6 @@ public class NotOfThisWorld extends CardImpl { } } -class TargetStackObjectTargetingControlledPermanent extends TargetObject { - - - public TargetStackObjectTargetingControlledPermanent() { - this.minNumberOfTargets = 1; - this.maxNumberOfTargets = 1; - this.zone = Zone.STACK; - this.targetName = "spell or ability that targets a permanent you control"; - } - - public TargetStackObjectTargetingControlledPermanent(final TargetStackObjectTargetingControlledPermanent target) { - super(target); - } - - @Override - public Filter getFilter() { - throw new UnsupportedOperationException("Not supported."); //To change body of generated methods, choose Tools | Templates. - } - - @Override - public boolean canTarget(UUID id, Ability source, Game game) { - StackObject stackObject = game.getStack().getStackObject(id); - if ((stackObject instanceof Spell) || (stackObject instanceof StackAbility)) { - return true; - } - return false; - } - - @Override - public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) { - return canChoose(sourceControllerId, game); - } - - @Override - public boolean canChoose(UUID sourceControllerId, Game game) { - for (StackObject stackObject : game.getStack()) { - if ((stackObject instanceof Spell) || (stackObject instanceof StackAbility)) { - Targets objectTargets = stackObject.getStackAbility().getTargets(); - if(!objectTargets.isEmpty()) { - for (Target target : objectTargets) { - for (UUID targetId : target.getTargets()) { - Permanent targetedPermanent = game.getPermanentOrLKIBattlefield(targetId); - if (targetedPermanent != null && targetedPermanent.getControllerId().equals(sourceControllerId)) { - return true; - } - } - } - } - } - } - return false; - } - - @Override - public Set possibleTargets(UUID sourceId, UUID sourceControllerId, - Game game) { - return possibleTargets(sourceControllerId, game); - } - - @Override - public Set possibleTargets(UUID sourceControllerId, Game game) { - Set possibleTargets = new HashSet<>(); - for (StackObject stackObject : game.getStack()) { - if ((stackObject instanceof Spell) || (stackObject instanceof StackAbility)) { - Targets objectTargets = stackObject.getStackAbility().getTargets(); - if(!objectTargets.isEmpty()) { - for (Target target : objectTargets) { - for (UUID targetId : target.getTargets()) { - Permanent targetedPermanent = game.getPermanentOrLKIBattlefield(targetId); - if (targetedPermanent != null && targetedPermanent.getControllerId().equals(sourceControllerId)) { - possibleTargets.add(stackObject.getId()); - } - } - } - } - } - } - return possibleTargets; - } - - @Override - public TargetStackObjectTargetingControlledPermanent copy() { - return new TargetStackObjectTargetingControlledPermanent(this); - } - -} - class NotOfThisWorldCondition implements Condition { private static final FilterCreaturePermanent filter = new FilterCreaturePermanent("creature you control with power 7 or greater"); diff --git a/Mage.Sets/src/mage/sets/scarsofmirrodin/TurnAside.java b/Mage.Sets/src/mage/sets/scarsofmirrodin/TurnAside.java index f6f9cb5c4c9..a878638f92d 100644 --- a/Mage.Sets/src/mage/sets/scarsofmirrodin/TurnAside.java +++ b/Mage.Sets/src/mage/sets/scarsofmirrodin/TurnAside.java @@ -28,40 +28,35 @@ package mage.sets.scarsofmirrodin; -import mage.constants.CardType; -import mage.constants.Rarity; -import mage.abilities.Ability; +import java.util.UUID; import mage.abilities.effects.common.CounterTargetEffect; import mage.cards.CardImpl; -import mage.constants.Zone; -import mage.filter.Filter; +import mage.constants.CardType; +import mage.constants.Rarity; import mage.filter.FilterSpell; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.game.stack.Spell; -import mage.game.stack.StackObject; -import mage.target.TargetObject; +import mage.filter.common.FilterControlledPermanent; +import mage.filter.predicate.other.TargetsPermanentPredicate; +import mage.target.TargetSpell; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; -import mage.target.Target; /** * @author ayratn */ public class TurnAside extends CardImpl { - private static FilterSpell filter = new FilterSpell("spell that targets a permanent you control"); + private final static FilterSpell filter = new FilterSpell("spell that targets a permanent you control"); + + static { + filter.add(new TargetsPermanentPredicate(new FilterControlledPermanent())); + } public TurnAside(UUID ownerId) { super(ownerId, 49, "Turn Aside", Rarity.COMMON, new CardType[]{CardType.INSTANT}, "{U}"); this.expansionSetCode = "SOM"; - // Counter target spell that targets a permanent you control. this.getSpellAbility().addEffect(new CounterTargetEffect()); - this.getSpellAbility().addTarget(new CustomTargetSpell(filter)); + this.getSpellAbility().addTarget(new TargetSpell(filter)); } public TurnAside(final TurnAside card) { @@ -72,109 +67,4 @@ public class TurnAside extends CardImpl { public TurnAside copy() { return new TurnAside(this); } - - private class CustomTargetSpell extends TargetObject { - - protected FilterSpell filter; - - public CustomTargetSpell() { - this(1, 1, new FilterSpell()); - } - - public CustomTargetSpell(FilterSpell filter) { - this(1, 1, filter); - } - - public CustomTargetSpell(int numTargets, FilterSpell filter) { - this(numTargets, numTargets, filter); - } - - public CustomTargetSpell(int minNumTargets, int maxNumTargets, FilterSpell filter) { - this.minNumberOfTargets = minNumTargets; - this.maxNumberOfTargets = maxNumTargets; - this.zone = Zone.STACK; - this.filter = filter; - this.targetName = filter.getMessage(); - } - - public CustomTargetSpell(final CustomTargetSpell target) { - super(target); - this.filter = target.filter.copy(); - } - - @Override - public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) { - return canChoose(sourceControllerId, game); - } - - @Override - public Set possibleTargets(UUID sourceId, UUID sourceControllerId, Game game) { - return possibleTargets(sourceControllerId, game); - } - - @Override - public boolean canTarget(UUID id, Ability source, Game game) { - if (super.canTarget(id, source, game)) { - if (targetsMyPermanent(id, source.getControllerId(), game)) { - return true; - } - } - return false; - } - - @Override - public boolean canChoose(UUID sourceControllerId, Game game) { - int count = 0; - for (StackObject stackObject : game.getStack()) { - if (stackObject instanceof Spell && game.getPlayer(sourceControllerId).getInRange().contains(stackObject.getControllerId()) && filter.match((Spell) stackObject, game)) { - if (targetsMyPermanent(stackObject.getId(), sourceControllerId, game)) { - count++; - if (count >= this.minNumberOfTargets) - return true; - } - } - } - return false; - } - - @Override - public Set possibleTargets(UUID sourceControllerId, Game game) { - Set possibleTargets = new HashSet(); - for (StackObject stackObject : game.getStack()) { - if (stackObject instanceof Spell && game.getPlayer(sourceControllerId).getInRange().contains(stackObject.getControllerId()) && filter.match((Spell) stackObject, game)) { - if (targetsMyPermanent(stackObject.getId(), sourceControllerId, game)) { - - possibleTargets.add(stackObject.getId()); - } - } - } - return possibleTargets; - } - - @Override - public Filter getFilter() { - return filter; - } - - private boolean targetsMyPermanent(UUID id, UUID controllerId, Game game) { - StackObject spell = game.getStack().getStackObject(id); - if (spell != null) { - Ability ability = spell.getStackAbility(); - for (Target target : ability.getTargets()) { - for (UUID permanentId : target.getTargets()) { - Permanent permanent = game.getPermanent(permanentId); - if (permanent != null && permanent.getControllerId().equals(controllerId)) { - return true; - } - } - } - } - return false; - } - - @Override - public CustomTargetSpell copy() { - return new CustomTargetSpell(this); - } - } } diff --git a/Mage.Sets/src/mage/sets/torment/HydromorphGuardian.java b/Mage.Sets/src/mage/sets/torment/HydromorphGuardian.java index 261fc1c2e34..0eaaf30cd1f 100644 --- a/Mage.Sets/src/mage/sets/torment/HydromorphGuardian.java +++ b/Mage.Sets/src/mage/sets/torment/HydromorphGuardian.java @@ -29,9 +29,7 @@ package mage.sets.torment; import java.util.UUID; import mage.MageInt; -import mage.MageObject; import mage.abilities.Ability; -import mage.abilities.Mode; import mage.abilities.common.SimpleActivatedAbility; import mage.abilities.costs.common.SacrificeSourceCost; import mage.abilities.costs.mana.ColoredManaCost; @@ -42,12 +40,8 @@ import mage.constants.ColoredManaSymbol; import mage.constants.Rarity; import mage.constants.Zone; import mage.filter.FilterSpell; -import mage.filter.predicate.ObjectSourcePlayer; -import mage.filter.predicate.ObjectSourcePlayerPredicate; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.game.stack.Spell; -import mage.target.Target; +import mage.filter.common.FilterControlledCreaturePermanent; +import mage.filter.predicate.other.TargetsPermanentPredicate; import mage.target.TargetSpell; /** @@ -59,7 +53,7 @@ public class HydromorphGuardian extends CardImpl { private final static FilterSpell filter = new FilterSpell("spell that targets one or more creatures you control"); static { - filter.add(new HydromorphGuardianPredicate()); + filter.add(new TargetsPermanentPredicate(new FilterControlledCreaturePermanent())); } public HydromorphGuardian(UUID ownerId) { @@ -85,31 +79,3 @@ public class HydromorphGuardian extends CardImpl { return new HydromorphGuardian(this); } } - -class HydromorphGuardianPredicate implements ObjectSourcePlayerPredicate> { - - @Override - public boolean apply(ObjectSourcePlayer input, Game game) { - Spell spell = game.getStack().getSpell(input.getObject().getId()); - if (spell != null) { - for (UUID modeId : spell.getSpellAbility().getModes().getSelectedModes()) { - Mode mode = spell.getSpellAbility().getModes().get(modeId); - for (Target target : mode.getTargets()) { - for (UUID targetId : target.getTargets()) { - Permanent permanent = game.getPermanent(targetId); - if (permanent.getCardType().contains(CardType.CREATURE) - && permanent.getControllerId().equals(input.getPlayerId())) { - return true; - } - } - } - } - } - return false; - } - - @Override - public String toString() { - return "that targets one or more creatures you control"; - } -} diff --git a/Mage.Sets/src/mage/sets/torment/HydromorphGull.java b/Mage.Sets/src/mage/sets/torment/HydromorphGull.java new file mode 100644 index 00000000000..c427837724d --- /dev/null +++ b/Mage.Sets/src/mage/sets/torment/HydromorphGull.java @@ -0,0 +1,85 @@ +/* + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ +package mage.sets.torment; + +import java.util.UUID; +import mage.MageInt; +import mage.abilities.Ability; +import mage.abilities.common.SimpleActivatedAbility; +import mage.abilities.costs.common.SacrificeSourceCost; +import mage.abilities.costs.mana.ColoredManaCost; +import mage.abilities.effects.common.CounterTargetEffect; +import mage.abilities.keyword.FlyingAbility; +import mage.cards.CardImpl; +import mage.constants.CardType; +import mage.constants.ColoredManaSymbol; +import mage.constants.Rarity; +import mage.constants.Zone; +import mage.filter.FilterSpell; +import mage.filter.common.FilterControlledCreaturePermanent; +import mage.filter.predicate.other.TargetsPermanentPredicate; +import mage.target.TargetSpell; + +/** + * + * @author LoneFox + */ +public class HydromorphGull extends CardImpl { + + private final static FilterSpell filter = new FilterSpell("spell that targets one or more creatures you control"); + + static { + filter.add(new TargetsPermanentPredicate(new FilterControlledCreaturePermanent())); + } + + public HydromorphGull(UUID ownerId) { + super(ownerId, 40, "Hydromorph Gull", Rarity.UNCOMMON, new CardType[]{CardType.CREATURE}, "{3}{U}{U}"); + this.expansionSetCode = "TOR"; + this.subtype.add("Elemental"); + this.subtype.add("Bird"); + this.power = new MageInt(3); + this.toughness = new MageInt(3); + + // Flying + this.addAbility(FlyingAbility.getInstance()); + // {U}, Sacrifice Hydromorph Gull: Counter target spell that targets one or more creatures you control. + Ability ability = new SimpleActivatedAbility(Zone.BATTLEFIELD, new CounterTargetEffect(), new ColoredManaCost(ColoredManaSymbol.U)); + ability.addCost(new SacrificeSourceCost()); + ability.addTarget(new TargetSpell(filter)); + this.addAbility(ability); + } + + public HydromorphGull(final HydromorphGull card) { + super(card); + } + + @Override + public HydromorphGull copy() { + return new HydromorphGull(this); + } +} diff --git a/Mage.Sets/src/mage/sets/urzaslegacy/Intervene.java b/Mage.Sets/src/mage/sets/urzaslegacy/Intervene.java index 2713856744f..aa35fd116bd 100644 --- a/Mage.Sets/src/mage/sets/urzaslegacy/Intervene.java +++ b/Mage.Sets/src/mage/sets/urzaslegacy/Intervene.java @@ -27,24 +27,15 @@ */ package mage.sets.urzaslegacy; -import java.util.HashSet; -import java.util.Set; import java.util.UUID; - -import mage.constants.CardType; -import mage.constants.Rarity; -import mage.abilities.Ability; import mage.abilities.effects.common.CounterTargetEffect; import mage.cards.CardImpl; -import mage.constants.Zone; -import mage.filter.Filter; +import mage.constants.CardType; +import mage.constants.Rarity; import mage.filter.FilterSpell; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.game.stack.Spell; -import mage.game.stack.StackObject; -import mage.target.Target; -import mage.target.TargetObject; +import mage.filter.common.FilterCreaturePermanent; +import mage.filter.predicate.other.TargetsPermanentPredicate; +import mage.target.TargetSpell; /** * @@ -52,13 +43,19 @@ import mage.target.TargetObject; */ public class Intervene extends CardImpl { + private final static FilterSpell filter = new FilterSpell("spell that targets a creature"); + + static { + filter.add(new TargetsPermanentPredicate(new FilterCreaturePermanent())); + } + public Intervene(UUID ownerId) { super(ownerId, 33, "Intervene", Rarity.COMMON, new CardType[]{CardType.INSTANT}, "{U}"); this.expansionSetCode = "ULG"; // Counter target spell that targets a creature. - this.getSpellAbility().addTarget(new InterveneTargetSpell()); + this.getSpellAbility().addTarget(new TargetSpell(filter)); this.getSpellAbility().addEffect(new CounterTargetEffect()); } @@ -70,93 +67,4 @@ public class Intervene extends CardImpl { public Intervene copy() { return new Intervene(this); } - - private class InterveneTargetSpell extends TargetObject { - - - public InterveneTargetSpell() { - super(1, Zone.STACK); - this.targetName = "spell that targets a creature"; - } - - public InterveneTargetSpell(final InterveneTargetSpell target) { - super(target); - } - - @Override - public boolean canChoose(UUID sourceId, UUID sourceControllerId, Game game) { - return canChoose(sourceControllerId, game); - } - - @Override - public Set possibleTargets(UUID sourceId, UUID sourceControllerId, Game game) { - return possibleTargets(sourceControllerId, game); - } - - @Override - public boolean canTarget(UUID id, Ability source, Game game) { - if (super.canTarget(id, source, game)) { - if (targetsCreature(id, game)) { - return true; - } - } - return false; - } - - @Override - public boolean canChoose(UUID sourceControllerId, Game game) { - for (StackObject stackObject : game.getStack()) { - if (stackObject instanceof Spell) { - if (targetsCreature(stackObject.getId(), game)) { - return true; - } - } - } - return false; - } - - @Override - public Set possibleTargets(UUID sourceControllerId, Game game) { - Set possibleTargets = new HashSet(); - for (StackObject stackObject : game.getStack()) { - if (stackObject instanceof Spell) { - if (targetsCreature(stackObject.getId(), game)) { - possibleTargets.add(stackObject.getId()); - } - } - } - return possibleTargets; - } - - - private boolean targetsCreature(UUID id, Game game) { - StackObject spell = game.getStack().getStackObject(id); - if (spell != null) { - Ability ability = spell.getStackAbility(); - if (ability != null && !ability.getTargets().isEmpty()) { - for (Target target : ability.getTargets()) { - for (UUID targetId : target.getTargets()) { - Permanent permanent = game.getPermanent(targetId); - if (permanent != null && permanent.getCardType().contains(CardType.CREATURE)) { - return true; - } - } - } - } - } - return false; - } - - @Override - public InterveneTargetSpell copy() { - return new InterveneTargetSpell(this); - } - - @Override - public Filter getFilter() { - return new FilterSpell(); - } - } } - - diff --git a/Mage/src/mage/filter/predicate/other/TargetsPermanentPredicate.java b/Mage/src/mage/filter/predicate/other/TargetsPermanentPredicate.java new file mode 100644 index 00000000000..0cf864bbc42 --- /dev/null +++ b/Mage/src/mage/filter/predicate/other/TargetsPermanentPredicate.java @@ -0,0 +1,76 @@ +/* + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ +package mage.filter.predicate.other; + +import java.util.UUID; +import mage.MageObject; +import mage.abilities.Mode; +import mage.filter.FilterPermanent; +import mage.filter.predicate.ObjectSourcePlayer; +import mage.filter.predicate.ObjectSourcePlayerPredicate; +import mage.game.Game; +import mage.game.permanent.Permanent; +import mage.game.stack.StackObject; +import mage.target.Target; + +/** + * + * @author LoneFox + */ +public class TargetsPermanentPredicate implements ObjectSourcePlayerPredicate> { + + private final FilterPermanent targetFilter; + + public TargetsPermanentPredicate(FilterPermanent targetFilter) { + this.targetFilter = targetFilter; + } + + @Override + public boolean apply(ObjectSourcePlayer input, Game game) { + StackObject object = game.getStack().getStackObject(input.getObject().getId()); + if(object != null) { + for(UUID modeId : object.getStackAbility().getModes().getSelectedModes()) { + Mode mode = object.getStackAbility().getModes().get(modeId); + for(Target target : mode.getTargets()) { + for(UUID targetId : target.getTargets()) { + Permanent permanent = game.getPermanentOrLKIBattlefield(targetId); + if(permanent != null) { + return targetFilter.match(permanent, input.getSourceId(), input.getPlayerId(), game); + } + } + } + } + } + return false; + } + + @Override + public String toString() { + return "that targets " + targetFilter.getMessage(); + } +}