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

@ -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.";
}