[ATQ] fixed implementation of Goblin Artisans (fixes #7629)

This commit is contained in:
Evan Kranzler 2021-02-27 09:04:26 -05:00
parent 50071b59a0
commit d4311916a4

View file

@ -1,50 +1,49 @@
package mage.cards.g; package mage.cards.g;
import java.util.List;
import java.util.UUID;
import mage.MageInt; import mage.MageInt;
import mage.MageObjectReference;
import mage.abilities.Ability; import mage.abilities.Ability;
import mage.abilities.Mode;
import mage.abilities.common.SimpleActivatedAbility; import mage.abilities.common.SimpleActivatedAbility;
import mage.abilities.costs.common.TapSourceCost; 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.CardImpl;
import mage.cards.CardSetInfo; import mage.cards.CardSetInfo;
import mage.cards.Cards;
import mage.cards.CardsImpl;
import mage.constants.CardType; import mage.constants.CardType;
import mage.constants.SubType; import mage.constants.SubType;
import mage.constants.Outcome; import mage.constants.TargetController;
import mage.constants.Zone; import mage.filter.FilterSpell;
import mage.filter.FilterCard; import mage.filter.common.FilterArtifactSpell;
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.game.Game; import mage.game.Game;
import mage.game.permanent.Permanent; 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.Target;
import mage.target.TargetCard; import mage.target.TargetSpell;
import java.util.Collection;
import java.util.UUID;
/** /**
* * @author TheElk801
* @author MarcoMarin
*/ */
public final class GoblinArtisans extends CardImpl { public final class GoblinArtisans extends CardImpl {
public GoblinArtisans(UUID ownerId, CardSetInfo setInfo) { 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.GOBLIN);
this.subtype.add(SubType.ARTIFICER); this.subtype.add(SubType.ARTIFICER);
this.power = new MageInt(1); this.power = new MageInt(1);
this.toughness = 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. // {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) { 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 { static {
filter.add(new NamePredicate("Goblin Artisans")); filter.add(TargetController.YOU.getOwnerPredicate());
} }
public GoblinArtisansEffect() { GoblinArtisansTarget() {
super(Outcome.Damage); super(filter);
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.";
} }
public GoblinArtisansEffect(GoblinArtisansEffect effect) { private GoblinArtisansTarget(final GoblinArtisansTarget target) {
super(effect); super(target);
} }
@Override @Override
public boolean apply(Game game, Ability source) { public GoblinArtisansTarget copy() {
Player controller = game.getPlayer(source.getControllerId()); return new GoblinArtisansTarget(this);
if (controller != null) { }
if (controller.flipCoin(source, game, true)) {
controller.drawCards(1, source, game);
} else {
List<Permanent> 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;
}
filter.add(Predicates.not(new PermanentIdPredicate(source.getSourceId()))); @Override
//removed the activating instance of Artisans, btw, wasn't that filter declared as static final? How come I can do this here? :) public boolean canTarget(UUID controllerId, UUID id, Ability source, Game game) {
List<Permanent> list = game.getBattlefield().getAllActivePermanents(filter, game); if (!super.canTarget(controllerId, id, source, 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 return false;
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()) { MageObjectReference sourceRef = new MageObjectReference(source.getSourceObject(game), game);
Mode mode = abil.getModes().get(modeId); Spell spell = game.getSpell(id);
for (Target target : mode.getTargets()) { if (spell == null) {
for (UUID targetId : target.getTargets()) { return false;
artifacts.remove(game.getPermanentOrLKIBattlefield(targetId)); }
}// we could for (StackObject stackObject : game.getStack()) {
}// remove this if (!(stackObject instanceof StackAbility)) {
}//closing bracers continue;
}// pyramid, if it's bothering anyone }
} //they are all one-liners after all :) Permanent permanent = ((StackAbility) stackObject).getSourcePermanentOrLKI(game);
if (!artifacts.isEmpty()) { if (permanent != null
Cards cards = new CardsImpl(); && !sourceRef.refersTo(permanent, game)
for (Permanent perm : artifacts) { && permanent.isCreature()
cards.add(perm.getId()); && "Goblin Artisans".equals(permanent.getName())
} && stackObject
TargetCard target = new TargetCard(Zone.BATTLEFIELD, new FilterCard()); .getStackAbility()
controller.choose(Outcome.Sacrifice, cards, target, game); .getTargets()
game.getPermanent(target.getFirstTarget()).sacrifice(source, game); .stream()
} .map(Target::getTargets)
return true; .flatMap(Collection::stream)
.anyMatch(id::equals)) {
return false;
} }
} }
return true;
return false;
}
@Override
public GoblinArtisansEffect copy() {
return new GoblinArtisansEffect(this);
} }
} }