use some staticfilters, rewrite some lines to java8 streams

This commit is contained in:
igoudt 2018-01-07 22:38:54 +01:00
parent 2564f61182
commit 6d16e41ec3
7 changed files with 37 additions and 63 deletions

View file

@ -27,20 +27,21 @@
*/ */
package mage.cards.a; package mage.cards.a;
import java.util.UUID;
import mage.abilities.effects.common.CounterTargetEffect; import mage.abilities.effects.common.CounterTargetEffect;
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.constants.SubType; import mage.constants.SubType;
import mage.filter.FilterSpell; import mage.filter.FilterSpell;
import mage.filter.common.FilterControlledPermanent; import mage.filter.StaticFilters;
import mage.filter.predicate.Predicates; import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.CardTypePredicate; import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.mageobject.SubtypePredicate; import mage.filter.predicate.mageobject.SubtypePredicate;
import mage.filter.predicate.other.TargetsPermanentPredicate; import mage.filter.predicate.other.TargetsPermanentPredicate;
import mage.target.TargetSpell; import mage.target.TargetSpell;
import java.util.UUID;
/** /**
* *
* @author LoneFox * @author LoneFox
@ -51,7 +52,7 @@ public class AvoidFate extends CardImpl {
static { static {
filter.add(Predicates.or(new CardTypePredicate(CardType.INSTANT), new SubtypePredicate(SubType.AURA))); filter.add(Predicates.or(new CardTypePredicate(CardType.INSTANT), new SubtypePredicate(SubType.AURA)));
filter.add(new TargetsPermanentPredicate(new FilterControlledPermanent())); filter.add(new TargetsPermanentPredicate(StaticFilters.FILTER_CONTROLLED_PERMANENT));
} }
public AvoidFate(UUID ownerId, CardSetInfo setInfo) { public AvoidFate(UUID ownerId, CardSetInfo setInfo) {

View file

@ -27,7 +27,6 @@
*/ */
package mage.cards.c; package mage.cards.c;
import java.util.UUID;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.common.BeginningOfUpkeepTriggeredAbility; import mage.abilities.common.BeginningOfUpkeepTriggeredAbility;
import mage.abilities.condition.common.SuspendedCondition; import mage.abilities.condition.common.SuspendedCondition;
@ -46,14 +45,17 @@ import mage.constants.Outcome;
import mage.constants.TargetController; import mage.constants.TargetController;
import mage.constants.Zone; import mage.constants.Zone;
import mage.counters.CounterType; import mage.counters.CounterType;
import mage.filter.StaticFilters;
import mage.filter.common.FilterControlledPermanent; import mage.filter.common.FilterControlledPermanent;
import mage.game.Game; import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player; import mage.players.Player;
import mage.target.Target; import mage.target.Target;
import mage.target.TargetPlayer; import mage.target.TargetPlayer;
import mage.target.common.TargetControlledPermanent; import mage.target.common.TargetControlledPermanent;
import java.util.Objects;
import java.util.UUID;
/** /**
* *
* @author anonymous * @author anonymous
@ -84,8 +86,6 @@ public class CurseOfTheCabal extends CardImpl {
class CurseOfTheCabalSacrificeEffect extends OneShotEffect { class CurseOfTheCabalSacrificeEffect extends OneShotEffect {
private static final FilterControlledPermanent FILTER = new FilterControlledPermanent(); // ggf filter.FilterPermanent
public CurseOfTheCabalSacrificeEffect() { public CurseOfTheCabalSacrificeEffect() {
super(Outcome.Sacrifice); super(Outcome.Sacrifice);
this.staticText = "Target player sacrifices half the permanents he or she controls, rounded down."; this.staticText = "Target player sacrifices half the permanents he or she controls, rounded down.";
@ -104,21 +104,20 @@ class CurseOfTheCabalSacrificeEffect extends OneShotEffect {
public boolean apply(Game game, Ability source) { public boolean apply(Game game, Ability source) {
Player targetPlayer = game.getPlayer(source.getFirstTarget()); Player targetPlayer = game.getPlayer(source.getFirstTarget());
if (targetPlayer != null) { if (targetPlayer != null) {
int amount = game.getBattlefield().countAll(FILTER, targetPlayer.getId(), game) / 2; int amount = game.getBattlefield().countAll(StaticFilters.FILTER_CONTROLLED_PERMANENT, targetPlayer.getId(), game) / 2;
if (amount < 1) { if (amount < 1) {
return true; return true;
} }
Target target = new TargetControlledPermanent(amount, amount, FILTER, true); Target target = new TargetControlledPermanent(amount, amount, StaticFilters.FILTER_CONTROLLED_PERMANENT, true);
if (target.canChoose(targetPlayer.getId(), game)) { if (target.canChoose(targetPlayer.getId(), game)) {
while (!target.isChosen() && target.canChoose(targetPlayer.getId(), game) && targetPlayer.canRespond()) { while (!target.isChosen() && target.canChoose(targetPlayer.getId(), game) && targetPlayer.canRespond()) {
targetPlayer.choose(Outcome.Sacrifice, target, source.getSourceId(), game); targetPlayer.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
} }
for (int idx = 0; idx < target.getTargets().size(); idx++) { //sacrifice all chosen (non null) permanents
Permanent permanent = game.getPermanent(target.getTargets().get(idx)); target.getTargets().stream()
if (permanent != null) { .map(game::getPermanent)
permanent.sacrifice(source.getSourceId(), game); .filter(Objects::nonNull)
} .forEach(permanent -> permanent.sacrifice(source.getSourceId(), game));
}
} }
return true; return true;
} }

View file

@ -27,7 +27,6 @@
*/ */
package mage.cards.m; package mage.cards.m;
import java.util.UUID;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.common.SimpleActivatedAbility; import mage.abilities.common.SimpleActivatedAbility;
import mage.abilities.costs.common.SacrificeTargetCost; import mage.abilities.costs.common.SacrificeTargetCost;
@ -37,26 +36,19 @@ import mage.cards.CardImpl;
import mage.cards.CardSetInfo; import mage.cards.CardSetInfo;
import mage.constants.CardType; import mage.constants.CardType;
import mage.constants.Zone; import mage.constants.Zone;
import mage.filter.common.FilterControlledPermanent; import mage.filter.StaticFilters;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.target.common.TargetControlledPermanent; import mage.target.common.TargetControlledPermanent;
import mage.target.common.TargetCreatureOrPlayer; import mage.target.common.TargetCreatureOrPlayer;
import java.util.UUID;
/** /**
* *
* @author TheElk801 * @author TheElk801
*/ */
public class MakeshiftMunitions extends CardImpl { public class MakeshiftMunitions extends CardImpl {
private static final FilterControlledPermanent filter = new FilterControlledPermanent("artifact or creature");
static {
filter.add(Predicates.or(
new CardTypePredicate(CardType.ARTIFACT),
new CardTypePredicate(CardType.CREATURE)
));
}
public MakeshiftMunitions(UUID ownerId, CardSetInfo setInfo) { public MakeshiftMunitions(UUID ownerId, CardSetInfo setInfo) {
super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{1}{R}"); super(ownerId, setInfo, new CardType[]{CardType.ENCHANTMENT}, "{1}{R}");
@ -65,7 +57,7 @@ public class MakeshiftMunitions extends CardImpl {
Ability ability = new SimpleActivatedAbility( Ability ability = new SimpleActivatedAbility(
Zone.BATTLEFIELD, Zone.BATTLEFIELD,
new DamageTargetEffect(1), new DamageTargetEffect(1),
new SacrificeTargetCost(new TargetControlledPermanent(filter)) new SacrificeTargetCost(new TargetControlledPermanent(StaticFilters.FILTER_CONTROLLED_PERMANENT_ARTIFACT_OR_CREATURE))
); );
ability.addTarget(new TargetCreatureOrPlayer()); ability.addTarget(new TargetCreatureOrPlayer());
ability.addCost(new GenericManaCost(1)); ability.addCost(new GenericManaCost(1));

View file

@ -32,10 +32,8 @@ import mage.abilities.SpellAbility;
import mage.abilities.common.SimpleStaticAbility; import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.costs.AdjustingSourceCosts; import mage.abilities.costs.AdjustingSourceCosts;
import mage.abilities.effects.common.AffinityEffect; import mage.abilities.effects.common.AffinityEffect;
import mage.constants.CardType;
import mage.constants.Zone; import mage.constants.Zone;
import mage.filter.common.FilterControlledPermanent; import mage.filter.StaticFilters;
import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.game.Game; import mage.game.Game;
import mage.util.CardUtil; import mage.util.CardUtil;
@ -43,14 +41,9 @@ import mage.util.CardUtil;
* Affinity for artifacts * Affinity for artifacts
*/ */
public class AffinityForArtifactsAbility extends SimpleStaticAbility implements AdjustingSourceCosts { public class AffinityForArtifactsAbility extends SimpleStaticAbility implements AdjustingSourceCosts {
private static final FilterControlledPermanent filter = new FilterControlledPermanent();
static {
filter.add(new CardTypePredicate(CardType.ARTIFACT));
}
public AffinityForArtifactsAbility() { public AffinityForArtifactsAbility() {
super(Zone.OUTSIDE, new AffinityEffect(filter)); super(Zone.OUTSIDE, new AffinityEffect(StaticFilters.FILTER_CONTROLLED_PERMANENT_ARTIFACT));
setRuleAtTheTop(true); setRuleAtTheTop(true);
} }
@ -71,7 +64,7 @@ public class AffinityForArtifactsAbility extends SimpleStaticAbility implements
@Override @Override
public void adjustCosts(Ability ability, Game game) { public void adjustCosts(Ability ability, Game game) {
if (ability instanceof SpellAbility) { if (ability instanceof SpellAbility) {
int count = game.getBattlefield().getAllActivePermanents(filter, ability.getControllerId(), game).size(); int count = game.getBattlefield().getAllActivePermanents(StaticFilters.FILTER_CONTROLLED_PERMANENT_ARTIFACT, ability.getControllerId(), game).size();
if (count > 0) { if (count > 0) {
CardUtil.adjustCost((SpellAbility)ability, count); CardUtil.adjustCost((SpellAbility)ability, count);
} }

View file

@ -27,22 +27,23 @@
*/ */
package mage.abilities.keyword; package mage.abilities.keyword;
import java.util.UUID;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.TriggeredAbilityImpl; import mage.abilities.TriggeredAbilityImpl;
import mage.abilities.effects.OneShotEffect; import mage.abilities.effects.OneShotEffect;
import mage.constants.Outcome; import mage.constants.Outcome;
import mage.constants.Zone; import mage.constants.Zone;
import mage.filter.common.FilterControlledPermanent; import mage.filter.StaticFilters;
import mage.game.Game; import mage.game.Game;
import mage.game.events.GameEvent; import mage.game.events.GameEvent;
import mage.game.events.GameEvent.EventType; import mage.game.events.GameEvent.EventType;
import mage.game.permanent.Permanent;
import mage.players.Player; import mage.players.Player;
import mage.target.Target; import mage.target.Target;
import mage.target.common.TargetControlledPermanent; import mage.target.common.TargetControlledPermanent;
import mage.util.CardUtil; import mage.util.CardUtil;
import java.util.Objects;
import java.util.UUID;
/** /**
* 702.84. Annihilator 702.84a Annihilator is a triggered ability. "Annihilator * 702.84. Annihilator 702.84a Annihilator is a triggered ability. "Annihilator
* N" means "Whenever this creature attacks, defending player sacrifices N * N" means "Whenever this creature attacks, defending player sacrifices N
@ -103,7 +104,6 @@ public class AnnihilatorAbility extends TriggeredAbilityImpl {
class AnnihilatorEffect extends OneShotEffect { class AnnihilatorEffect extends OneShotEffect {
private final int count; private final int count;
private static final FilterControlledPermanent FILTER = new FilterControlledPermanent();
AnnihilatorEffect(int count) { AnnihilatorEffect(int count) {
super(Outcome.Sacrifice); super(Outcome.Sacrifice);
@ -123,21 +123,19 @@ class AnnihilatorEffect extends OneShotEffect {
player = game.getPlayer(defendingPlayerId); player = game.getPlayer(defendingPlayerId);
} }
if (player != null) { if (player != null) {
int amount = Math.min(count, game.getBattlefield().countAll(FILTER, player.getId(), game)); int amount = Math.min(count, game.getBattlefield().countAll(StaticFilters.FILTER_CONTROLLED_PERMANENT, player.getId(), game));
if (amount > 0) { if (amount > 0) {
Target target = new TargetControlledPermanent(amount, amount, FILTER, true); Target target = new TargetControlledPermanent(amount, amount, StaticFilters.FILTER_CONTROLLED_PERMANENT, true);
if (target.canChoose(player.getId(), game)) { if (target.canChoose(player.getId(), game)) {
while (player.canRespond() while (player.canRespond()
&& target.canChoose(player.getId(), game) && target.canChoose(player.getId(), game)
&& !target.isChosen()) { && !target.isChosen()) {
player.choose(Outcome.Sacrifice, target, source.getSourceId(), game); player.choose(Outcome.Sacrifice, target, source.getSourceId(), game);
} }
for (int idx = 0; idx < target.getTargets().size(); idx++) { target.getTargets().stream()
Permanent permanent = game.getPermanent(target.getTargets().get(idx)); .map(game::getPermanent)
if (permanent != null) { .filter(Objects::nonNull)
permanent.sacrifice(source.getSourceId(), game); .forEach(permanent -> permanent.sacrifice(source.getSourceId(), game));
}
}
} }
return true; return true;
} }

View file

@ -168,12 +168,8 @@ public final class Predicates {
@Override @Override
public boolean apply(T t, Game game) { public boolean apply(T t, Game game) {
for (Predicate<? super T> component : components) { return components.stream().allMatch(predicate -> predicate.apply(t, game));
if (!component.apply(t, game)) {
return false;
}
}
return true;
} }
@Override @Override
@ -196,12 +192,7 @@ public final class Predicates {
@Override @Override
public boolean apply(T t, Game game) { public boolean apply(T t, Game game) {
for (Predicate<? super T> component : components) { return components.stream().anyMatch(predicate -> predicate.apply(t, game));
if (component.apply(t, game)) {
return true;
}
}
return false;
} }
@Override @Override

View file

@ -32,7 +32,7 @@ import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.effects.RestrictionUntapNotMoreThanEffect; import mage.abilities.effects.RestrictionUntapNotMoreThanEffect;
import mage.constants.Duration; import mage.constants.Duration;
import mage.constants.Zone; import mage.constants.Zone;
import mage.filter.common.FilterControlledPermanent; import mage.filter.StaticFilters;
import mage.game.Game; import mage.game.Game;
import mage.game.command.Emblem; import mage.game.command.Emblem;
import mage.players.Player; import mage.players.Player;
@ -53,7 +53,7 @@ public class DovinBaanEmblem extends Emblem {
class DovinBaanCantUntapEffect extends RestrictionUntapNotMoreThanEffect { class DovinBaanCantUntapEffect extends RestrictionUntapNotMoreThanEffect {
DovinBaanCantUntapEffect() { DovinBaanCantUntapEffect() {
super(Duration.WhileOnBattlefield, 2, new FilterControlledPermanent()); super(Duration.WhileOnBattlefield, 2, StaticFilters.FILTER_CONTROLLED_PERMANENT);
staticText = "Your opponents can't untap more than two permanents during their untap steps."; staticText = "Your opponents can't untap more than two permanents during their untap steps.";
} }