From 630b2c32d7481f8a938903a4ac70500effdb20e6 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Fri, 24 Jul 2015 15:04:45 +0200 Subject: [PATCH] * Reworked Cho-Manno's Blessing protection handling. --- .../mercadianmasques/ChoMannosBlessing.java | 61 ++---------- .../ProtectionChosenColorAttachedEffect.java | 98 +++++++++++++++++++ .../abilities/keyword/ProtectionAbility.java | 69 +++++++------ .../mage/game/permanent/PermanentImpl.java | 5 +- 4 files changed, 153 insertions(+), 80 deletions(-) create mode 100644 Mage/src/mage/abilities/effects/keyword/ProtectionChosenColorAttachedEffect.java diff --git a/Mage.Sets/src/mage/sets/mercadianmasques/ChoMannosBlessing.java b/Mage.Sets/src/mage/sets/mercadianmasques/ChoMannosBlessing.java index 446967ff67..03962c5f98 100644 --- a/Mage.Sets/src/mage/sets/mercadianmasques/ChoMannosBlessing.java +++ b/Mage.Sets/src/mage/sets/mercadianmasques/ChoMannosBlessing.java @@ -28,26 +28,18 @@ package mage.sets.mercadianmasques; import java.util.UUID; -import mage.abilities.Ability; import mage.abilities.common.AsEntersBattlefieldAbility; -import mage.abilities.effects.ContinuousEffect; -import mage.abilities.effects.OneShotEffect; +import mage.abilities.common.SimpleStaticAbility; import mage.abilities.effects.common.AttachEffect; -import mage.abilities.effects.common.continuous.GainAbilityAttachedEffect; +import mage.abilities.effects.common.ChooseColorEffect; +import mage.abilities.effects.keyword.ProtectionChosenColorAttachedEffect; import mage.abilities.keyword.EnchantAbility; import mage.abilities.keyword.FlashAbility; -import mage.abilities.keyword.ProtectionAbility; import mage.cards.CardImpl; -import mage.choices.ChoiceColor; -import mage.constants.AttachmentType; import mage.constants.CardType; -import mage.constants.Duration; import mage.constants.Outcome; import mage.constants.Rarity; -import mage.filter.FilterCard; -import mage.filter.predicate.mageobject.ColorPredicate; -import mage.game.Game; -import mage.players.Player; +import mage.constants.Zone; import mage.target.TargetPermanent; import mage.target.common.TargetCreaturePermanent; @@ -64,17 +56,19 @@ public class ChoMannosBlessing extends CardImpl { // Flash this.addAbility(FlashAbility.getInstance()); - + // Enchant creature TargetPermanent auraTarget = new TargetCreaturePermanent(); this.getSpellAbility().addTarget(auraTarget); this.getSpellAbility().addEffect(new AttachEffect(Outcome.Protect)); this.addAbility(new EnchantAbility(auraTarget.getTargetName())); - + // As Cho-Manno's Blessing enters the battlefield, choose a color. + this.addAbility(new AsEntersBattlefieldAbility(new ChooseColorEffect(Outcome.Benefit))); + // Enchanted creature has protection from the chosen color. This effect doesn't remove Cho-Manno's Blessing. - this.addAbility(new AsEntersBattlefieldAbility(new ChoMannosBlessingEffect())); - } + this.addAbility(new SimpleStaticAbility(Zone.BATTLEFIELD, new ProtectionChosenColorAttachedEffect(true))); + } public ChoMannosBlessing(final ChoMannosBlessing card) { super(card); @@ -85,38 +79,3 @@ public class ChoMannosBlessing extends CardImpl { return new ChoMannosBlessing(this); } } - -class ChoMannosBlessingEffect extends OneShotEffect { - - public ChoMannosBlessingEffect() { - super(Outcome.Protect); - this.staticText = "enchanted creature has protection from the chosen color. This effect doesn't remove {this}"; - } - - public ChoMannosBlessingEffect(final ChoMannosBlessingEffect effect) { - super(effect); - } - - @Override - public ChoMannosBlessingEffect copy() { - return new ChoMannosBlessingEffect(this); - } - - @Override - public boolean apply(Game game, Ability source) { - ChoiceColor choice = new ChoiceColor(); - choice.setMessage("Choose color to get protection from"); - Player controller = game.getPlayer(source.getControllerId()); - if (controller != null && controller.choose(outcome, choice, game)) { - FilterCard protectionFilter = new FilterCard(); - protectionFilter.add(new ColorPredicate(choice.getColor())); - protectionFilter.setMessage(choice.getChoice().toLowerCase()); - ProtectionAbility protectionAbility = new ProtectionAbility(protectionFilter); - protectionAbility.setRemovesAuras(false); - ContinuousEffect effect = new GainAbilityAttachedEffect(protectionAbility, AttachmentType.AURA, Duration.WhileOnBattlefield); - game.addEffect(effect, source); - return true; - } - return false; - } -} diff --git a/Mage/src/mage/abilities/effects/keyword/ProtectionChosenColorAttachedEffect.java b/Mage/src/mage/abilities/effects/keyword/ProtectionChosenColorAttachedEffect.java new file mode 100644 index 0000000000..aa514a45dd --- /dev/null +++ b/Mage/src/mage/abilities/effects/keyword/ProtectionChosenColorAttachedEffect.java @@ -0,0 +1,98 @@ +/* + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ +package mage.abilities.effects.keyword; + +import mage.ObjectColor; +import mage.abilities.Ability; +import mage.abilities.effects.ContinuousEffectImpl; +import mage.abilities.keyword.ProtectionAbility; +import mage.constants.Duration; +import mage.constants.Layer; +import mage.constants.Outcome; +import mage.constants.SubLayer; +import mage.filter.FilterObject; +import mage.filter.predicate.mageobject.ColorPredicate; +import mage.game.Game; +import mage.game.permanent.Permanent; + +/** + * + * @author LevelX2 + */ +public class ProtectionChosenColorAttachedEffect extends ContinuousEffectImpl { + + protected ObjectColor chosenColor; + protected ProtectionAbility protectionAbility; + protected boolean notRemoveItself; + + public ProtectionChosenColorAttachedEffect(boolean notRemoveItself) { + super(Duration.WhileOnBattlefield, Layer.AbilityAddingRemovingEffects_6, SubLayer.NA, Outcome.AddAbility); + this.notRemoveItself = notRemoveItself; + staticText = "{source} has protection from the chosen color" + (notRemoveItself ? ". This effect doesn't remove {this}" : ""); + } + + public ProtectionChosenColorAttachedEffect(final ProtectionChosenColorAttachedEffect effect) { + super(effect); + if (effect.chosenColor != null) { + this.chosenColor = effect.chosenColor.copy(); + } + if (effect.protectionAbility != null) { + this.protectionAbility = effect.protectionAbility.copy(); + } + } + + @Override + public ProtectionChosenColorAttachedEffect copy() { + return new ProtectionChosenColorAttachedEffect(this); + } + + @Override + public boolean apply(Game game, Ability source) { + Permanent attachement = game.getPermanent(source.getSourceId()); + if (attachement != null && attachement.getAttachedTo() != null) { + ObjectColor color = (ObjectColor) game.getState().getValue(attachement.getId() + "_color"); + if (color != null && (protectionAbility == null || !color.equals(chosenColor))) { + chosenColor = color; + FilterObject protectionFilter = new FilterObject(chosenColor.getDescription()); + protectionFilter.add(new ColorPredicate(chosenColor)); + protectionAbility = new ProtectionAbility(protectionFilter); + if (notRemoveItself) { + protectionAbility.setAuraIdNotToBeRemoved(source.getSourceId()); + } + } + if (protectionAbility != null) { + Permanent attachedTo = game.getPermanent(attachement.getAttachedTo()); + if (attachedTo != null) { + attachement.addAbility(protectionAbility, source.getSourceId(), game); + } + return true; + } + } + return false; + } +} diff --git a/Mage/src/mage/abilities/keyword/ProtectionAbility.java b/Mage/src/mage/abilities/keyword/ProtectionAbility.java index 1cf4a57b8c..3cacac4981 100644 --- a/Mage/src/mage/abilities/keyword/ProtectionAbility.java +++ b/Mage/src/mage/abilities/keyword/ProtectionAbility.java @@ -1,33 +1,33 @@ /* -* Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. -* -* Redistribution and use in source and binary forms, with or without modification, are -* permitted provided that the following conditions are met: -* -* 1. Redistributions of source code must retain the above copyright notice, this list of -* conditions and the following disclaimer. -* -* 2. Redistributions in binary form must reproduce the above copyright notice, this list -* of conditions and the following disclaimer in the documentation and/or other materials -* provided with the distribution. -* -* THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED -* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND -* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR -* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON -* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF -* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -* -* The views and conclusions contained in the software and documentation are those of the -* authors and should not be interpreted as representing official policies, either expressed -* or implied, of BetaSteward_at_googlemail.com. -*/ - + * Copyright 2010 BetaSteward_at_googlemail.com. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY BetaSteward_at_googlemail.com ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BetaSteward_at_googlemail.com OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of BetaSteward_at_googlemail.com. + */ package mage.abilities.keyword; +import java.util.UUID; import mage.MageObject; import mage.abilities.StaticAbility; import mage.cards.Card; @@ -49,17 +49,20 @@ public class ProtectionAbility extends StaticAbility { protected Filter filter; protected boolean removeAuras; + protected UUID auraIdNotToBeRemoved; // defines an Aura objectId that will not be removed from this protection ability public ProtectionAbility(Filter filter) { super(Zone.BATTLEFIELD, null); this.filter = filter; this.removeAuras = true; + this.auraIdNotToBeRemoved = null; } public ProtectionAbility(final ProtectionAbility ability) { super(ability); this.filter = ability.filter.copy(); this.removeAuras = ability.removeAuras; + this.auraIdNotToBeRemoved = ability.auraIdNotToBeRemoved; } @Override @@ -69,7 +72,8 @@ public class ProtectionAbility extends StaticAbility { @Override public String getRule() { - return "Protection from " + filter.getMessage() + (removeAuras ? "":". This effect doesn't remove auras."); + + return "Protection from " + filter.getMessage() + (removeAuras ? "" : ". This effect doesn't remove auras."); } public boolean canTarget(MageObject source, Game game) { @@ -112,4 +116,13 @@ public class ProtectionAbility extends StaticAbility { public boolean removesAuras() { return removeAuras; } + + public UUID getAuraIdNotToBeRemoved() { + return auraIdNotToBeRemoved; + } + + public void setAuraIdNotToBeRemoved(UUID auraIdNotToBeRemoved) { + this.auraIdNotToBeRemoved = auraIdNotToBeRemoved; + } + } diff --git a/Mage/src/mage/game/permanent/PermanentImpl.java b/Mage/src/mage/game/permanent/PermanentImpl.java index eb93cb1d56..3d7941c00b 100644 --- a/Mage/src/mage/game/permanent/PermanentImpl.java +++ b/Mage/src/mage/game/permanent/PermanentImpl.java @@ -950,7 +950,10 @@ public abstract class PermanentImpl extends CardImpl implements Permanent { @Override public boolean cantBeEnchantedBy(MageObject source, Game game) { for (ProtectionAbility ability : abilities.getProtectionAbilities()) { - if (!(source.getSubtype().contains("Aura") && !ability.removesAuras()) && !ability.canTarget(source, game)) { + if (!(source.getSubtype().contains("Aura") + && !ability.removesAuras()) + && !source.getId().equals(ability.getAuraIdNotToBeRemoved()) + && !ability.canTarget(source, game)) { return true; } }