From e7b5f9ba0495eb5c2e398600ccf652837dc8a927 Mon Sep 17 00:00:00 2001 From: theelk801 Date: Thu, 19 Jun 2025 17:11:18 -0400 Subject: [PATCH] refactor classes which extend TargetCreaturePermanent --- .../src/mage/cards/t/TemporaryInsanity.java | 74 +++++----------- Mage.Sets/src/mage/cards/t/Topple.java | 84 +++++-------------- 2 files changed, 42 insertions(+), 116 deletions(-) diff --git a/Mage.Sets/src/mage/cards/t/TemporaryInsanity.java b/Mage.Sets/src/mage/cards/t/TemporaryInsanity.java index 514db387a07..f6044020648 100644 --- a/Mage.Sets/src/mage/cards/t/TemporaryInsanity.java +++ b/Mage.Sets/src/mage/cards/t/TemporaryInsanity.java @@ -1,9 +1,5 @@ - package mage.cards.t; -import mage.MageObject; -import mage.abilities.Ability; -import mage.abilities.effects.Effect; import mage.abilities.effects.common.UntapTargetEffect; import mage.abilities.effects.common.continuous.GainAbilityTargetEffect; import mage.abilities.effects.common.continuous.GainControlTargetEffect; @@ -12,10 +8,17 @@ import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; import mage.constants.Duration; +import mage.filter.FilterPermanent; +import mage.filter.common.FilterCreaturePermanent; +import mage.filter.predicate.ObjectSourcePlayer; +import mage.filter.predicate.ObjectSourcePlayerPredicate; import mage.game.Game; import mage.game.permanent.Permanent; -import mage.target.common.TargetCreaturePermanent; +import mage.players.Player; +import mage.target.TargetPermanent; +import java.util.HashSet; +import java.util.Optional; import java.util.UUID; /** @@ -23,22 +26,20 @@ import java.util.UUID; */ public final class TemporaryInsanity extends CardImpl { + private static final FilterPermanent filter = new FilterCreaturePermanent("creature with power less than the number of cards in your graveyard"); + public TemporaryInsanity(UUID ownerId, CardSetInfo setInfo) { super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{3}{R}"); // Untap target creature with power less than the number of cards in your graveyard - this.getSpellAbility().addTarget(new TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard()); this.getSpellAbility().addEffect(new UntapTargetEffect()); + this.getSpellAbility().addTarget(new TargetPermanent(filter)); // and gain control of it until end of turn. - Effect effect = new GainControlTargetEffect(Duration.EndOfTurn); - effect.setText("and gain control of it until end of turn"); - this.getSpellAbility().addEffect(effect); + this.getSpellAbility().addEffect(new GainControlTargetEffect(Duration.EndOfTurn).setText("and gain control of it until end of turn")); // That creature gains haste until end of turn. - effect = new GainAbilityTargetEffect(HasteAbility.getInstance(), Duration.EndOfTurn); - effect.setText("That creature gains haste until end of turn."); - this.getSpellAbility().addEffect(effect); + this.getSpellAbility().addEffect(new GainAbilityTargetEffect(HasteAbility.getInstance(), Duration.EndOfTurn).setText("That creature gains haste until end of turn.")); } private TemporaryInsanity(final TemporaryInsanity card) { @@ -51,46 +52,17 @@ public final class TemporaryInsanity extends CardImpl { } } -class TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard extends TargetCreaturePermanent { - - public TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard() { - super(); - targetName = "creature with power less than the number of cards in your graveyard"; - } - - private TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard(final TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard target) { - super(target); - } +enum TemporaryInsanityPredicate implements ObjectSourcePlayerPredicate { + instance; @Override - public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) { - if (super.canTarget(controllerId, id, source, game)) { - Permanent target = game.getPermanent(id); - if (target != null) { - return target.getPower().getValue() < game.getPlayer(source.getControllerId()).getGraveyard().size(); - } - return false; - } - return false; - } - - @Override - public boolean canChoose(UUID sourceControllerId, Ability source, Game game) { - MageObject targetSource = game.getObject(source); - if (targetSource != null) { - for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, sourceControllerId, source, game)) { - if (permanent.canBeTargetedBy(targetSource, sourceControllerId, source, game)) { - if (permanent.getPower().getValue() < game.getPlayer(sourceControllerId).getGraveyard().size()) { - return true; - } - } - } - } - return false; - } - - @Override - public TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard copy() { - return new TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard(this); + public boolean apply(ObjectSourcePlayer input, Game game) { + return Optional + .ofNullable(input.getPlayerId()) + .map(game::getPlayer) + .map(Player::getGraveyard) + .map(HashSet::size) + .filter(x -> input.getObject().getPower().getValue() < x) + .isPresent(); } } diff --git a/Mage.Sets/src/mage/cards/t/Topple.java b/Mage.Sets/src/mage/cards/t/Topple.java index b5c3d5eb9c9..04b93b63c86 100644 --- a/Mage.Sets/src/mage/cards/t/Topple.java +++ b/Mage.Sets/src/mage/cards/t/Topple.java @@ -1,18 +1,18 @@ package mage.cards.t; -import mage.MageObject; -import mage.abilities.Ability; +import mage.abilities.dynamicvalue.common.GreatestAmongPermanentsValue; import mage.abilities.effects.common.ExileTargetEffect; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.CardType; +import mage.filter.FilterPermanent; +import mage.filter.common.FilterCreaturePermanent; +import mage.filter.predicate.ObjectSourcePlayer; +import mage.filter.predicate.ObjectSourcePlayerPredicate; import mage.game.Game; import mage.game.permanent.Permanent; -import mage.target.common.TargetCreaturePermanent; +import mage.target.TargetPermanent; -import java.util.HashSet; -import java.util.List; -import java.util.Set; import java.util.UUID; /** @@ -20,12 +20,19 @@ import java.util.UUID; */ public final class Topple extends CardImpl { + private static final FilterPermanent filter + = new FilterCreaturePermanent("creature with the greatest power among creatures on the battlefield"); + + static { + filter.add(TopplePredicate.instance); + } + public Topple(UUID ownerId, CardSetInfo setInfo) { super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{2}{W}"); // Exile target creature with the greatest power among creatures on the battlefield. this.getSpellAbility().addEffect(new ExileTargetEffect()); - this.getSpellAbility().addTarget(new ToppleTargetCreature()); + this.getSpellAbility().addTarget(new TargetPermanent(filter)); } private Topple(final Topple card) { @@ -38,65 +45,12 @@ public final class Topple extends CardImpl { } } -class ToppleTargetCreature extends TargetCreaturePermanent { - - public ToppleTargetCreature() { - super(); - withTargetName("creature with the greatest power among creatures on the battlefield"); - } - - private ToppleTargetCreature(final ToppleTargetCreature target) { - super(target); - } +enum TopplePredicate implements ObjectSourcePlayerPredicate { + instance; @Override - public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) { - if (super.canTarget(controllerId, id, source, game)) { - int maxPower = 0; - for (Permanent permanent : game.getBattlefield().getActivePermanents(filter, source.getControllerId(), source, game)) { - if (permanent.getPower().getValue() > maxPower) { - maxPower = permanent.getPower().getValue(); - } - } - Permanent targetPermanent = game.getPermanent(id); - if (targetPermanent != null) { - return targetPermanent.getPower().getValue() == maxPower; - } - } - return false; - } - - @Override - public Set possibleTargets(UUID sourceControllerId, Ability source, Game game) { - int maxPower = 0; - List activePermanents = game.getBattlefield().getActivePermanents(filter, sourceControllerId, source, game); - Set possibleTargets = new HashSet<>(); - MageObject targetSource = game.getObject(source); - if (targetSource == null) { - return possibleTargets; - } - for (Permanent permanent : activePermanents) { - if (permanent.getPower().getValue() > maxPower) { - maxPower = permanent.getPower().getValue(); - } - } - for (Permanent permanent : activePermanents) { - if (!targets.containsKey(permanent.getId()) && permanent.canBeTargetedBy(targetSource, sourceControllerId, source, game)) { - if (permanent.getPower().getValue() == maxPower) { - possibleTargets.add(permanent.getId()); - } - } - } - return possibleTargets; - } - - @Override - public boolean canChoose(UUID sourceControllerId, Ability source, Game game) { - return !possibleTargets(sourceControllerId, source, game).isEmpty(); - } - - @Override - public ToppleTargetCreature copy() { - return new ToppleTargetCreature(this); + public boolean apply(ObjectSourcePlayer input, Game game) { + return input.getObject().getPower().getValue() + >= GreatestAmongPermanentsValue.POWER_ALL_CREATURES.calculate(game, input.getSource(), null); } }