Refactor PreventAllDamageToAllEffect

This commit is contained in:
Oleg Agafonov 2019-12-24 10:48:08 +04:00
parent 9d2dfc048d
commit 5cb040607e
11 changed files with 104 additions and 108 deletions

View file

@ -1,44 +1,46 @@
package mage.abilities.effects.common;
import java.util.UUID;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.effects.PreventionEffectImpl;
import mage.constants.Duration;
import mage.filter.FilterInPlay;
import mage.filter.common.FilterCreatureOrPlayer;
import mage.filter.common.FilterCreaturePermanent;
import mage.filter.FilterPermanent;
import mage.filter.FilterPlayer;
import mage.filter.common.FilterPermanentOrPlayer;
import mage.filter.predicate.other.PlayerIdPredicate;
import mage.filter.predicate.permanent.PermanentIdPredicate;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.players.Player;
import java.util.UUID;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class PreventAllDamageToAllEffect extends PreventionEffectImpl {
protected FilterInPlay filter;
public PreventAllDamageToAllEffect(Duration duration, FilterCreaturePermanent filter) {
this(duration, createFilter(filter));
protected FilterPermanentOrPlayer filter;
public PreventAllDamageToAllEffect(Duration duration, FilterPermanent filterPermanent) {
this(duration, createFilter(filterPermanent, null));
}
public PreventAllDamageToAllEffect(Duration duration, FilterInPlay filter) {
public PreventAllDamageToAllEffect(Duration duration, FilterPermanent filterPermanent, boolean onlyCombat) {
this(duration, createFilter(filterPermanent, null), onlyCombat);
}
public PreventAllDamageToAllEffect(Duration duration, FilterPermanentOrPlayer filter) {
this(duration, filter, false);
}
public PreventAllDamageToAllEffect(Duration duration, FilterInPlay filter, boolean onlyCombat) {
public PreventAllDamageToAllEffect(Duration duration, FilterPermanentOrPlayer filter, boolean onlyCombat) {
super(duration, Integer.MAX_VALUE, onlyCombat);
this.filter = filter;
staticText = "Prevent all "
+ (onlyCombat ? "combat ":"")
+ "damage that would be dealt to "
+ (onlyCombat ? "combat " : "")
+ "damage that would be dealt to "
+ filter.getMessage()
+ (duration.toString().isEmpty() ?"": ' ' + duration.toString());
+ (duration.toString().isEmpty() ? "" : ' ' + duration.toString());
}
public PreventAllDamageToAllEffect(final PreventAllDamageToAllEffect effect) {
@ -46,13 +48,25 @@ public class PreventAllDamageToAllEffect extends PreventionEffectImpl {
this.filter = effect.filter.copy();
}
private static FilterInPlay createFilter(FilterCreaturePermanent filter) {
FilterCreatureOrPlayer newfilter = new FilterCreatureOrPlayer(filter.getMessage());
newfilter.setCreatureFilter(filter);
newfilter.getPlayerFilter().add(new PlayerIdPredicate(UUID.randomUUID()));
return newfilter;
private static FilterPermanentOrPlayer createFilter(FilterPermanent filterPermanent, FilterPlayer filterPlayer) {
String message = String.join(
" and ",
filterPermanent != null ? filterPermanent.getMessage() : "",
filterPlayer != null ? filterPlayer.getMessage() : "");
FilterPermanent filter1 = filterPermanent;
if (filter1 == null) {
filter1 = new FilterPermanent();
filter1.add(new PermanentIdPredicate(UUID.randomUUID())); // disable filter
}
FilterPlayer filter2 = filterPlayer;
if (filter2 == null) {
filter2 = new FilterPlayer();
filter2.add(new PlayerIdPredicate(UUID.randomUUID())); // disable filter
}
return new FilterPermanentOrPlayer(message, filter1, filter2);
}
@Override
public PreventAllDamageToAllEffect copy() {
return new PreventAllDamageToAllEffect(this);
@ -66,17 +80,9 @@ public class PreventAllDamageToAllEffect extends PreventionEffectImpl {
@Override
public boolean applies(GameEvent event, Ability source, Game game) {
if (super.applies(event, source, game)) {
Permanent permanent = game.getPermanent(event.getTargetId());
if (permanent != null) {
if (filter.match(permanent, source.getSourceId(), source.getControllerId(), game)) {
return true;
}
}
else {
Player player = game.getPlayer(event.getTargetId());
if (player != null && filter.match(player, source.getSourceId(), source.getControllerId(), game)) {
return true;
}
MageObject object = game.getObject(event.getTargetId());
if (object != null) {
return filter.match(object, source.getSourceId(), source.getControllerId(), game);
}
}
return false;

View file

@ -15,6 +15,7 @@ import mage.filter.predicate.mageobject.CardTypePredicate;
import mage.filter.predicate.mageobject.MulticoloredPredicate;
import mage.filter.predicate.mageobject.SubtypePredicate;
import mage.filter.predicate.mageobject.SupertypePredicate;
import mage.filter.predicate.other.PlayerPredicate;
import mage.filter.predicate.permanent.AnotherPredicate;
import mage.filter.predicate.permanent.AttackingPredicate;
import mage.filter.predicate.permanent.ControllerPredicate;
@ -607,4 +608,10 @@ public final class StaticFilters {
FILTER_CARD_ARTIFACT_OR_CREATURE.setLockedFilter(true);
}
public static final FilterPlayer FILTER_PLAYER_CONTROLLER = new FilterPlayer("you");
static {
FILTER_PLAYER_CONTROLLER.add(new PlayerPredicate(TargetController.YOU));
FILTER_PLAYER_CONTROLLER.setLockedFilter(true);
}
}