From 166c6a359084d32379addd34fdf63beba3131b49 Mon Sep 17 00:00:00 2001 From: LevelX2 Date: Mon, 6 Feb 2017 22:55:52 +0100 Subject: [PATCH] * Sunforger - Fixed activated unattach ability. --- Mage.Sets/src/mage/cards/s/Sunforger.java | 111 ++++++++++++++---- .../abilities/costs/common/UnattachCost.java | 4 +- 2 files changed, 93 insertions(+), 22 deletions(-) diff --git a/Mage.Sets/src/mage/cards/s/Sunforger.java b/Mage.Sets/src/mage/cards/s/Sunforger.java index f6212a20487..1c79ff1fb2e 100644 --- a/Mage.Sets/src/mage/cards/s/Sunforger.java +++ b/Mage.Sets/src/mage/cards/s/Sunforger.java @@ -30,8 +30,11 @@ package mage.cards.s; import java.util.UUID; import mage.ObjectColor; import mage.abilities.Ability; +import mage.abilities.SpellAbility; import mage.abilities.common.SimpleActivatedAbility; import mage.abilities.common.SimpleStaticAbility; +import mage.abilities.costs.Cost; +import mage.abilities.costs.CostImpl; import mage.abilities.costs.mana.GenericManaCost; import mage.abilities.costs.mana.ManaCostsImpl; import mage.abilities.effects.OneShotEffect; @@ -46,14 +49,15 @@ import mage.constants.Outcome; import mage.constants.Zone; import mage.filter.Filter.ComparisonType; import mage.filter.FilterCard; +import mage.filter.predicate.Predicate; import mage.filter.predicate.Predicates; import mage.filter.predicate.mageobject.CardTypePredicate; import mage.filter.predicate.mageobject.ColorPredicate; import mage.filter.predicate.mageobject.ConvertedManaCostPredicate; import mage.game.Game; +import mage.game.permanent.Permanent; import mage.players.Player; import mage.target.common.TargetCardInLibrary; -import mage.abilities.costs.common.UnattachCost; /** * @@ -62,7 +66,7 @@ import mage.abilities.costs.common.UnattachCost; public class Sunforger extends CardImpl { public Sunforger(UUID ownerId, CardSetInfo setInfo) { - super(ownerId,setInfo,new CardType[]{CardType.ARTIFACT},"{3}"); + super(ownerId, setInfo, new CardType[]{CardType.ARTIFACT}, "{3}"); this.subtype.add("Equipment"); // Equipped creature gets +4/+0. @@ -70,7 +74,7 @@ public class Sunforger extends CardImpl { // {R}{W}, Unattach Sunforger: Search your library for a red or white instant card with converted mana cost 4 or less and cast that card without paying its mana cost. Then shuffle your library. Ability ability = new SimpleActivatedAbility(Zone.BATTLEFIELD, new SunforgerEffect(), new ManaCostsImpl("{R}{W}")); - ability.addCost(new UnattachCost(this.getName(), this.getId())); + ability.addCost(new SunforgerUnattachCost(this.getName())); this.addAbility(ability); // Equip {3} @@ -90,16 +94,6 @@ public class Sunforger extends CardImpl { class SunforgerEffect extends OneShotEffect { - private static final FilterCard filter = new FilterCard("red or white instant card with converted mana cost 4 or less"); - - static { - filter.add(Predicates.or( - new ColorPredicate(ObjectColor.RED), - new ColorPredicate(ObjectColor.WHITE))); - filter.add(new CardTypePredicate(CardType.INSTANT)); - filter.add(new ConvertedManaCostPredicate(ComparisonType.LessThan, 5)); - } - public SunforgerEffect() { super(Outcome.PlayForFree); staticText = "Search your library for a red or white instant card with converted mana cost 4 or less and cast that card without paying its mana cost. Then shuffle your library"; @@ -116,21 +110,98 @@ class SunforgerEffect extends OneShotEffect { @Override public boolean apply(Game game, Ability source) { - Player you = game.getPlayer(source.getControllerId()); - if (you != null) { - if (you.getLibrary().size() > 0) { + Player controller = game.getPlayer(source.getControllerId()); + if (controller != null) { + if (controller.getLibrary().size() > 0) { + /** + * 10/1/2005 Any card you find must be legally castable (for + * example, you have to be able to choose a legal target for + * it). If you can’t find a castable card (or choose not to), + * nothing happens and you shuffle your library. + */ + + FilterCard filter = new FilterCard("red or white instant card with converted mana cost 4 or less"); TargetCardInLibrary target = new TargetCardInLibrary(filter); - if (you.searchLibrary(target, game, you.getId())) { + filter.add(Predicates.or( + new ColorPredicate(ObjectColor.RED), + new ColorPredicate(ObjectColor.WHITE))); + filter.add(new CardTypePredicate(CardType.INSTANT)); + filter.add(new ConvertedManaCostPredicate(ComparisonType.LessThan, 5)); + filter.add(new CardCanBeCastPredicate(source.getControllerId())); + if (controller.searchLibrary(target, game, controller.getId())) { UUID targetId = target.getFirstTarget(); - Card card = you.getLibrary().remove(targetId, game); + Card card = game.getCard(targetId); if (card != null) { - you.cast(card.getSpellAbility(), game, true); + controller.cast(card.getSpellAbility(), game, true); } } } - you.shuffleLibrary(source, game); + controller.shuffleLibrary(source, game); return true; } return false; } } + +class SunforgerUnattachCost extends CostImpl { + + public SunforgerUnattachCost(String name) { + this.text = "Unattach " + name; + } + + public SunforgerUnattachCost(final SunforgerUnattachCost cost) { + super(cost); + } + + @Override + public boolean pay(Ability ability, Game game, UUID sourceId, UUID controllerId, boolean noMana, Cost costToPay) { + Permanent attachment = game.getPermanent(sourceId); + if (attachment != null & attachment.getAttachedTo() != null) { + Permanent attachedTo = game.getPermanent(attachment.getAttachedTo()); + if (attachedTo != null) { + paid = attachedTo.removeAttachment(attachment.getId(), game); + } + } + return paid; + } + + @Override + public boolean canPay(Ability ability, UUID sourceId, UUID controllerId, Game game) { + Permanent attachment = game.getPermanent(sourceId); + if (attachment != null && attachment.getAttachedTo() != null) { + Permanent attachedTo = game.getPermanent(attachment.getAttachedTo()); + if (attachedTo != null) { + return true; + } + + } + return false; + } + + @Override + public SunforgerUnattachCost copy() { + return new SunforgerUnattachCost(this); + } +} + +class CardCanBeCastPredicate implements Predicate { + + private final UUID controllerId; + + public CardCanBeCastPredicate(UUID controllerId) { + this.controllerId = controllerId; + } + + @Override + public boolean apply(Card input, Game game) { + SpellAbility ability = input.getSpellAbility().copy(); + ability.setControllerId(controllerId); + input.adjustTargets(ability, game); + return ability.canChooseTarget(game); + } + + @Override + public String toString() { + return "CardCanBeCastPredicate"; + } +} diff --git a/Mage/src/main/java/mage/abilities/costs/common/UnattachCost.java b/Mage/src/main/java/mage/abilities/costs/common/UnattachCost.java index 5400d470614..9cdc11a23a6 100644 --- a/Mage/src/main/java/mage/abilities/costs/common/UnattachCost.java +++ b/Mage/src/main/java/mage/abilities/costs/common/UnattachCost.java @@ -1,11 +1,11 @@ package mage.abilities.costs.common; +import java.util.UUID; import mage.abilities.Ability; import mage.abilities.costs.Cost; +import mage.abilities.costs.CostImpl; import mage.game.Game; import mage.game.permanent.Permanent; -import java.util.UUID; -import mage.abilities.costs.CostImpl; /** * @author Galatolol