From 8463d693d41bdb37bcd9ca7f62ff1062b0fc047e Mon Sep 17 00:00:00 2001 From: Evan Kranzler Date: Sat, 5 Jan 2019 13:41:34 -0500 Subject: [PATCH] Condensed "targets a permanent you control" conditions into a single class --- Mage.Sets/src/mage/cards/p/PriceOfFame.java | 55 +++++++------------ Mage.Sets/src/mage/cards/s/SavageStomp.java | 44 ++++----------- Mage.Sets/src/mage/cards/t/TitanicBrawl.java | 48 ++++------------ .../SourceTargetsPermanentCondition.java | 45 +++++++++++++++ 4 files changed, 86 insertions(+), 106 deletions(-) create mode 100644 Mage/src/main/java/mage/abilities/condition/common/SourceTargetsPermanentCondition.java diff --git a/Mage.Sets/src/mage/cards/p/PriceOfFame.java b/Mage.Sets/src/mage/cards/p/PriceOfFame.java index 79f7737b9e..194d35fe99 100644 --- a/Mage.Sets/src/mage/cards/p/PriceOfFame.java +++ b/Mage.Sets/src/mage/cards/p/PriceOfFame.java @@ -1,36 +1,44 @@ package mage.cards.p; -import java.util.Iterator; -import java.util.UUID; -import mage.abilities.Ability; import mage.abilities.common.SimpleStaticAbility; import mage.abilities.condition.Condition; +import mage.abilities.condition.common.SourceTargetsPermanentCondition; import mage.abilities.effects.common.DestroyTargetEffect; import mage.abilities.effects.common.cost.SpellCostReductionSourceEffect; import mage.abilities.effects.keyword.SurveilEffect; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; +import mage.constants.SuperType; import mage.constants.Zone; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.game.stack.StackObject; -import mage.target.Target; +import mage.filter.FilterPermanent; +import mage.filter.common.FilterControlledCreaturePermanent; +import mage.filter.predicate.mageobject.SupertypePredicate; import mage.target.common.TargetCreaturePermanent; +import java.util.UUID; + /** - * * @author TheElk801 */ public final class PriceOfFame extends CardImpl { + private static final FilterPermanent filter + = new FilterControlledCreaturePermanent("a legendary creature"); + + static { + filter.add(new SupertypePredicate(SuperType.LEGENDARY)); + } + + private static final Condition condition = new SourceTargetsPermanentCondition(filter); + public PriceOfFame(UUID ownerId, CardSetInfo setInfo) { super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{3}{B}"); // This spell costs {2} less to cast if it targets a legendary creature. - this.addAbility(new SimpleStaticAbility(Zone.STACK, - new SpellCostReductionSourceEffect(2, PriceOfFameCondition.instance)) - .setRuleAtTheTop(true)); + this.addAbility(new SimpleStaticAbility( + Zone.STACK, new SpellCostReductionSourceEffect(2, condition) + ).setRuleAtTheTop(true)); // Destroy target creature. this.getSpellAbility().addEffect(new DestroyTargetEffect()); @@ -49,28 +57,3 @@ public final class PriceOfFame extends CardImpl { return new PriceOfFame(this); } } - -enum PriceOfFameCondition implements Condition { - instance; - - @Override - public boolean apply(Game game, Ability source) { - StackObject sourceSpell = game.getStack().getStackObject(source.getSourceId()); - if (sourceSpell != null) { - Iterator targets = sourceSpell.getStackAbility().getTargets().iterator(); - while (targets.hasNext()) { - Permanent permanent = game.getPermanentOrLKIBattlefield(targets.next().getFirstTarget()); - if (permanent != null && permanent.isCreature() && permanent.isLegendary()) { - return true; - } - } - } - return false; - } - - @Override - public String toString() { - return "it targets a legendary creature"; - } - -} diff --git a/Mage.Sets/src/mage/cards/s/SavageStomp.java b/Mage.Sets/src/mage/cards/s/SavageStomp.java index 38dfe0ebf8..0b03f1085e 100644 --- a/Mage.Sets/src/mage/cards/s/SavageStomp.java +++ b/Mage.Sets/src/mage/cards/s/SavageStomp.java @@ -1,9 +1,9 @@ package mage.cards.s; -import mage.abilities.Ability; import mage.abilities.common.SimpleStaticAbility; import mage.abilities.condition.Condition; +import mage.abilities.condition.common.SourceTargetsPermanentCondition; import mage.abilities.effects.Effect; import mage.abilities.effects.common.FightTargetsEffect; import mage.abilities.effects.common.cost.SpellCostReductionSourceEffect; @@ -15,16 +15,14 @@ import mage.constants.SubType; import mage.constants.TargetController; import mage.constants.Zone; import mage.counters.CounterType; +import mage.filter.FilterPermanent; +import mage.filter.common.FilterControlledCreaturePermanent; import mage.filter.common.FilterCreaturePermanent; import mage.filter.predicate.permanent.ControllerPredicate; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.game.stack.StackObject; import mage.target.Target; import mage.target.common.TargetControlledCreaturePermanent; import mage.target.common.TargetCreaturePermanent; -import java.util.Iterator; import java.util.UUID; /** @@ -34,18 +32,22 @@ public final class SavageStomp extends CardImpl { private static final FilterCreaturePermanent filter = new FilterCreaturePermanent("creature you don't control"); + private static final FilterPermanent filter2 + = new FilterControlledCreaturePermanent(SubType.DINOSAUR, "a Dinosaur you control"); static { filter.add(new ControllerPredicate(TargetController.NOT_YOU)); } + private static final Condition condition = new SourceTargetsPermanentCondition(filter2); + public SavageStomp(UUID ownerId, CardSetInfo setInfo) { super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{2}{G}"); // Savage Stomp costs {2} less to cast if it targets a Dinosaur you control. - this.addAbility(new SimpleStaticAbility(Zone.STACK, - new SpellCostReductionSourceEffect(2, SavageStompCondition.instance)) - .setRuleAtTheTop(true)); + this.addAbility(new SimpleStaticAbility( + Zone.STACK, new SpellCostReductionSourceEffect(2, condition) + ).setRuleAtTheTop(true)); // Put a +1/+1 counter on target creature you control. Then that creature fights target creature you don't control. Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance()); @@ -67,29 +69,3 @@ public final class SavageStomp extends CardImpl { return new SavageStomp(this); } } - -enum SavageStompCondition implements Condition { - instance; - - @Override - public boolean apply(Game game, Ability source) { - StackObject sourceSpell = game.getStack().getStackObject(source.getSourceId()); - if (sourceSpell == null) { - return false; - } - Iterator targets = sourceSpell.getStackAbility().getTargets().iterator(); - while (targets.hasNext()) { - Permanent permanent = game.getPermanentOrLKIBattlefield(targets.next().getFirstTarget()); - if (permanent != null && permanent.hasSubtype(SubType.DINOSAUR, game) - && permanent.isControlledBy(source.getControllerId())) { - return true; - } - } - return false; - } - - @Override - public String toString() { - return "it targets a Dinosaur you control"; - } -} diff --git a/Mage.Sets/src/mage/cards/t/TitanicBrawl.java b/Mage.Sets/src/mage/cards/t/TitanicBrawl.java index 0e097fdc1d..4a5aaf85cd 100644 --- a/Mage.Sets/src/mage/cards/t/TitanicBrawl.java +++ b/Mage.Sets/src/mage/cards/t/TitanicBrawl.java @@ -1,8 +1,8 @@ package mage.cards.t; -import mage.abilities.Ability; import mage.abilities.common.SimpleStaticAbility; import mage.abilities.condition.Condition; +import mage.abilities.condition.common.SourceTargetsPermanentCondition; import mage.abilities.effects.common.FightTargetsEffect; import mage.abilities.effects.common.cost.SpellCostReductionSourceEffect; import mage.cards.CardImpl; @@ -11,16 +11,14 @@ import mage.constants.CardType; import mage.constants.TargetController; import mage.constants.Zone; import mage.counters.CounterType; +import mage.filter.FilterPermanent; +import mage.filter.common.FilterControlledCreaturePermanent; import mage.filter.common.FilterCreaturePermanent; import mage.filter.predicate.permanent.ControllerPredicate; -import mage.game.Game; -import mage.game.permanent.Permanent; -import mage.game.stack.StackObject; -import mage.target.Target; +import mage.filter.predicate.permanent.CounterPredicate; import mage.target.common.TargetControlledCreaturePermanent; import mage.target.common.TargetCreaturePermanent; -import java.util.Iterator; import java.util.UUID; /** @@ -30,18 +28,23 @@ public final class TitanicBrawl extends CardImpl { private static final FilterCreaturePermanent filter = new FilterCreaturePermanent("creature you don't control"); + private static final FilterPermanent filter2 + = new FilterControlledCreaturePermanent("a creature you control with a +1/+1 counter on it"); static { filter.add(new ControllerPredicate(TargetController.NOT_YOU)); + filter2.add(new CounterPredicate(CounterType.P1P1)); } + private static final Condition condition = new SourceTargetsPermanentCondition(filter2); + public TitanicBrawl(UUID ownerId, CardSetInfo setInfo) { super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{1}{G}"); // This spell costs {1} less to cast if it targets a creature you control with a +1/+1 counter on it. - this.addAbility(new SimpleStaticAbility(Zone.STACK, - new SpellCostReductionSourceEffect(1, TitanicBrawlCondition.instance)) - .setRuleAtTheTop(true)); + this.addAbility(new SimpleStaticAbility( + Zone.STACK, new SpellCostReductionSourceEffect(1, condition) + ).setRuleAtTheTop(true)); // Target creature you control fights target creature you don't control. this.getSpellAbility().addEffect(new FightTargetsEffect()); @@ -58,30 +61,3 @@ public final class TitanicBrawl extends CardImpl { return new TitanicBrawl(this); } } - -enum TitanicBrawlCondition implements Condition { - instance; - - @Override - public boolean apply(Game game, Ability source) { - StackObject sourceSpell = game.getStack().getStackObject(source.getSourceId()); - if (sourceSpell == null) { - return false; - } - Iterator targets = sourceSpell.getStackAbility().getTargets().iterator(); - while (targets.hasNext()) { - Permanent permanent = game.getPermanentOrLKIBattlefield(targets.next().getFirstTarget()); - if (permanent != null && permanent.getCounters(game).containsKey(CounterType.P1P1) - && permanent.isControlledBy(source.getControllerId())) { - return true; - } - } - return false; - } - - @Override - public String toString() { - return "it targets a creature you control with a +1/+1 counter on it"; - } - -} diff --git a/Mage/src/main/java/mage/abilities/condition/common/SourceTargetsPermanentCondition.java b/Mage/src/main/java/mage/abilities/condition/common/SourceTargetsPermanentCondition.java new file mode 100644 index 0000000000..b4e3697752 --- /dev/null +++ b/Mage/src/main/java/mage/abilities/condition/common/SourceTargetsPermanentCondition.java @@ -0,0 +1,45 @@ +package mage.abilities.condition.common; + +import mage.abilities.Ability; +import mage.abilities.condition.Condition; +import mage.filter.FilterPermanent; +import mage.game.Game; +import mage.game.permanent.Permanent; +import mage.game.stack.StackObject; +import mage.target.Target; + +import java.util.Iterator; + +/** + * @author TheElk801 + */ +public class SourceTargetsPermanentCondition implements Condition { + + private final FilterPermanent filter; + + public SourceTargetsPermanentCondition(FilterPermanent filter) { + this.filter = filter; + } + + @Override + public boolean apply(Game game, Ability source) { + StackObject sourceSpell = game.getStack().getStackObject(source.getSourceId()); + if (sourceSpell == null) { + return false; + } + Iterator targets = sourceSpell.getStackAbility().getTargets().iterator(); + while (targets.hasNext()) { + Permanent permanent = game.getPermanentOrLKIBattlefield(targets.next().getFirstTarget()); + if (permanent != null && filter.match(permanent, source.getSourceId(), source.getControllerId(), game)) { + return true; + } + } + return false; + } + + @Override + public String toString() { + return "it targets " + filter.getMessage(); + } + +}