diff --git a/Mage.Sets/src/mage/cards/g/GoblinArtisans.java b/Mage.Sets/src/mage/cards/g/GoblinArtisans.java index aa2a7ae2b8..d675fafe7c 100644 --- a/Mage.Sets/src/mage/cards/g/GoblinArtisans.java +++ b/Mage.Sets/src/mage/cards/g/GoblinArtisans.java @@ -1,50 +1,49 @@ - package mage.cards.g; -import java.util.List; -import java.util.UUID; import mage.MageInt; +import mage.MageObjectReference; import mage.abilities.Ability; -import mage.abilities.Mode; import mage.abilities.common.SimpleActivatedAbility; import mage.abilities.costs.common.TapSourceCost; -import mage.abilities.effects.OneShotEffect; +import mage.abilities.effects.common.CounterTargetEffect; +import mage.abilities.effects.common.DrawCardSourceControllerEffect; +import mage.abilities.effects.common.FlipCoinEffect; import mage.cards.CardImpl; import mage.cards.CardSetInfo; -import mage.cards.Cards; -import mage.cards.CardsImpl; import mage.constants.CardType; import mage.constants.SubType; -import mage.constants.Outcome; -import mage.constants.Zone; -import mage.filter.FilterCard; -import mage.filter.FilterPermanent; -import mage.filter.common.FilterControlledArtifactPermanent; -import mage.filter.predicate.Predicates; -import mage.filter.predicate.mageobject.NamePredicate; -import mage.filter.predicate.permanent.PermanentIdPredicate; +import mage.constants.TargetController; +import mage.filter.FilterSpell; +import mage.filter.common.FilterArtifactSpell; import mage.game.Game; import mage.game.permanent.Permanent; -import mage.players.Player; +import mage.game.stack.Spell; +import mage.game.stack.StackAbility; +import mage.game.stack.StackObject; import mage.target.Target; -import mage.target.TargetCard; +import mage.target.TargetSpell; + +import java.util.Collection; +import java.util.UUID; /** - * - * @author MarcoMarin + * @author TheElk801 */ public final class GoblinArtisans extends CardImpl { public GoblinArtisans(UUID ownerId, CardSetInfo setInfo) { - super(ownerId,setInfo,new CardType[]{CardType.CREATURE},"{R}"); + super(ownerId, setInfo, new CardType[]{CardType.CREATURE}, "{R}"); this.subtype.add(SubType.GOBLIN); this.subtype.add(SubType.ARTIFICER); this.power = new MageInt(1); this.toughness = new MageInt(1); // {tap}: Flip a coin. If you win the flip, draw a card. If you lose the flip, counter target artifact spell you control that isn't the target of an ability from another creature named Goblin Artisans. - this.addAbility(new SimpleActivatedAbility(Zone.BATTLEFIELD, new GoblinArtisansEffect(), new TapSourceCost())); - + Ability ability = new SimpleActivatedAbility(new FlipCoinEffect( + new DrawCardSourceControllerEffect(1), new CounterTargetEffect() + ), new TapSourceCost()); + ability.addTarget(new GoblinArtisansTarget()); + this.addAbility(ability); } private GoblinArtisans(final GoblinArtisans card) { @@ -57,68 +56,59 @@ public final class GoblinArtisans extends CardImpl { } } -class GoblinArtisansEffect extends OneShotEffect { +class GoblinArtisansTarget extends TargetSpell { - private static final FilterPermanent filter = new FilterPermanent("permanent named Goblin Artisans"); + private static final FilterSpell filter = new FilterArtifactSpell( + "artifact spell you control that isn't the target " + + "of an ability from another creature named Goblin Artisans" + ); static { - filter.add(new NamePredicate("Goblin Artisans")); + filter.add(TargetController.YOU.getOwnerPredicate()); } - public GoblinArtisansEffect() { - super(Outcome.Damage); - staticText = "Flip a coin. If you win the flip, draw a card. If you lose the flip, counter target artifact spell you control that isn't the target of an ability from another creature named Goblin Artisans."; + GoblinArtisansTarget() { + super(filter); } - public GoblinArtisansEffect(GoblinArtisansEffect effect) { - super(effect); + private GoblinArtisansTarget(final GoblinArtisansTarget target) { + super(target); } @Override - public boolean apply(Game game, Ability source) { - Player controller = game.getPlayer(source.getControllerId()); - if (controller != null) { - if (controller.flipCoin(source, game, true)) { - controller.drawCards(1, source, game); - } else { - List artifacts = game.getBattlefield().getActivePermanents(new FilterControlledArtifactPermanent(), source.getControllerId(), game); - if (artifacts.isEmpty()) {//Don't even bother if there is no artifact to 'counter'/sacrifice - return true; - } + public GoblinArtisansTarget copy() { + return new GoblinArtisansTarget(this); + } - filter.add(Predicates.not(new PermanentIdPredicate(source.getSourceId()))); - //removed the activating instance of Artisans, btw, wasn't that filter declared as static final? How come I can do this here? :) - List list = game.getBattlefield().getAllActivePermanents(filter, game); - for (Permanent perm : list) { // should I limit below for a particular kind of ability? Going for the most general, it's unlikely there'll be any other artisans anyway, so not concerned about efficiency :p - for (Ability abil : perm.getAbilities(game)) {//below is copied from TargetsPermanentPredicate, but why only "selectedModes"? Shouldnt be more general as well? - for (UUID modeId : abil.getModes().getSelectedModes()) { - Mode mode = abil.getModes().get(modeId); - for (Target target : mode.getTargets()) { - for (UUID targetId : target.getTargets()) { - artifacts.remove(game.getPermanentOrLKIBattlefield(targetId)); - }// we could - }// remove this - }//closing bracers - }// pyramid, if it's bothering anyone - } //they are all one-liners after all :) - if (!artifacts.isEmpty()) { - Cards cards = new CardsImpl(); - for (Permanent perm : artifacts) { - cards.add(perm.getId()); - } - TargetCard target = new TargetCard(Zone.BATTLEFIELD, new FilterCard()); - controller.choose(Outcome.Sacrifice, cards, target, game); - game.getPermanent(target.getFirstTarget()).sacrifice(source, game); - } - return true; + @Override + public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) { + if (!super.canTarget(controllerId, id, source, game)) { + return false; + } + MageObjectReference sourceRef = new MageObjectReference(source.getSourceObject(game), game); + Spell spell = game.getSpell(id); + if (spell == null) { + return false; + } + for (StackObject stackObject : game.getStack()) { + if (!(stackObject instanceof StackAbility)) { + continue; + } + Permanent permanent = ((StackAbility) stackObject).getSourcePermanentOrLKI(game); + if (permanent != null + && !sourceRef.refersTo(permanent, game) + && permanent.isCreature() + && "Goblin Artisans".equals(permanent.getName()) + && stackObject + .getStackAbility() + .getTargets() + .stream() + .map(Target::getTargets) + .flatMap(Collection::stream) + .anyMatch(id::equals)) { + return false; } } - - return false; - } - - @Override - public GoblinArtisansEffect copy() { - return new GoblinArtisansEffect(this); + return true; } }