From 0a96201b6a7890b61dea07f3f843f810299f59f5 Mon Sep 17 00:00:00 2001 From: Alex Vasile <48962821+Alex-Vasile@users.noreply.github.com> Date: Mon, 22 Aug 2022 21:39:28 -0400 Subject: [PATCH] Simplified Mirri and Crawlspace by making their identical effects a common effect that is shared between them --- Mage.Sets/src/mage/cards/c/Crawlspace.java | 50 +------------- .../cards/m/MirriWeatherlightDuelist.java | 67 +++++-------------- ...ngeMaxNumberThatCanAttackSourceEffect.java | 61 +++++++++++++++++ 3 files changed, 78 insertions(+), 100 deletions(-) create mode 100644 Mage/src/main/java/mage/abilities/effects/common/continuous/ChangeMaxNumberThatCanAttackSourceEffect.java diff --git a/Mage.Sets/src/mage/cards/c/Crawlspace.java b/Mage.Sets/src/mage/cards/c/Crawlspace.java index e074600fe6e..18956035817 100644 --- a/Mage.Sets/src/mage/cards/c/Crawlspace.java +++ b/Mage.Sets/src/mage/cards/c/Crawlspace.java @@ -5,6 +5,7 @@ import java.util.UUID; import mage.abilities.Ability; import mage.abilities.common.SimpleStaticAbility; import mage.abilities.effects.ContinuousEffectImpl; +import mage.abilities.effects.common.continuous.ChangeMaxNumberThatCanAttackSourceEffect; import mage.cards.CardImpl; import mage.cards.CardSetInfo; import mage.constants.*; @@ -21,7 +22,7 @@ public final class Crawlspace extends CardImpl { super(ownerId,setInfo,new CardType[]{CardType.ARTIFACT},"{3}"); // No more than two creatures can attack you each combat. - this.addAbility(new SimpleStaticAbility(Zone.BATTLEFIELD, new ChangeMaxAttackedBySourceEffect(2))); + this.addAbility(new SimpleStaticAbility(Zone.BATTLEFIELD, new ChangeMaxNumberThatCanAttackSourceEffect(2))); } @@ -34,50 +35,3 @@ public final class Crawlspace extends CardImpl { return new Crawlspace(this); } } - -class ChangeMaxAttackedBySourceEffect extends ContinuousEffectImpl { - - private final int maxAttackedBy; - - public ChangeMaxAttackedBySourceEffect(int maxAttackedBy) { - super(Duration.WhileOnBattlefield, Outcome.Benefit); - this.maxAttackedBy = maxAttackedBy; - staticText = "No more than two creatures can attack you each combat"; - } - - public ChangeMaxAttackedBySourceEffect(final ChangeMaxAttackedBySourceEffect effect) { - super(effect); - this.maxAttackedBy = effect.maxAttackedBy; - } - - @Override - public ChangeMaxAttackedBySourceEffect copy() { - return new ChangeMaxAttackedBySourceEffect(this); - } - - @Override - public boolean apply(Layer layer, SubLayer sublayer, Ability source, Game game) { - switch (layer) { - case RulesEffects: - Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - // Change the rule - if (controller.getMaxAttackedBy()> maxAttackedBy) { - controller.setMaxAttackedBy(maxAttackedBy); - } - } - break; - } - return false; - } - - @Override - public boolean apply(Game game, Ability source) { - return false; - } - - @Override - public boolean hasLayer(Layer layer) { - return layer == Layer.RulesEffects; - } -} diff --git a/Mage.Sets/src/mage/cards/m/MirriWeatherlightDuelist.java b/Mage.Sets/src/mage/cards/m/MirriWeatherlightDuelist.java index 9eb2985b4ec..d04e724dd93 100644 --- a/Mage.Sets/src/mage/cards/m/MirriWeatherlightDuelist.java +++ b/Mage.Sets/src/mage/cards/m/MirriWeatherlightDuelist.java @@ -9,6 +9,7 @@ import mage.abilities.decorator.ConditionalContinuousEffect; import mage.abilities.effects.ContinuousEffectImpl; import mage.abilities.effects.RestrictionEffect; import mage.abilities.effects.common.AddContinuousEffectToGame; +import mage.abilities.effects.common.continuous.ChangeMaxNumberThatCanAttackSourceEffect; import mage.abilities.keyword.FirstStrikeAbility; import mage.cards.CardImpl; import mage.cards.CardSetInfo; @@ -41,7 +42,7 @@ public final class MirriWeatherlightDuelist extends CardImpl { // As long as Mirri, Weatherlight Duelist is tapped, no more than one creature can attack you each combat. Ability ability = new SimpleStaticAbility(Zone.BATTLEFIELD, new ConditionalContinuousEffect( - new MirriWeatherlightDuelistAttackRestrictionEffect(1), SourceTappedCondition.TAPPED, + new ChangeMaxNumberThatCanAttackSourceEffect(1), SourceTappedCondition.TAPPED, "As long as {this} is tapped, no more than one creature can attack you each combat.")); this.addAbility(ability); } @@ -82,59 +83,21 @@ class MirriWeatherlightDuelistBlockRestrictionEffect extends RestrictionEffect { if (attacker == null) { return true; } - for (UUID creatureId : game.getCombat().getBlockers()) { - Permanent existingBlocker = game.getPermanent(creatureId); - if (game.getPlayer(existingBlocker.getControllerId()).hasOpponent(attacker.getControllerId(), game) && existingBlocker.isControlledBy(newBlocker.getControllerId())) { + Player controller = game.getPlayer(attacker.getControllerId()); + if (controller == null) { + return true; // Supposed to return false since without the controller this effect should not restrict blockers + } + + for (UUID existingBlockerId : game.getCombat().getBlockers()) { + Permanent existingBlocker = game.getPermanent(existingBlockerId); + if (existingBlocker == null) { + continue; + } + if (controller.hasOpponent(existingBlocker.getControllerId(), game) + && existingBlocker.isControlledBy(newBlocker.getControllerId())) { return false; } } return true; } -} - -class MirriWeatherlightDuelistAttackRestrictionEffect extends ContinuousEffectImpl { - - private final int maxAttackedBy; - - public MirriWeatherlightDuelistAttackRestrictionEffect(int maxAttackedBy) { - super(Duration.WhileOnBattlefield, Outcome.Benefit); - this.maxAttackedBy = maxAttackedBy; - staticText = "No more than one creature can attack you each combat"; - } - - public MirriWeatherlightDuelistAttackRestrictionEffect(final MirriWeatherlightDuelistAttackRestrictionEffect effect) { - super(effect); - this.maxAttackedBy = effect.maxAttackedBy; - } - - @Override - public MirriWeatherlightDuelistAttackRestrictionEffect copy() { - return new MirriWeatherlightDuelistAttackRestrictionEffect(this); - } - - @Override - public boolean apply(Layer layer, SubLayer sublayer, Ability source, Game game) { - switch (layer) { - case RulesEffects: - Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - // Change the rule - if (controller.getMaxAttackedBy() > maxAttackedBy) { - controller.setMaxAttackedBy(maxAttackedBy); - } - } - break; - } - return false; - } - - @Override - public boolean apply(Game game, Ability source) { - return false; - } - - @Override - public boolean hasLayer(Layer layer) { - return layer == Layer.RulesEffects; - } -} +} \ No newline at end of file diff --git a/Mage/src/main/java/mage/abilities/effects/common/continuous/ChangeMaxNumberThatCanAttackSourceEffect.java b/Mage/src/main/java/mage/abilities/effects/common/continuous/ChangeMaxNumberThatCanAttackSourceEffect.java new file mode 100644 index 00000000000..2de1995fd57 --- /dev/null +++ b/Mage/src/main/java/mage/abilities/effects/common/continuous/ChangeMaxNumberThatCanAttackSourceEffect.java @@ -0,0 +1,61 @@ +package mage.abilities.effects.common.continuous; + +import mage.abilities.Ability; +import mage.abilities.effects.ContinuousEffectImpl; +import mage.constants.Duration; +import mage.constants.Layer; +import mage.constants.Outcome; +import mage.constants.SubLayer; +import mage.game.Game; +import mage.players.Player; + +/** + * @author LevelX2 + */ +public class ChangeMaxNumberThatCanAttackSourceEffect extends ContinuousEffectImpl { + + private final int maxAttackedBy; + + public ChangeMaxNumberThatCanAttackSourceEffect(int maxAttackedBy) { + super(Duration.WhileOnBattlefield, Outcome.Benefit); + this.maxAttackedBy = maxAttackedBy; + staticText = "No more than " + (maxAttackedBy == 1 ? "one" : "two") + " creatures can attack you each combat"; + } + + public ChangeMaxNumberThatCanAttackSourceEffect(final ChangeMaxNumberThatCanAttackSourceEffect effect) { + super(effect); + this.maxAttackedBy = effect.maxAttackedBy; + } + + @Override + public ChangeMaxNumberThatCanAttackSourceEffect copy() { + return new ChangeMaxNumberThatCanAttackSourceEffect(this); + } + + @Override + public boolean apply(Layer layer, SubLayer sublayer, Ability source, Game game) { + if (layer != Layer.RulesEffects) { + return false; + } + + Player controller = game.getPlayer(source.getControllerId()); + if (controller == null) { + return false; + } + // Change the rule + if (controller.getMaxAttackedBy()> maxAttackedBy) { + controller.setMaxAttackedBy(maxAttackedBy); + } + return true; + } + + @Override + public boolean apply(Game game, Ability source) { + return false; + } + + @Override + public boolean hasLayer(Layer layer) { + return layer == Layer.RulesEffects; + } +}