refactor classes which extend TargetCreaturePermanent

This commit is contained in:
theelk801 2025-06-19 17:11:18 -04:00
parent 9744819551
commit e7b5f9ba04
2 changed files with 42 additions and 116 deletions

View file

@ -1,9 +1,5 @@
package mage.cards.t; 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.UntapTargetEffect;
import mage.abilities.effects.common.continuous.GainAbilityTargetEffect; import mage.abilities.effects.common.continuous.GainAbilityTargetEffect;
import mage.abilities.effects.common.continuous.GainControlTargetEffect; import mage.abilities.effects.common.continuous.GainControlTargetEffect;
@ -12,10 +8,17 @@ import mage.cards.CardImpl;
import mage.cards.CardSetInfo; import mage.cards.CardSetInfo;
import mage.constants.CardType; import mage.constants.CardType;
import mage.constants.Duration; 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.Game;
import mage.game.permanent.Permanent; 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; import java.util.UUID;
/** /**
@ -23,22 +26,20 @@ import java.util.UUID;
*/ */
public final class TemporaryInsanity extends CardImpl { 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) { public TemporaryInsanity(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{3}{R}"); super(ownerId, setInfo, new CardType[]{CardType.INSTANT}, "{3}{R}");
// Untap target creature with power less than the number of cards in your graveyard // 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().addEffect(new UntapTargetEffect());
this.getSpellAbility().addTarget(new TargetPermanent(filter));
// and gain control of it until end of turn. // and gain control of it until end of turn.
Effect effect = new GainControlTargetEffect(Duration.EndOfTurn); this.getSpellAbility().addEffect(new GainControlTargetEffect(Duration.EndOfTurn).setText("and gain control of it until end of turn"));
effect.setText("and gain control of it until end of turn");
this.getSpellAbility().addEffect(effect);
// That creature gains haste until end of turn. // That creature gains haste until end of turn.
effect = new GainAbilityTargetEffect(HasteAbility.getInstance(), Duration.EndOfTurn); this.getSpellAbility().addEffect(new GainAbilityTargetEffect(HasteAbility.getInstance(), Duration.EndOfTurn).setText("That creature gains haste until end of turn."));
effect.setText("That creature gains haste until end of turn.");
this.getSpellAbility().addEffect(effect);
} }
private TemporaryInsanity(final TemporaryInsanity card) { private TemporaryInsanity(final TemporaryInsanity card) {
@ -51,46 +52,17 @@ public final class TemporaryInsanity extends CardImpl {
} }
} }
class TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard extends TargetCreaturePermanent { enum TemporaryInsanityPredicate implements ObjectSourcePlayerPredicate<Permanent> {
instance;
public TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard() {
super();
targetName = "creature with power less than the number of cards in your graveyard";
}
private TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard(final TargetCreatureWithPowerLessThanNumberOfCardsInYourGraveyard target) {
super(target);
}
@Override @Override
public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) { public boolean apply(ObjectSourcePlayer<Permanent> input, Game game) {
if (super.canTarget(controllerId, id, source, game)) { return Optional
Permanent target = game.getPermanent(id); .ofNullable(input.getPlayerId())
if (target != null) { .map(game::getPlayer)
return target.getPower().getValue() < game.getPlayer(source.getControllerId()).getGraveyard().size(); .map(Player::getGraveyard)
} .map(HashSet::size)
return false; .filter(x -> input.getObject().getPower().getValue() < x)
} .isPresent();
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);
} }
} }

View file

@ -1,18 +1,18 @@
package mage.cards.t; package mage.cards.t;
import mage.MageObject; import mage.abilities.dynamicvalue.common.GreatestAmongPermanentsValue;
import mage.abilities.Ability;
import mage.abilities.effects.common.ExileTargetEffect; import mage.abilities.effects.common.ExileTargetEffect;
import mage.cards.CardImpl; import mage.cards.CardImpl;
import mage.cards.CardSetInfo; import mage.cards.CardSetInfo;
import mage.constants.CardType; 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.Game;
import mage.game.permanent.Permanent; 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; import java.util.UUID;
/** /**
@ -20,12 +20,19 @@ import java.util.UUID;
*/ */
public final class Topple extends CardImpl { 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) { public Topple(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{2}{W}"); super(ownerId, setInfo, new CardType[]{CardType.SORCERY}, "{2}{W}");
// Exile target creature with the greatest power among creatures on the battlefield. // Exile target creature with the greatest power among creatures on the battlefield.
this.getSpellAbility().addEffect(new ExileTargetEffect()); this.getSpellAbility().addEffect(new ExileTargetEffect());
this.getSpellAbility().addTarget(new ToppleTargetCreature()); this.getSpellAbility().addTarget(new TargetPermanent(filter));
} }
private Topple(final Topple card) { private Topple(final Topple card) {
@ -38,65 +45,12 @@ public final class Topple extends CardImpl {
} }
} }
class ToppleTargetCreature extends TargetCreaturePermanent { enum TopplePredicate implements ObjectSourcePlayerPredicate<Permanent> {
instance;
public ToppleTargetCreature() {
super();
withTargetName("creature with the greatest power among creatures on the battlefield");
}
private ToppleTargetCreature(final ToppleTargetCreature target) {
super(target);
}
@Override @Override
public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) { public boolean apply(ObjectSourcePlayer<Permanent> input, Game game) {
if (super.canTarget(controllerId, id, source, game)) { return input.getObject().getPower().getValue()
int maxPower = 0; >= GreatestAmongPermanentsValue.POWER_ALL_CREATURES.calculate(game, input.getSource(), null);
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<UUID> possibleTargets(UUID sourceControllerId, Ability source, Game game) {
int maxPower = 0;
List<Permanent> activePermanents = game.getBattlefield().getActivePermanents(filter, sourceControllerId, source, game);
Set<UUID> 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);
} }
} }