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;
import java.util.UUID;
import mage.abilities.effects.common.CounterTargetEffect;
import mage.cards.CardImpl;
import mage.cards.CardSetInfo;
import mage.constants.CardType;
import mage.constants.SubType;
import mage.filter.FilterSpell;
import mage.filter.common.FilterControlledPermanent;
import mage.filter.StaticFilters;
import mage.filter.predicate.Predicates;
import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.mageobject.SubtypePredicate;
import mage.filter.predicate.other.TargetsPermanentPredicate;
import mage.target.TargetSpell;
import java.util.UUID;
/**
*
* @author LoneFox
@ -51,7 +52,7 @@ public class AvoidFate extends CardImpl {
static {
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) {

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -32,7 +32,7 @@ import mage.abilities.common.SimpleStaticAbility;
import mage.abilities.effects.RestrictionUntapNotMoreThanEffect;
import mage.constants.Duration;
import mage.constants.Zone;
import mage.filter.common.FilterControlledPermanent;
import mage.filter.StaticFilters;
import mage.game.Game;
import mage.game.command.Emblem;
import mage.players.Player;
@ -53,7 +53,7 @@ public class DovinBaanEmblem extends Emblem {
class DovinBaanCantUntapEffect extends RestrictionUntapNotMoreThanEffect {
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.";
}